@daign/observable
v1.1.3
Published
Simple implementation of observable pattern in Typescript
Downloads
1,263
Readme
daign-observable
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