Skip to content

Instantly share code, notes, and snippets.

@Oppodelldog
Created May 9, 2020 21:01
Show Gist options
  • Save Oppodelldog/2b319973afebd3a9103d7eab7fd4cabb to your computer and use it in GitHub Desktop.
Save Oppodelldog/2b319973afebd3a9103d7eab7fd4cabb to your computer and use it in GitHub Desktop.
Observer
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