0.1.1 • Published 7 years ago

event-watcher v0.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

event-watcher Build Status

Smart event manager

Getting starter

Install via npm

npm install event-watcher

and init

const watcher = new EventWatcher();

Using

You can use mongodb like filter syntax, you can see more info about it https://github.com/crcn/sift.js

watcher
  .on('test-event')
  //once('test-event')
  .filter({name: 'bob', age: {$gt: 10}})
  .handle((resource) => {
    // executed just one time with resource={name: 'bob', age: 22}
  });

watcher.emit('test-event', {name: 'bo'});
watcher.emit('test-event', {name: 'bob', age: 22});
watcher.emit('test-event', {name: 'bob', age: 10});

Use custom filters

If you want use custom filters, just put callback-filter that results boolean

watcher
  .on('test-event')
  .filter(resource => resource.name === 'bob' && resource.age > 10)
  .handle((resource) => {
    // executed just one time with resource={name: 'bob', age: 22}
  });

watcher.emit('test-event', {name: 'bo'});
watcher.emit('test-event', {name: 'bob', age: 22});
watcher.emit('test-event', {name: 'bob', age: 10});

Killing handlers

const handler = watcher.on('test-event').filter({age: 10}).handle(() => {});

handler.off();
// or
watcher.off('test-event', handler);
0.1.1

7 years ago

0.1.0

7 years ago