@daign/observable
TypeScript icon, indicating that this package has built-in type declarations

1.1.3 • Public • Published

daign-observable

CI Coverage 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

# Get a full lcov report
npm run coverage

Readme

Keywords

Package Sidebar

Install

npm i @daign/observable

Weekly Downloads

4

Version

1.1.3

License

MIT

Unpacked Size

12.5 kB

Total Files

13

Last publish

Collaborators

  • daign