You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.6 KiB

7 years ago
  1. # Ultron
  2. [![Made by unshift](https://img.shields.io/badge/made%20by-unshift-00ffcc.svg?style=flat-square)](http://unshift.io)[![Version npm](http://img.shields.io/npm/v/ultron.svg?style=flat-square)](http://browsenpm.org/package/ultron)[![Build Status](http://img.shields.io/travis/unshiftio/ultron/master.svg?style=flat-square)](https://travis-ci.org/unshiftio/ultron)[![Dependencies](https://img.shields.io/david/unshiftio/ultron.svg?style=flat-square)](https://david-dm.org/unshiftio/ultron)[![Coverage Status](http://img.shields.io/coveralls/unshiftio/ultron/master.svg?style=flat-square)](https://coveralls.io/r/unshiftio/ultron?branch=master)[![IRC channel](http://img.shields.io/badge/IRC-irc.freenode.net%23unshift-00a8ff.svg?style=flat-square)](http://webchat.freenode.net/?channels=unshift)
  3. Ultron is a high-intelligence robot. It gathers intelligence so it can start
  4. improving upon his rudimentary design. It will learn your event emitting
  5. patterns and find ways to exterminate them. Allowing you to remove only the
  6. event emitters that **you** assigned and not the ones that your users or
  7. developers assigned. This can prevent race conditions, memory leaks and even file
  8. descriptor leaks from ever happening as you won't remove clean up processes.
  9. ## Installation
  10. The module is designed to be used in browsers using browserify and in Node.js.
  11. You can install the module through the public npm registry by running the
  12. following command in CLI:
  13. ```
  14. npm install --save ultron
  15. ```
  16. ## Usage
  17. In all examples we assume that you've required the library as following:
  18. ```js
  19. 'use strict';
  20. var Ultron = require('ultron');
  21. ```
  22. Now that we've required the library we can construct our first `Ultron` instance.
  23. The constructor requires one argument which should be the `EventEmitter`
  24. instance that we need to operate upon. This can be the `EventEmitter` module
  25. that ships with Node.js or `EventEmitter3` or anything else as long as it
  26. follow the same API and internal structure as these 2. So with that in mind we
  27. can create the instance:
  28. ```js
  29. //
  30. // For the sake of this example we're going to construct an empty EventEmitter
  31. //
  32. var EventEmitter = require('events').EventEmitter; // or require('eventmitter3');
  33. var events = new EventEmitter();
  34. var ultron = new Ultron(events);
  35. ```
  36. You can now use the following API's from the Ultron instance:
  37. ### Ultron.on
  38. Register a new event listener for the given event. It follows the exact same API
  39. as `EventEmitter.on` but it will return itself instead of returning the
  40. EventEmitter instance. If you are using EventEmitter3 it also supports the
  41. context param:
  42. ```js
  43. ultron.on('event-name', handler, { custom: 'function context' });
  44. ```
  45. ### Ultron.once
  46. Exactly the same as the [Ultron.on](#ultronon) but it only allows the execution
  47. once.
  48. ### Ultron.remove
  49. This is where all the magic happens and the safe removal starts. This function
  50. accepts different argument styles:
  51. - No arguments, assume that all events need to be removed so it will work as
  52. `removeAllListeners()` API.
  53. - 1 argument, when it's a string it will be split on ` ` and `,` to create a
  54. list of events that need to be cleared.
  55. - Multiple arguments, we assume that they are all names of events that need to
  56. be cleared.
  57. ```js
  58. ultron.remove('foo, bar baz'); // Removes foo, bar and baz.
  59. ultron.remove('foo', 'bar', 'baz'); // Removes foo, bar and baz.
  60. ultron.remove(); // Removes everything.
  61. ```
  62. If you just want to remove a single event listener using a function reference
  63. you can still use the EventEmitter's `removeListener(event, fn)` API:
  64. ```js
  65. function foo() {}
  66. ulton.on('foo', foo);
  67. events.removeListener('foo', foo);
  68. ```
  69. ## License
  70. MIT