From af23b5d94784dd589d9a7059604c546ca7d98c57 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 9 Jan 2018 05:58:58 +1100 Subject: [PATCH] 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); } }); ```