Switch back from Actor.current, using Dataspace.activeBehavior() instead

This commit is contained in:
Tony Garnock-Jones 2016-08-07 21:01:49 -04:00
parent eaade6e4fd
commit 3977e57b38
1 changed files with 11 additions and 17 deletions

View File

@ -31,15 +31,13 @@ function Actor(bootFn, optName) {
this.boot = function() { this.boot = function() {
var self = this; var self = this;
withCurrentFacet(self, null, function () { withCurrentFacet(null, function () {
bootFn.call(self.fields); bootFn.call(self.fields);
}); });
this.quiesce(); this.quiesce();
}; };
} }
Actor.current = null;
(function () { (function () {
var priorities = ['PRIORITY_QUERY_HIGH', var priorities = ['PRIORITY_QUERY_HIGH',
'PRIORITY_QUERY', 'PRIORITY_QUERY',
@ -70,7 +68,7 @@ Actor.prototype.handleEvent = function(e) {
throw new Error('Syndicate: pendingActions must not be nonempty at start of handleEvent'); throw new Error('Syndicate: pendingActions must not be nonempty at start of handleEvent');
} }
this.facets.forEach(function (f) { this.facets.forEach(function (f) {
withCurrentFacet(actor, f, function () { f.handleEvent(e, false); }); withCurrentFacet(f, function () { f.handleEvent(e, false); });
}); });
this.quiesce(); this.quiesce();
}; };
@ -82,12 +80,12 @@ Actor.prototype.quiesce = function() {
var entry = this.nextPendingAction(false); var entry = this.nextPendingAction(false);
if (!entry) break; if (!entry) break;
withCurrentFacet(actor, entry.facet, entry.action); withCurrentFacet(entry.facet, entry.action);
this.dataflowGraph.repairDamage(function (subjectId) { this.dataflowGraph.repairDamage(function (subjectId) {
var facet = subjectId[0]; var facet = subjectId[0];
var endpoint = subjectId[1]; var endpoint = subjectId[1];
withCurrentFacet(actor, facet, function () { withCurrentFacet(facet, function () {
// TODO: coalesce patches within a single actor // TODO: coalesce patches within a single actor
var aggregate = Patch.emptyPatch; var aggregate = Patch.emptyPatch;
var patch = Patch.retract(__).andThen(endpoint.subscriptionFn.call(facet.fields)); var patch = Patch.retract(__).andThen(endpoint.subscriptionFn.call(facet.fields));
@ -142,20 +140,16 @@ function Facet(actor) {
Facet.current = null; Facet.current = null;
function withCurrentFacet(actor, facet, f) { function withCurrentFacet(facet, f) {
var previousActor = Actor.current;
var previousFacet = Facet.current; var previousFacet = Facet.current;
Actor.current = actor;
Facet.current = facet; Facet.current = facet;
var result; var result;
try { try {
result = f(); result = f();
} catch (e) { } catch (e) {
Actor.current = previousActor;
Facet.current = previousFacet; Facet.current = previousFacet;
throw e; throw e;
} }
Actor.current = previousActor;
Facet.current = previousFacet; Facet.current = previousFacet;
return result; return result;
} }
@ -307,11 +301,11 @@ Facet.prototype.completeBuild = function() {
if (this.parent) { if (this.parent) {
this.parent.children = this.parent.children.add(this); this.parent.children = this.parent.children.add(this);
} }
withCurrentFacet(this.actor, facet, function () { withCurrentFacet(facet, function () {
facet.initBlocks.forEach(function(b) { b.call(facet.fields); }); facet.initBlocks.forEach(function(b) { b.call(facet.fields); });
}); });
var initialEvent = _Dataspace.stateChange(new Patch.Patch(facet.actor.knowledge, Trie.emptyTrie)); var initialEvent = _Dataspace.stateChange(new Patch.Patch(facet.actor.knowledge, Trie.emptyTrie));
withCurrentFacet(this.actor, facet, function () { facet.handleEvent(initialEvent, true); }); withCurrentFacet(facet, function () { facet.handleEvent(initialEvent, true); });
}; };
Facet.prototype.terminate = function() { Facet.prototype.terminate = function() {
@ -338,7 +332,7 @@ Facet.prototype.terminate = function() {
child.terminate(); child.terminate();
}); });
withCurrentFacet(this.actor, facet, function () { withCurrentFacet(facet, function () {
facet.doneBlocks.forEach(function(b) { b.call(facet.fields); }); facet.doneBlocks.forEach(function(b) { b.call(facet.fields); });
}); });
}; };
@ -355,7 +349,7 @@ function Endpoint(subscriptionFn, handlerFn) {
function referenceField(obj, prop) { function referenceField(obj, prop) {
if (!(prop in obj)) { if (!(prop in obj)) {
Actor.current.dataflowGraph.recordObservation(Immutable.List.of(obj, prop)); Dataspace.activeBehavior().dataflowGraph.recordObservation(Immutable.List.of(obj, prop));
} }
return obj[prop]; return obj[prop];
} }
@ -364,14 +358,14 @@ function declareField(obj, prop, init) {
if (prop in obj) { if (prop in obj) {
obj[prop] = init; obj[prop] = init;
} else { } else {
Actor.current.dataflowGraph.defineObservableProperty(obj, prop, init, { Dataspace.activeBehavior().dataflowGraph.defineObservableProperty(obj, prop, init, {
objectId: Immutable.List.of(obj, prop) objectId: Immutable.List.of(obj, prop)
}); });
} }
} }
function deleteField(obj, prop) { function deleteField(obj, prop) {
Actor.current.dataflowGraph.recordDamage(Immutable.List.of(obj, prop)); Dataspace.activeBehavior().dataflowGraph.recordDamage(Immutable.List.of(obj, prop));
return delete obj[prop]; return delete obj[prop];
} }