cache the getting
This commit is contained in:
@ -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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user