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.
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
Transpile TS code to JS in dist/
folder. Maps included.
npm run build
Tests framework used are: mocha & chai.
npm test
Testing with coverage:
nyc npm test
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.