Update createEventHub.md

This commit is contained in:
atomiks
2018-01-09 05:46:17 +11:00
committed by GitHub
parent 823236dbf7
commit f18b4942b9

View File

@ -2,7 +2,7 @@
Creates a pub/sub ([publishsubscribe](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
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(obj, fn); // subscribe a handler to listen for the object
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
```