Fix bug where `this` was referring to the fields of a parent facet, not the facet currently being constructed

This commit is contained in:
Tony Garnock-Jones 2016-12-06 05:53:06 +13:00
parent 26d4a75318
commit 51a28b9349
3 changed files with 64 additions and 3 deletions

View File

@ -55,9 +55,9 @@ function buildActor(nameExpOpt, block, withReact) {
}
function reactWrap(blockCode) {
return '{ Syndicate.Actor.Facet.build((function () { ' +
return '{ Syndicate.Actor.Facet.build(function () { ' +
blockCode +
' }).bind(this)); }';
' }); }';
}
function buildOnEvent(isTerminal, eventType, subscription, projection, bindings, body) {

View File

@ -0,0 +1,59 @@
// bin/syndicatec compiler/demo-bad-this.js | node
//
// Bug with this-ness. Symptomatic output:
//
// + render one false
// + render two false
// present one
// - render one false
// - render two false
// + render one one
// + render two one
//
// Good output:
//
// + render one false
// + render two false
// present one
// - render one false
// + render one one
var Syndicate = require('./src/main.js');
assertion type user(who);
assertion type present(who);
assertion type rendered(who, isPresent);
ground dataspace {
actor {
assert user('one');
assert present('one');
}
actor {
assert user('two');
// assert present('two');
}
actor {
during user($who) {
field this.isPresent = false;
on asserted present(who) {
console.log('present', who);
this.isPresent = who;
}
on retracted present(who) {
console.log('absent', who);
this.isPresent = false;
}
assert rendered(who, this.isPresent);
}
}
actor {
during rendered($who, $isPresent) {
on start { console.log('+ render', who, isPresent); }
on stop { console.log('- render', who, isPresent); }
}
}
}

View File

@ -139,7 +139,9 @@ Facet.current = null;
Facet.build = function(f) {
var facet = new Facet(Dataspace.activeBehavior());
withCurrentFacet(facet, f);
withCurrentFacet(facet, function () {
f.call(facet.fields);
});
facet.completeBuild();
};