observers
v0.5.2
Published
A simple implentation of JavaScript observables
Downloads
9
Maintainers
Readme
Observers
A light implementation of JavaScript observables
Installation
npm install observers --save
Sample
import obs = require('observers');
var obj = obs.observe({ key: 'value' });
assert(obj().key === 'value');
obj.subscribe(val => assert(val.key === 'newvalue'));
obj({ key: 'newvalue' });
var array = obs.observeArray(['a', 'b', 'c']);
assert(array.join('') === 'abc');
array.push('d');
array.find(v => v === 'a'); // 'a'
array.findIndex(v => v === 'd'); // 3
array.filter(v => v < 'c'); // '['a', 'b']
array.map(v => v === 'd' ? 'e' : v); // ['a', 'b', 'c', 'e']
array.remove(v => v > 'c'); // ['a', 'b', 'c']
// ...
API
observe
function <T>observe(value?: T): Observable<T>;
// Example
var anObservable = obs.observe(5);
var another = obs.observe(5);
another(7);
observeArray
function <T>observeArray(values?: Array<T>): ObservableArray<T>;
// Example
var obsArray = obs.observeArray([1,2,3,4,5,6]);
computed
Computed observables that depend on other observables (object
, array
or computed
) will re-evaluate
when their dependent observables are modifed.
See the below example:
function <T>computed(evaluator: () => T): Computed<T>;
// Example (using above example values)
var comp = obs.computed(() => anObservable() + another()); // 12
comp.subscribe(value => console.log(`My computed changed to ${value}`);
another(9); // Will cause the computed to re-evaluate and notify subscribers
Shared functions
All observables (Obervable
, ObservableArray
, Computed
) have the following functions:
getter
Returns the current value of an observable with no side effects
function (): any;
// Example
var someNumber = obs.observe(12);
someNumber(); // Returns 12 with no side effects
subscribe
Takes a function that will be called with the new value of an observable
function subscribe(func: (newValue: T) => void): void;
// Example
someComputed.subscribe(newValue => $.post('/api/something', { data: newValue });
removeSubscribers
Remove all listener functions on a specific observable.
Warning: May have undesirable side effects.
function removeSubscribers();
// Example
someObservable.removeSubscribers();
Supported array functions
every
function every(predicate: (value: T) => boolean): boolean;
find
function find(predicate: (value: T) => boolean): T;
findIndex
function findIndex(predicate: (value: T) => boolean): number;
filter
function filter(predicate: (value: T) => boolean): Array<T>;
join
function join(seperator?: string): string;
map
function map(predicate: (value: T) => any): Array<any>;
pop
function pop(): T;
push
function push(value: T): number;
reverse
function reverse(): Array<T>;
reduce
function reduce(predicate: (previous: U, current: T, index: number, array: Array<T>) => U): U;
remove
Returns an array of the removed items
function remove(predicate: (value: T) => boolean): Array<T>;
removeAll
Returns an array of the removed items
function removeAll(): Array<T>;
removeSubscribers
Clears the list of subscribers
function removeSubscribers(): void;
shift
function shift(): number;
slice
function slice(start?: number, end?: number): Array<T>;
sort
function sort(comparer: (left: T, right: T) => number|boolean): Array<T>;
splice
function splice(start?: number, end?: number): Array<T>;
subscribe
Subscribes to changes to the observable
function subscribe(callback: (newValue: Array<T>) => void): void;
some
function some(predicate: (value: T) => boolean): boolean;
unshift
function unshift(...values: T[]): number;
update
Returns the newValue
if successful.
Returns undefined
if unsuccessful.
function update(predicate: (value: T) => boolean, newValue: T): T;