abstract-observable
TypeScript icon, indicating that this package has built-in type declarations

0.2.5 • Public • Published

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();

Readme

Keywords

none

Package Sidebar

Install

npm i abstract-observable

Weekly Downloads

3

Version

0.2.5

License

MIT

Unpacked Size

701 kB

Total Files

327

Last publish

Collaborators

  • dan2dev