1 | # event-emitter-es6
|
2 |
|
3 | A small event emitter library. Works in the browser and in Node. Can be used with es6 inheritance or as stand-alone lib.
|
4 |
|
5 | Inspired by the [tiny-emitter package on npm](https://www.npmjs.com/package/tiny-emitter).
|
6 |
|
7 | For more information you can see jsdoc info in `index.es6` file.
|
8 |
|
9 | Very simply applies to express.
|
10 |
|
11 | ## Install
|
12 |
|
13 | Node
|
14 |
|
15 | ```
|
16 | npm install event-emitter-es6 --save
|
17 | ```
|
18 |
|
19 | Browser
|
20 |
|
21 | ```
|
22 | bower install event-emitter-es6 --save
|
23 | ```
|
24 |
|
25 | ```html
|
26 | <script src="bower_components/event-emitter-es6/dist/event-emitter.min.js"></script>
|
27 | ```
|
28 |
|
29 | or apply via express
|
30 |
|
31 | ```javascript
|
32 | var express = require('express');
|
33 | var app = express();
|
34 |
|
35 | <... your code here ...>
|
36 |
|
37 | app.use( require('event-emitter-es6/router') );
|
38 | ```
|
39 |
|
40 | ```html
|
41 | <script src="event-emitter-es6/event-emitter.min.js"></script>
|
42 | ```
|
43 |
|
44 | ## Usage
|
45 |
|
46 | Node
|
47 |
|
48 | ```javascript
|
49 | var EventEmitter = require('event-emitter-es6');
|
50 | var emitter = new Emitter();
|
51 |
|
52 | emitter.on('some-event', function listener1 (param1, param2, param3) {
|
53 | console.info('listener1', param1, param2, param3);
|
54 | });
|
55 | emitter.on('some-event', function listener2 (param1, param2, param3) {
|
56 | console.info('listener2', param1, param2, param3);
|
57 | });
|
58 | emitter.emit('some-event', 'some-val1', 'some-val2', 'some-val3');
|
59 | ```
|
60 |
|
61 | Browser
|
62 |
|
63 | ```js
|
64 | var emitter = new EventEmitter();
|
65 |
|
66 | emitter.on('some-event', function listener1 (param1, param2, param3) {
|
67 | console.info('listener1', param1, param2, param3);
|
68 | });
|
69 |
|
70 | emitter.on('some-event', function listener2 (param1, param2, param3) {
|
71 | console.info('listener2', param1, param2, param3);
|
72 | });
|
73 |
|
74 | emitter.emit('some-event', 'some-val1', 'some-val2', 'some-val3');
|
75 | ```
|
76 |
|
77 | ES6
|
78 |
|
79 | ```js
|
80 | class SomeEmittingClass extends EventEmitter {
|
81 | constructor() {
|
82 | super();
|
83 | }
|
84 |
|
85 | fireExampleEvent() {
|
86 | this.emit('some-event', 'some-val1', 'some-val2', 'some-val3');
|
87 | }
|
88 | }
|
89 |
|
90 | var emittingInstance = new SomeEmittingClass();
|
91 |
|
92 | emittingInstance.on('some-event', function listener1 (param1, param2, param3) {
|
93 | console.info('listener1', param1, param2, param3);
|
94 | });
|
95 | emittingInstance.on('some-event', function listener2 (param1, param2, param3) {
|
96 | console.info('listener2', param1, param2, param3);
|
97 | });
|
98 | emittingInstance.fireExampleEvent();
|
99 | ```
|
100 |
|
101 |
|
102 | ## Instance Methods
|
103 |
|
104 | ### constructor([opts])
|
105 |
|
106 | An option can be passed to constructor
|
107 |
|
108 | * `opts` - settings object for create event emitter
|
109 | * `opts.emitDelay` - delay in ms to emit event. If passed 0 - all events emits synchronously. By default = 10
|
110 | * `opts.strictMode` - when emit event with no listeners - fires error. By default = false
|
111 |
|
112 | ### destroy()
|
113 |
|
114 | Completely destroys event emitter.
|
115 |
|
116 | ### on(event, callback)
|
117 |
|
118 | Subscribe to an event
|
119 |
|
120 | * `event` - the name of the event to subscribe to
|
121 | * `callback` - the function to call when event is emitted (for transfer context use __bind__ method of Function.prototype)
|
122 |
|
123 | ### once(event, callback)
|
124 |
|
125 | Subscribe to an event only **once**
|
126 |
|
127 | * `event` - the name of the event to subscribe to
|
128 | * `callback` - the function to call when event is emitted (for transfer context use __bind__ method of Function.prototype)
|
129 |
|
130 | ### off(event[, callback])
|
131 |
|
132 | Unsubscribe from an event or all events. If no callback is provided, it unsubscribes you from all events.
|
133 |
|
134 | * `event` - the name of the event to unsubscribe from
|
135 | * `callback` - the function used when binding to the event. If you used function with __bind__ method - must be passed the same function, that was getted after binding.
|
136 |
|
137 | ### emit(event[, ...arguments])
|
138 |
|
139 | Trigger a named event
|
140 |
|
141 | * `event` - the event name to emit
|
142 | * `arguments...` - any number of arguments to pass to the event subscribers
|
143 |
|
144 | ### emitSync(event[, ...arguments])
|
145 |
|
146 | Trigger a named event immediate (even the emitter was created as async instance)
|
147 |
|
148 | * `event` - the event name to emit
|
149 | * `arguments...` - any number of arguments to pass to the event subscribers
|
150 |
|
151 | ## Build
|
152 |
|
153 | Build (Browserifies, and minifies)
|
154 |
|
155 | ```
|
156 | npm install
|
157 | npm run build
|
158 | ```
|
159 |
|
160 | ## Test
|
161 |
|
162 | Test
|
163 |
|
164 | ```
|
165 | npm install
|
166 | npm run test
|
167 | ```
|
168 |
|
169 | ## Change list
|
170 |
|
171 | ### Version 1.1.5
|
172 |
|
173 | * destroy() method of class
|
174 |
|
175 | ### Version 1.1.4
|
176 |
|
177 | * All code covered with tests |
\ | No newline at end of file |