f5241f51ab4a01baf7fada1341079a9d5a3e48db0b5a7d8563c185b4ba3f7f27e0ff81f9aad4cc461d317a043a7540655733df548dca657acbbe643dba4a57 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # tiny-emitter
  2. A tiny (less than 1k) event emitter library.
  3. ## Install
  4. ### npm
  5. ```
  6. npm install tiny-emitter --save
  7. ```
  8. ## Usage
  9. ```js
  10. var Emitter = require('tiny-emitter');
  11. var emitter = new Emitter();
  12. emitter.on('some-event', function (arg1, arg2, arg3) {
  13. //
  14. });
  15. emitter.emit('some-event', 'arg1 value', 'arg2 value', 'arg3 value');
  16. ```
  17. Alternatively, you can skip the initialization step by requiring `tiny-emitter/instance` instead. This pulls in an already initialized emitter.
  18. ```js
  19. var emitter = require('tiny-emitter/instance');
  20. emitter.on('some-event', function (arg1, arg2, arg3) {
  21. //
  22. });
  23. emitter.emit('some-event', 'arg1 value', 'arg2 value', 'arg3 value');
  24. ```
  25. ## Instance Methods
  26. ### on(event, callback[, context])
  27. Subscribe to an event
  28. * `event` - the name of the event to subscribe to
  29. * `callback` - the function to call when event is emitted
  30. * `context` - (OPTIONAL) - the context to bind the event callback to
  31. ### once(event, callback[, context])
  32. Subscribe to an event only **once**
  33. * `event` - the name of the event to subscribe to
  34. * `callback` - the function to call when event is emitted
  35. * `context` - (OPTIONAL) - the context to bind the event callback to
  36. ### off(event[, callback])
  37. Unsubscribe from an event or all events. If no callback is provided, it unsubscribes you from all events.
  38. * `event` - the name of the event to unsubscribe from
  39. * `callback` - the function used when binding to the event
  40. ### emit(event[, arguments...])
  41. Trigger a named event
  42. * `event` - the event name to emit
  43. * `arguments...` - any number of arguments to pass to the event subscribers
  44. ## Test and Build
  45. Build (Tests, Browserifies, and minifies)
  46. ```
  47. npm install
  48. npm run build
  49. ```
  50. Test
  51. ```
  52. npm install
  53. npm test
  54. ```
  55. ## License
  56. [MIT](https://github.com/scottcorgan/tiny-emitter/blob/master/LICENSE)