Created
May 9, 2020 21:01
-
-
Save Oppodelldog/2b319973afebd3a9103d7eab7fd4cabb to your computer and use it in GitHub Desktop.
Observer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Observer<T> { | |
protected handlers: Array<(arg0: T) => void>; | |
constructor() { | |
this.handlers = new Array<(arg0: T) => void>(); | |
} | |
public subscribe(f: (arg0: T) => void) { | |
this.handlers.push(f) | |
} | |
public unsubscribe(f: (arg0: T) => void) { | |
const index = this.handlers.indexOf(f); | |
if (index >= 0) { | |
this.handlers.splice(index, 1); | |
} | |
} | |
public notify(t: T) { | |
this.handlers.forEach((f) => f(t)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment