Simple implementation of observable pattern in Typescript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.editorconfig
.gitignore
LICENSE
README.md
package.json
tsconfig.json
tslint.json

README.md

daign-observable

NPM package

Simple implementation of observable pattern in Typescript

Installation

npm install @daign/observable --save

Usage

import {Observable} from '@daign/observable';

// Inherit from Observable
class MyClass extends Observable {
  private _x: number;

  public get x(): number {
    return this._x;
  }

  public set x( value: number ) {
    this._x = value;

    // Call notifyObservers to signal a change to the observers
    this.notifyObservers();
  }

  constructor() {
    super();
    this._x = 0;
  }
}

const myClass = new MyClass();

// Register a callback to be called on changes
const removeListener = myClass.subscribeToChanges( () => {
  console.log( myClass.x );
} );
myClass.x = 1;

// Remove the callback registration from the observable
removeListener();

Scripts

Build

npm run build

Run lint analysis

npm run lint

Run unit tests with code coverage

npm run test