kefirjs/kefir
View on GitHubDocumentation request: toProperty or just Properties and activation
Open
#125 opened on Jul 16, 2015
docshelp wanted
Description
At first I thought this was a bug, then an inconsistency, now I realize it's not perfectly clear of this behavior:
var pool = Kefir.pool();
var prop = pool.toProperty();
pool.plug(Kefir.constant(0));
prop.onValue(function(x){ console.log('Property: ', x); });
// Output:
// Property: 0
Registering an onValue to the pool changes this result:
var pool = Kefir.pool();
var prop = pool.toProperty();
pool.onValue(function(x){ console.log('Pool: ', x); });
pool.plug(Kefir.constant(0));
prop.onValue(function(x){ console.log('Property: ', x); });
// Output:
// Pool: 0
To get the expected output:
var pool = Kefir.pool();
var prop = pool.toProperty().onValue(function(){}); // <-- noop to force activation
pool.onValue(function(x){ console.log('Pool: ', x); });
pool.plug(Kefir.constant(0));
prop.onValue(function(x){ console.log('Property: ', x); });
// Output:
// Pool: 0
// Property: 0
In the first example, the property gets activated first so it sees the pool's plug value.
In the second example, the pool gets activated before the property does, so the pool triggers but the property does not. Another plug to the pool triggers both.
In the third example, the property is forced active. The other way is to use the "default value" callback. An example like this in the docs would be very helpful in understanding how lazy properties are.
Relevant issue: #43