UNPKG

4.04 kBMarkdownView Raw
1# event-emitter-es6
2
3A small event emitter library. Works in the browser and in Node. Can be used with es6 inheritance or as stand-alone lib.
4
5Inspired by the [tiny-emitter package on npm](https://www.npmjs.com/package/tiny-emitter).
6
7For more information you can see jsdoc info in `index.es6` file.
8
9Very simply applies to express.
10
11## Install
12
13Node
14
15```
16npm install event-emitter-es6 --save
17```
18
19Browser
20
21```
22bower 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
29or apply via express
30
31```javascript
32var express = require('express');
33var app = express();
34
35<... your code here ...>
36
37app.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
46Node
47
48```javascript
49var EventEmitter = require('event-emitter-es6');
50var emitter = new Emitter();
51
52emitter.on('some-event', function listener1 (param1, param2, param3) {
53 console.info('listener1', param1, param2, param3);
54});
55emitter.on('some-event', function listener2 (param1, param2, param3) {
56 console.info('listener2', param1, param2, param3);
57});
58emitter.emit('some-event', 'some-val1', 'some-val2', 'some-val3');
59```
60
61Browser
62
63```js
64var emitter = new EventEmitter();
65
66emitter.on('some-event', function listener1 (param1, param2, param3) {
67 console.info('listener1', param1, param2, param3);
68});
69
70emitter.on('some-event', function listener2 (param1, param2, param3) {
71 console.info('listener2', param1, param2, param3);
72});
73
74emitter.emit('some-event', 'some-val1', 'some-val2', 'some-val3');
75```
76
77ES6
78
79```js
80class 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
90var emittingInstance = new SomeEmittingClass();
91
92emittingInstance.on('some-event', function listener1 (param1, param2, param3) {
93 console.info('listener1', param1, param2, param3);
94});
95emittingInstance.on('some-event', function listener2 (param1, param2, param3) {
96 console.info('listener2', param1, param2, param3);
97});
98emittingInstance.fireExampleEvent();
99```
100
101
102## Instance Methods
103
104### constructor([opts])
105
106An 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
114Completely destroys event emitter.
115
116### on(event, callback)
117
118Subscribe 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
125Subscribe 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
132Unsubscribe 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
139Trigger 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
146Trigger 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
153Build (Browserifies, and minifies)
154
155```
156npm install
157npm run build
158```
159
160## Test
161
162Test
163
164```
165npm install
166npm 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