项目作者: Arction

项目描述 :
Simple and fast event emitter library.
高级语言: TypeScript
项目地址: git://github.com/Arction/Eventer.git
创建时间: 2019-07-04T12:37:36Z
项目社区:https://github.com/Arction/Eventer

开源协议:MIT License

下载


Eventer

Simple and fast event emitter library. Subscription is based on a unique Token, which can be used to unsubscribe from the topic.

The Eventer is used in LightningChart® JS charting library. https://www.arction.com/

Installation

npm install --save @arction/eventer

Documentation

Online documentation is available at arction.github.io/eventer

Getting started

  1. import { Eventer } from '@arction/eventer'
  2. const eventer = new Eventer()
  3. // new topic is created, handler is added as the first subscriber to the topic
  4. const token1 = eventer.on('topic1', () => console.log('I am called from topic1'))
  5. // 'topic1' already exists, so new listener is simply added to existing collection of handlers
  6. const token2 = eventer.on('topic1', () => console.log('I am also called from topic1'))
  7. // new collection of handlers is created for the second topic
  8. // the function is added to it
  9. eventer.on('topic2', () => console.log('I am called from topic2'))
  10. eventer.emit('topic1')
  11. // calls two functions which listen to the event
  12. //
  13. // outputs:
  14. // I am called from topic1
  15. // I am also called from topic1
  16. eventer.emit('topic2')
  17. // calls single functions which listen to the event
  18. //
  19. // outputs:
  20. // I am called from topic2
  21. // remove the second listener from the first topic by token
  22. eventer.off(token2)
  23. eventer.emit('topic1')
  24. // calls single functions which listen to the event
  25. //
  26. // outputs:
  27. // I am called from topic1
  28. // remove all listeners from all topics
  29. eventer.allOff()
  30. eventer.emit('topic2')
  31. // nothing happened, since all subscriptions were terminated

Subscribe to topic

Subscribing to a topic is easy.

  1. eventer.on('topic', ()=>{ console.log('Topic handler') })

Multiple subscriptions to a single topic can exist.

  1. eventer.on('topic', ()=>{ console.log('Topic handler') })
  2. eventer.on('topic2', ()=>{ console.log('Topic 2 handler') })
  3. eventer.on('topic3', ()=>{ console.log('Topic 3 handler') })

Unsubscribe from topic

You can unsubscribe from a single topic by using the Token returned by eventer when you subscribed to the topic.

  1. const token = eventer.on('topic', ()=>{ console.log('Topic handler') })
  2. eventer.off(token)

You can also unsubscibe all listeners from a topic.

  1. eventer.topicOff('topic')

It is also possible to unsubscribe all topics and all listeners.

  1. eventer.allOff()

Emiting events

Events can be emitted for a topic. The emit can contain 0 or more arguments that will be provided to the topic listeners.

  1. eventer.emit('topic')
  2. eventer.on('topic2', ( arg1, arg2 ) => console.log( arg1, arg2 ))
  3. eventer.emit('topic2', 'Hello ', 'World')

Development instructions

The project is developed using TypeScript. Build system of the project heavily relies on Node.js. Dependencies are managed with npm, therefore, remember to run npm install before starting of anything else.

There are several npm scripts, which are used for development process:

Name Command Description
build npm run build run build system
test npm test run tests and watch
lint npm run lint run static analyzer and watch
docs npm run docs generate typedoc documentation
ci:test npm run ci:test run tests once
ci:lint npm run ci:lint run static analyzer once
ci:watch npm run ci:watch run CI circle and watch