aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/TypedEvent.ts
diff options
context:
space:
mode:
authorkimdahey <claire_kim1@brown.edu>2019-12-03 16:32:13 -0500
committerkimdahey <claire_kim1@brown.edu>2019-12-03 16:32:13 -0500
commit70583fa47bd9920d1823d381708c81283534d6ce (patch)
tree5c81976813436852403ea352797efe1d518dbb1a /src/client/util/TypedEvent.ts
parent56b83d89f37a5523ab319977e3385f539ecaf996 (diff)
parent213962406327cc2f7267064f3016fabf0fd16872 (diff)
merged w master
Diffstat (limited to 'src/client/util/TypedEvent.ts')
-rw-r--r--src/client/util/TypedEvent.ts62
1 files changed, 31 insertions, 31 deletions
diff --git a/src/client/util/TypedEvent.ts b/src/client/util/TypedEvent.ts
index 532ba78eb..90fd299c1 100644
--- a/src/client/util/TypedEvent.ts
+++ b/src/client/util/TypedEvent.ts
@@ -1,40 +1,40 @@
export interface Listener<T> {
- (event: T): any;
+ (event: T): any;
}
export interface Disposable {
- dispose(): void;
+ dispose(): void;
}
/** passes through events as they happen. You will not get events from before you start listening */
export class TypedEvent<T> {
- private listeners: Listener<T>[] = [];
- private listenersOncer: Listener<T>[] = [];
-
- on = (listener: Listener<T>): Disposable => {
- this.listeners.push(listener);
- return {
- dispose: () => this.off(listener)
- };
- }
-
- once = (listener: Listener<T>): void => {
- this.listenersOncer.push(listener);
- }
-
- off = (listener: Listener<T>) => {
- var callbackIndex = this.listeners.indexOf(listener);
- if (callbackIndex > -1) this.listeners.splice(callbackIndex, 1);
- }
-
- emit = (event: T) => {
- /** Update any general listeners */
- this.listeners.forEach((listener) => listener(event));
-
- /** Clear the `once` queue */
- this.listenersOncer.forEach((listener) => listener(event));
- this.listenersOncer = [];
- }
-
- pipe = (te: TypedEvent<T>): Disposable => this.on((e) => te.emit(e));
+ private listeners: Listener<T>[] = [];
+ private listenersOncer: Listener<T>[] = [];
+
+ on = (listener: Listener<T>): Disposable => {
+ this.listeners.push(listener);
+ return {
+ dispose: () => this.off(listener)
+ };
+ }
+
+ once = (listener: Listener<T>): void => {
+ this.listenersOncer.push(listener);
+ }
+
+ off = (listener: Listener<T>) => {
+ const callbackIndex = this.listeners.indexOf(listener);
+ if (callbackIndex > -1) this.listeners.splice(callbackIndex, 1);
+ }
+
+ emit = (event: T) => {
+ /** Update any general listeners */
+ this.listeners.forEach((listener) => listener(event));
+
+ /** Clear the `once` queue */
+ this.listenersOncer.forEach((listener) => listener(event));
+ this.listenersOncer = [];
+ }
+
+ pipe = (te: TypedEvent<T>): Disposable => this.on((e) => te.emit(e));
} \ No newline at end of file