reactive-value
v0.1.0
Published
Reactive value interface
Downloads
50
Maintainers
Readme
reactive-value
Object representation of mutable value
Usage
var ObservableValue = require('reactive-value');
var observable = new ObservableValue('foo');
Current value is accessible at value
property:
observable.value; // 'foo'
change
events are emitted on any change
var emitted;
observable.on('change', function (event) { emitted = event; });
obj.value = 'bar';
console.log(emitted); // { newValue: 'bar', oldValue: 'foo' };
Installation
$ npm install reactive-value
To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack
Additional utilities
isObservableValue (reactive-value/is-reactive-value)
Whether object shares ObservableValue interface
var isObservableValue = require('reactive-value/is-reactive-value');
console.log(isObservableValue({})); // false
console.log(isObservableValue(new ObservableValue())); // true
eq
Create observable value out of two different values that may share ObservableValue but don't have to. If both values do not represent ObservableValue, plain boolean value is returned
var eq = require('reactive-value/eq');
console.log(isObservableValue(eq('foo', 'bar'))); // false
console.log(eq('foo', 'bar')); // false
console.log(eq('foo', 'foo')); // true
var m1 = new ObservableValue();
var em = eq(m1, 'foo');
console.log(isObservableValue(em)); // true
console.log(em.value); // false
em.on('change', function (value) { emitted = value; });
m1.value = 'foo';
console.log(emitted); // true
console.log(em.value); // true
m1.value = 'bar';
console.log(emitted); // false
console.log(em.value); // false
emitted = null;
m1.value = 'other';
console.log(emitted); // null
console.log(em.value); // false
var m2 = new ObservableValue();
m2.value = 'other';
em = eq(m1, m2);
console.log(em.value); // true
m2.value = 'foo';
console.log(em.value); // false
m1.value = 'foo';
console.log(em.value); // true
Tests
$ npm test