[Modern] Move emitter to utils

This commit is contained in:
Jordan Eldredge 2021-09-11 13:20:19 -07:00
parent a174aaf17d
commit d7c4df7b66
2 changed files with 23 additions and 23 deletions

View file

@ -1,26 +1,4 @@
import { clamp } from "../utils";
class Emitter {
_cbs: { [event: string]: Array<() => void> } = {};
on(event: string, cb: () => void) {
if (this._cbs[event] == null) {
this._cbs[event] = [];
}
this._cbs[event].push(cb);
return () => {
this._cbs[event] = this._cbs[event].filter((c) => c !== cb);
};
}
trigger(event: string) {
const subscriptions = this._cbs[event];
if (subscriptions == null) {
return;
}
for (const cb of subscriptions) {
cb();
}
}
}
import { clamp, Emitter } from "../utils";
export class AudioPlayer {
_input: HTMLInputElement = document.createElement("input");

View file

@ -79,3 +79,25 @@ export function findLast<T>(
}
}
}
export class Emitter {
_cbs: { [event: string]: Array<() => void> } = {};
on(event: string, cb: () => void) {
if (this._cbs[event] == null) {
this._cbs[event] = [];
}
this._cbs[event].push(cb);
return () => {
this._cbs[event] = this._cbs[event].filter((c) => c !== cb);
};
}
trigger(event: string) {
const subscriptions = this._cbs[event];
if (subscriptions == null) {
return;
}
for (const cb of subscriptions) {
cb();
}
}
}