More declarative style of declaring computed values
Rich-Harris opened this issue · comments
Rich Harris commented
Possibly a bit blue-sky, but this syntax would be neat:
state.compute( 'width', '<% x2 %> - <% x1 %>' );
// as opposed to
state.compute( 'width', {
triggers: [ 'x1', 'x2' ],
fn: function ( x1, x2 ) {
return x2 - x1;
}
});
Probably best to use different delimiters, to avoid confusion with templating languages.
At any rate, something like this would be very compact and fairly straightforward to a) identify dependencies, and b) compile to a function.
Rich Harris commented
Done. Example usage:
model = new Statesman({
foo: 1
});
model.compute( 'bar', '2 * ${foo}' );
model.get( 'bar' ); // 2
model.observe( 'bar', function ( newValue, oldValue ) {
console.log( 'bar changed from %s to %s', oldValue, newValue );
}, { init: false });
model.set( 'foo', 2 ); // logs 'bar changed from 2 to 4'