mitt

https://github.com/developit/mitt

  • mitt
  • all
  • on
  • off
  • emit ```jsx npm install —save mitt

//

import mitt from ‘mitt’

const emitter = mitt()

// listen to an event emitter.on(‘foo’, e => console.log(‘foo’, e) )

// listen to all events emitter.on(‘*’, (type, e) => console.log(type, e) )

// fire an event emitter.emit(‘foo’, { a: ‘b’ })

// clearing all events emitter.all.clear()

// working with handler references: function onFoo() {} emitter.on(‘foo’, onFoo) // listen emitter.off(‘foo’, onFoo) // unlisten

  1. <a name="GLRxz"></a>
  2. ### ts
  3. ```tsx
  4. import mitt from 'mitt';
  5. type Events = {
  6. foo: string;
  7. bar?: number;
  8. };
  9. const emitter = mitt<Events>(); // inferred as Emitter<Events>
  10. emitter.on('foo', (e) => {}); // 'e' has inferred type 'string'
  11. emitter.emit('foo', 42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)

PubSubJS

https://github.com/mroderick/PubSubJS

  1. npm install pubsub-js
  2. import PubSub from 'pubsub-js'