Update createEventHub.md
This commit is contained in:
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Creates a pub/sub ([publish–subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern)) event hub with `emit`, `on`, and `off` methods.
|
Creates a pub/sub ([publish–subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern)) event hub with `emit`, `on`, and `off` methods.
|
||||||
|
|
||||||
Instantiate a new `Map` object to allow any event type (including objects) to be the key, and also so object prototype property names are not resolved.
|
Instantiate a new `Map` object to allow any event type (including objects) to be the key, and also so `Object.prototype` property names are not resolved.
|
||||||
|
|
||||||
For `emit`, resolve the array of handlers based on the `event` argument and then run
|
For `emit`, resolve the array of handlers based on the `event` argument and then run
|
||||||
each one with `Array.forEach()` by passing in the data as an argument.
|
each one with `Array.forEach()` by passing in the data as an argument.
|
||||||
@ -37,6 +37,6 @@ const hub = createEventHub();
|
|||||||
hub.on('message', fn); // subscribe a handler to listen for 'message' events
|
hub.on('message', fn); // subscribe a handler to listen for 'message' events
|
||||||
hub.on(obj, fn); // subscribe a handler to listen for the object
|
hub.on(obj, fn); // subscribe a handler to listen for the object
|
||||||
hub.emit('message', 'hello!'); // console logs 'hello!'
|
hub.emit('message', 'hello!'); // console logs 'hello!'
|
||||||
hub.emit(obj, 'hello!'); // console logs 'hello'
|
hub.emit(obj, 'hello!'); // console logs 'hello!'
|
||||||
hub.off('message', fn); // unsubscribe our handler from 'message', the obj event will still work
|
hub.off('message', fn); // unsubscribe our handler from 'message', the obj event will still work
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user