cache the getting

This commit is contained in:
atomiks
2018-01-09 05:58:58 +11:00
committed by GitHub
parent f18b4942b9
commit af23b5d947

View File

@ -19,12 +19,14 @@ const createEventHub = () => ({
(this.hub.get(event) || []).forEach(handler => handler(data)); (this.hub.get(event) || []).forEach(handler => handler(data));
}, },
on(event, handler) { on(event, handler) {
if (!this.hub.get(event)) this.hub.set(event, []); const handlers = this.hub.get(event);
this.hub.get(event).push(handler); if (!handlers) this.hub.set(event, []);
handlers.push(handler);
}, },
off(event, handler) { off(event, handler) {
const i = (this.hub.get(event) || []).findIndex(h => h === handler); const handlers = this.hub.get(event);
if (i > -1) this.hub.get(event).splice(i, 1); const i = (handlers || []).findIndex(h => h === handler);
if (i > -1) handlers.splice(i, 1);
} }
}); });
``` ```