From 3bb85243ad439c9c344328e90fce6145e88685b5 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 6 Jan 2018 00:07:26 +1100 Subject: [PATCH 01/19] Add createEventHub --- snippets/createEventHub.md | 36 ++++++++++++++++++++++++++++++++++++ tag_database | 1 + 2 files changed, 37 insertions(+) create mode 100644 snippets/createEventHub.md diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md new file mode 100644 index 000000000..9804e9e4a --- /dev/null +++ b/snippets/createEventHub.md @@ -0,0 +1,36 @@ +### createEventHub + +Creates a pubsub event hub with `emit`, `on`, and `off` methods. + +For `emit`, resolve the array of handlers based on the `event` argument and then run +each one by passing in the data as an argument. + +For `on`, create an array for the event if it does not yet exist, then push the handler +into the array. + +For `off`, find the index of the handler in the event array and remove it. + +```js +const createEventHub = () => ({ + hub: {}, + emit(event, data) { + this.hub[event].forEach(handler => handler(data)); + }, + on(event, handler) { + if (!this.hub[event]) this.hub[event] = []; + this.hub[event].push(handler); + }, + off(event, handler) { + const i = this.hub[event].findIndex(h => h === handler); + if (i) this.hub[event].splice(i, 1); + } +}); +``` + +```js +const hub = createEventHub(); +const fn = data => console.log(data); +hub.on('message', fn); // subscribe a handler to listen for 'message' events +hub.emit('message', 'hello!'); // console logs 'hello!' +hub.off('message', fn); // unsubscribe our handler from 'message' +``` diff --git a/tag_database b/tag_database index dc0014097..a596effd8 100644 --- a/tag_database +++ b/tag_database @@ -20,6 +20,7 @@ compose:function copyToClipboard:browser countOccurrences:array countVowels:string +createEventHub:utility currentURL:browser curry:function deepFlatten:array From 3ac45b6be39075efd0154300b231313892250dcd Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 6 Jan 2018 00:09:51 +1100 Subject: [PATCH 02/19] Update createEventHub.md --- snippets/createEventHub.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index 9804e9e4a..c1cd73097 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -3,12 +3,12 @@ Creates a pubsub event hub with `emit`, `on`, and `off` methods. For `emit`, resolve the array of handlers based on the `event` argument and then run -each one by passing in the data as an argument. +each one with `Array.forEach()` by passing in the data as an argument. -For `on`, create an array for the event if it does not yet exist, then push the handler -into the array. +For `on`, create an array for the event if it does not yet exist, then use `Array.push()` to add the handler +to the array. -For `off`, find the index of the handler in the event array and remove it. +For `off`, use `Array.findIndex()` to find the index of the handler in the event array and remove it. ```js const createEventHub = () => ({ From c2140cb3aad8c5cbbb8117a7f11f87cd79be77b6 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 6 Jan 2018 00:18:41 +1100 Subject: [PATCH 03/19] Update createEventHub.md --- snippets/createEventHub.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index c1cd73097..86ded9037 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -8,7 +8,7 @@ each one with `Array.forEach()` by passing in the data as an argument. For `on`, create an array for the event if it does not yet exist, then use `Array.push()` to add the handler to the array. -For `off`, use `Array.findIndex()` to find the index of the handler in the event array and remove it. +For `off`, use `Array.findIndex()` to find the index of the handler in the event array and remove it using `Array.splice()`. ```js const createEventHub = () => ({ From 9f7fc75c4fab051fb31732c119d8ff3c4d2579f5 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 6 Jan 2018 00:26:30 +1100 Subject: [PATCH 04/19] fix index typo --- snippets/createEventHub.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index 86ded9037..e1117b7db 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -22,7 +22,7 @@ const createEventHub = () => ({ }, off(event, handler) { const i = this.hub[event].findIndex(h => h === handler); - if (i) this.hub[event].splice(i, 1); + if (i > -1) this.hub[event].splice(i, 1); } }); ``` From 4b6b5743c5b86a83f79bf21d917f699907e306d4 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 6 Jan 2018 00:30:47 +1100 Subject: [PATCH 05/19] silence error for `emit` --- snippets/createEventHub.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index e1117b7db..65690fa92 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -14,7 +14,7 @@ For `off`, use `Array.findIndex()` to find the index of the handler in the event const createEventHub = () => ({ hub: {}, emit(event, data) { - this.hub[event].forEach(handler => handler(data)); + (this.hub[event] || []).forEach(handler => handler(data)); }, on(event, handler) { if (!this.hub[event]) this.hub[event] = []; From 217bf29360172c4af56c409291c22ce38fa7377a Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 6 Jan 2018 01:26:07 +1100 Subject: [PATCH 06/19] Update createEventHub.md --- snippets/createEventHub.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index 65690fa92..66c718bc7 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -21,7 +21,7 @@ const createEventHub = () => ({ this.hub[event].push(handler); }, off(event, handler) { - const i = this.hub[event].findIndex(h => h === handler); + const i = (this.hub[event] || []).findIndex(h => h === handler); if (i > -1) this.hub[event].splice(i, 1); } }); From 3de2c5138d633520dfc827844e7124939f93113d Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 6 Jan 2018 02:56:15 +1100 Subject: [PATCH 07/19] Update tag_database --- tag_database | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tag_database b/tag_database index a596effd8..212c1a883 100644 --- a/tag_database +++ b/tag_database @@ -20,7 +20,7 @@ compose:function copyToClipboard:browser countOccurrences:array countVowels:string -createEventHub:utility +createEventHub:browser,utility,advanced currentURL:browser curry:function deepFlatten:array From c2fcbe6d208cdefd9a7f68b17416d0bb59edfb31 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 6 Jan 2018 07:42:11 +1100 Subject: [PATCH 08/19] Update createEventHub.md --- snippets/createEventHub.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index 66c718bc7..c0530a7bd 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -1,6 +1,6 @@ ### createEventHub -Creates a pubsub 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. 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. From 578a655062858c3bddcb172dc8fb48221d9f6dc9 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 5 Jan 2018 23:57:11 +0200 Subject: [PATCH 09/19] Update tag_database --- tag_database | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tag_database b/tag_database index 58ec130d5..4b6eaec24 100644 --- a/tag_database +++ b/tag_database @@ -21,7 +21,7 @@ countOccurrences:array countVowels:string currentURL:browser,url curry:function,recursion -createEventHub:browser,utility,advanced +createEventHub:browser,event,advanced deepFlatten:array,recursion defer:function detectDeviceType:browser From 823236dbf778c882c5d371836630cd9b341faa84 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 9 Jan 2018 05:44:23 +1100 Subject: [PATCH 10/19] Use Map instead --- snippets/createEventHub.md | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index c0530a7bd..decc156b7 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -2,6 +2,8 @@ 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. + 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. @@ -12,25 +14,29 @@ For `off`, use `Array.findIndex()` to find the index of the handler in the event ```js const createEventHub = () => ({ - hub: {}, + hub: new Map(), emit(event, data) { - (this.hub[event] || []).forEach(handler => handler(data)); + (this.hub.get(event) || []).forEach(handler => handler(data)); }, on(event, handler) { - if (!this.hub[event]) this.hub[event] = []; - this.hub[event].push(handler); + if (!this.hub.get(event)) this.hub.set(event, []); + this.hub.get(event).push(handler); }, off(event, handler) { - const i = (this.hub[event] || []).findIndex(h => h === handler); - if (i > -1) this.hub[event].splice(i, 1); + const i = (this.hub.get(event) || []).findIndex(h => h === handler); + if (i > -1) this.hub.get(event).splice(i, 1); } }); ``` ```js -const hub = createEventHub(); const fn = data => console.log(data); +const obj = {}; + +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.off('message', fn); // unsubscribe our handler from 'message' +hub.emit(obj, 'hello!'); // console logs 'hello' +hub.off('message', fn); // unsubscribe our handler from 'message', the obj event will still work ``` From f18b4942b94747b74498015810c0850d058a4bd3 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 9 Jan 2018 05:46:17 +1100 Subject: [PATCH 11/19] Update createEventHub.md --- snippets/createEventHub.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index decc156b7..3b0e10c33 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -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. -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 ``` From af23b5d94784dd589d9a7059604c546ca7d98c57 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 9 Jan 2018 05:58:58 +1100 Subject: [PATCH 12/19] cache the getting --- snippets/createEventHub.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index 3b0e10c33..3740df1bd 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -19,12 +19,14 @@ const createEventHub = () => ({ (this.hub.get(event) || []).forEach(handler => handler(data)); }, on(event, handler) { - if (!this.hub.get(event)) this.hub.set(event, []); - this.hub.get(event).push(handler); + const handlers = this.hub.get(event); + if (!handlers) this.hub.set(event, []); + handlers.push(handler); }, off(event, handler) { - const i = (this.hub.get(event) || []).findIndex(h => h === handler); - if (i > -1) this.hub.get(event).splice(i, 1); + const handlers = this.hub.get(event); + const i = (handlers || []).findIndex(h => h === handler); + if (i > -1) handlers.splice(i, 1); } }); ``` From 2961ba996ac1dc6557f874fb5f36eb683c31801b Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 9 Jan 2018 08:02:35 +1100 Subject: [PATCH 13/19] Update createEventHub.md --- snippets/createEventHub.md | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index 3740df1bd..555f5aaf8 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -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. -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. +Create an empty `hub` property using `Object.create(null)` to create a truly empty object that does not inherit properties from `Object.prototype` (which would be resolved if the event name matched one of the properties). 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. @@ -14,31 +14,26 @@ For `off`, use `Array.findIndex()` to find the index of the handler in the event ```js const createEventHub = () => ({ - hub: new Map(), + hub: Object.create(null), emit(event, data) { - (this.hub.get(event) || []).forEach(handler => handler(data)); + (this.hub[event] || []).forEach(handler => handler(data)); }, on(event, handler) { - const handlers = this.hub.get(event); - if (!handlers) this.hub.set(event, []); - handlers.push(handler); + if (!this.hub[event]) this.hub[event] = []; + this.hub[event].push(handler); }, off(event, handler) { - const handlers = this.hub.get(event); - const i = (handlers || []).findIndex(h => h === handler); - if (i > -1) handlers.splice(i, 1); + const i = (this.hub[event] || []).findIndex(h => h === handler); + if (i > -1) this.hub[event].splice(i, 1); } }); ``` ```js const fn = data => console.log(data); -const obj = {}; - 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.off('message', fn); // unsubscribe our handler from 'message', the obj event will still work +hub.off('message', fn); // unsubscribe our handler from 'message' ``` From da99919b6d1f4a78ad44544274150cf2d70d2a4e Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 9 Jan 2018 08:42:54 +1100 Subject: [PATCH 14/19] Update createEventHub.md --- snippets/createEventHub.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index 555f5aaf8..a19b11558 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -30,10 +30,10 @@ const createEventHub = () => ({ ``` ```js -const fn = data => console.log(data); +const fn = data => console.log(data.hello); const hub = createEventHub(); -hub.on('message', fn); // subscribe a handler to listen for 'message' events -hub.emit('message', 'hello!'); // console logs 'hello!' -hub.off('message', fn); // unsubscribe our handler from 'message' +hub.on('message', fn); // subscribe the handler to listen for 'message' events +hub.emit('message', { hello: 'world' }); // logs 'world' +hub.off('message', fn); // unsubscribe the handler from 'message' events ``` From 409a05e52c1cb85f5004b8b8ef997d5a8cca502e Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 9 Jan 2018 09:15:59 +1100 Subject: [PATCH 15/19] More extensive examples --- snippets/createEventHub.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index a19b11558..83b9e78da 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -30,10 +30,18 @@ const createEventHub = () => ({ ``` ```js -const fn = data => console.log(data.hello); +const handler = data => console.log(data); const hub = createEventHub(); -hub.on('message', fn); // subscribe the handler to listen for 'message' events -hub.emit('message', { hello: 'world' }); // logs 'world' -hub.off('message', fn); // unsubscribe the handler from 'message' events +// Listen for different types of events +hub.on('message', handler); // subscribe the handler to listen for a 'message' event +hub.on('message', () => console.log('Message event fired')); // subscribe a different handler to listen for a 'message' event +hub.on('double', number => console.log(number * 2)); // subscribe a handler to listen for a 'double' event + +// Emit events to invoke all subscribed handlers to them, passing the data to them as an argument +hub.emit('message', 'hello world'); // logs 'hello world' and 'Message event fired' +hub.emit('message', { hello: 'world' }); // logs the object and 'Message event fired' +hub.emit('double', 5); // logs 10 + +hub.off('message', handler); // unsubscribe a single handler from the 'message' event ``` From bdae0ed89f6bb940a83e7f8aa3fdc31a2d49ba58 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 9 Jan 2018 09:17:15 +1100 Subject: [PATCH 16/19] Update createEventHub.md --- snippets/createEventHub.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index 83b9e78da..57ed523b7 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -38,7 +38,7 @@ hub.on('message', handler); // subscribe the handler to listen for a 'message' e hub.on('message', () => console.log('Message event fired')); // subscribe a different handler to listen for a 'message' event hub.on('double', number => console.log(number * 2)); // subscribe a handler to listen for a 'double' event -// Emit events to invoke all subscribed handlers to them, passing the data to them as an argument +// Emit events to invoke all handlers subscribed to them, passing the data to them as an argument hub.emit('message', 'hello world'); // logs 'hello world' and 'Message event fired' hub.emit('message', { hello: 'world' }); // logs the object and 'Message event fired' hub.emit('double', 5); // logs 10 From b906114c10501496bae58986df2b38e602be47c3 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 9 Jan 2018 09:21:32 +1100 Subject: [PATCH 17/19] Update createEventHub.md --- snippets/createEventHub.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index 57ed523b7..44eb57063 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -32,16 +32,18 @@ const createEventHub = () => ({ ```js const handler = data => console.log(data); const hub = createEventHub(); +let increment = 0; -// Listen for different types of events -hub.on('message', handler); // subscribe the handler to listen for a 'message' event -hub.on('message', () => console.log('Message event fired')); // subscribe a different handler to listen for a 'message' event -hub.on('double', number => console.log(number * 2)); // subscribe a handler to listen for a 'double' event +// Subscribe/listen for different types of events +hub.on('message', handler); +hub.on('message', () => console.log('Message event fired')); +hub.on('increment', () => increment++); // Emit events to invoke all handlers subscribed to them, passing the data to them as an argument hub.emit('message', 'hello world'); // logs 'hello world' and 'Message event fired' hub.emit('message', { hello: 'world' }); // logs the object and 'Message event fired' -hub.emit('double', 5); // logs 10 +hub.emit('increment'); // `increment` variable is now 1 -hub.off('message', handler); // unsubscribe a single handler from the 'message' event +// Unsubscribe/unlisten a handler from the 'message' event +hub.off('message', handler); ``` From a098cc63315f3fcf439a48e14c59ef8d589ebde6 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 9 Jan 2018 09:32:47 +1100 Subject: [PATCH 18/19] Update createEventHub.md --- snippets/createEventHub.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index 44eb57063..c368d5d6e 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -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. -Create an empty `hub` property using `Object.create(null)` to create a truly empty object that does not inherit properties from `Object.prototype` (which would be resolved if the event name matched one of the properties). +Set a `hub` property using `Object.create(null)` to create a truly empty object that does not inherit properties from `Object.prototype` (which would be resolved if the event name matched one of the properties). 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. @@ -34,16 +34,16 @@ const handler = data => console.log(data); const hub = createEventHub(); let increment = 0; -// Subscribe/listen for different types of events +// Subscribe: listen for different types of events hub.on('message', handler); hub.on('message', () => console.log('Message event fired')); hub.on('increment', () => increment++); -// Emit events to invoke all handlers subscribed to them, passing the data to them as an argument +// Publish: emit events to invoke all handlers subscribed to them, passing the data to them as an argument hub.emit('message', 'hello world'); // logs 'hello world' and 'Message event fired' hub.emit('message', { hello: 'world' }); // logs the object and 'Message event fired' hub.emit('increment'); // `increment` variable is now 1 -// Unsubscribe/unlisten a handler from the 'message' event +// Unsubscribe: stop a specific handler from listening to the 'message' event hub.off('message', handler); ``` From c7310ca70be2aa4d00f3e3b2601c0d353d611bac Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 9 Jan 2018 14:17:33 +0200 Subject: [PATCH 19/19] Update createEventHub.md --- snippets/createEventHub.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/snippets/createEventHub.md b/snippets/createEventHub.md index c368d5d6e..de2b0139e 100644 --- a/snippets/createEventHub.md +++ b/snippets/createEventHub.md @@ -2,14 +2,10 @@ Creates a pub/sub ([publish–subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern)) event hub with `emit`, `on`, and `off` methods. -Set a `hub` property using `Object.create(null)` to create a truly empty object that does not inherit properties from `Object.prototype` (which would be resolved if the event name matched one of the properties). - -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. - +Use `Object.create(null)` to create an empty `hub` object that does not inherit properties from `Object.prototype`. +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. For `on`, create an array for the event if it does not yet exist, then use `Array.push()` to add the handler to the array. - For `off`, use `Array.findIndex()` to find the index of the handler in the event array and remove it using `Array.splice()`. ```js