abstract-observable
v0.2.5
Published
Simple typescript abstract class for observer pattern.
Readme
abstract-observable
Simple typescript abstract class for observer pattern.
Install
yarn add abstract-observableor
npm install --save abstract-observable1 - Create an observable class
export class WantToBeObserved extends Observable {
public someAction() {
// do stuff here then notify
this.notify();
}
}
const observableInstance = new WantToBeObserved();2 - Create some observers
export class WantToObserve implements IObserver {
public notify(): void {
// do stuff here
}
}
export function wantToObserveFunction(): void {
// do some other stuff here
}3 - Subscribe the observers
// subscribe class instance
const observerInstance = new WantToObserve();
const unsubscribeInstance = observableInstance.subscribe(observerInstance);
// subscribe function
observableInstance.subscribe(wantToObserveFunction);
// to unsubscribe call the return of subscribe
unsubscribeInstance();