Skip to content

Latest commit

 

History

History
85 lines (66 loc) · 2.05 KB

README.md

File metadata and controls

85 lines (66 loc) · 2.05 KB

slim-observer

Javascript (typescript) implementation of observer pattern on less then 100 rows. It provides just o few most common functions and minified version is smaller then 2kB.

Travis Coveralls github GitHub package version

Usage

Basic usage

// create observable subject
var observable = new Subject();

// create first listener
observable.subscribe(function(newValue){
    alert(newValue);
});

// create second listener
observable.subscribe(function(newValue){
    console.log(newValue);
});

observable.next('hello'); // emit value to all listeners
observable.next('world'); // emit another value

Get last emitted value

observable.getLast();

Create subject with initial value

var observable = new Subject('initial value');

New subscriber with all history values replay.

observable.replayAndSubscribe(function(newValue){
    console.log(newValue);
});

Unsubscribe existing listener.

var observable = new Subject();

var subscriber = observable.subscribe(function(newValue){
   alert(newValue);
});

subscriber.unsubscribe();

Emitting only unique values (if new value is different).

observable.nextUnique(123).nextUnique(123); // emits value to subscribers only once

Building tools

Build

Transpile TS code to JS in dist/ folder. Maps included.

npm run build

Unit Test

Tests framework used are: mocha & chai.

npm test

Testing with coverage:

nyc npm test

License

This project seed is licensed as Public Domain. Therefore, do whatever you want including changing the license for your needs in your project. More specifically, it was licensed as CC0 (Creative Commons 0) to further improve the freedom of a Public Domain Licence in context where it is not applicable.