0.2.5 • Published 6 years ago
abstract-observable v0.2.5
abstract-observable
Simple typescript abstract class for observer pattern.
Install
yarn add abstract-observable
or
npm install --save abstract-observable
1 - 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();