GithubHelp home page GithubHelp logo

node-newsemitter's Introduction

node-newsemitter

An event emitter that emits only new events. Because sometimes we don't care about old events.

Depfu codecov

Usage

const NewsEmitter = require('newsemitter');
const news = new NewsEmitter();

news.on('item', (item) => {
  console.log('new item:', item.title);
});

news.emit('item', { title: 'hello there' });
news.emit('item', { title: 'hello world' });
news.emit('item', { title: 'hello there' });
news.emit('item', { title: 'hey' });
news.emit('item', { title: 'hey' });
news.emit('item', { title: 'hey' });

// new item: hello there
// new item: hello world
// new item: hey

API

new NewsEmitter([options])

Creates an instance of a NewsEmitter. options can be

  • filter - An array of events that will be affected. If not given, event names will not be filtered.
  • ignore - An array of events that will be ignored. Defaults to ['newListener', 'removeListener'].
  • maxHistory - Maximum number of history items to remember. Default is 10.
  • manageHistory - If true, does not add emitted events to history. Instead expects you to manually manage history with addHistory(). Defaults to false.
  • identifier - Function used to compare one event to another. Called with a list of arguments that NewsEmitter#emit() is called with, sans event name. Should return a string. Default is JSON.stringify.

NewsEmitter#emit(event)

Emits an event, only if it has not been emitted before. Returns true if item is new and emtted. False otherwise.

NewsEmitter#reset([event])

Resets history of an event. If no event given, resets all history.

NewsEmitter#addHistory(event, arr)

Adds items in arr as event's history. Truncated as necessary based on max history length considering the last item in the array as newest.

Install

npm install newsemitter

Tests

Tests are written with mocha

npm test

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.