Rearrange Actor instances so exceptions during construction are correctly blamed on the new actor
This commit is contained in:
parent
160938cec8
commit
d777418d5c
41
src/actor.js
41
src/actor.js
|
@ -5,17 +5,23 @@ var Route = Minimart.Route;
|
|||
|
||||
Actor._chunks = null;
|
||||
|
||||
function Actor(ctor) {
|
||||
function Actor(bootfn) {
|
||||
return {
|
||||
boot: function () {
|
||||
delete this.boot;
|
||||
var oldChunks = Actor._chunks;
|
||||
try {
|
||||
Actor._chunks = [];
|
||||
var behavior = new ctor();
|
||||
return finalizeActor(behavior, Actor._chunks);
|
||||
bootfn.call(this);
|
||||
finalizeActor(this, Actor._chunks);
|
||||
Actor._chunks = oldChunks;
|
||||
} catch (e) {
|
||||
Actor._chunks = oldChunks;
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function checkChunks(type) {
|
||||
if (!Actor._chunks) {
|
||||
|
@ -112,27 +118,11 @@ Actor.observeGestalt = function (gestaltFn, eventHandlerFn) {
|
|||
};
|
||||
|
||||
function finalizeActor(behavior, chunks) {
|
||||
var oldBoot = behavior.boot;
|
||||
var oldHandleEvent = behavior.handleEvent;
|
||||
var projections = {};
|
||||
var compiledProjections = {};
|
||||
var previousObjs = {};
|
||||
|
||||
behavior.boot = function () {
|
||||
if (oldBoot) { oldBoot.call(this); }
|
||||
for (var i = 0; i < chunks.length; i++) {
|
||||
var chunk = chunks[i];
|
||||
if (chunk.kind === 'observer') {
|
||||
if (chunk.options.presence) { this[chunk.options.presence] = false; }
|
||||
if (chunk.options.name) { this[chunk.options.name] = []; }
|
||||
if (chunk.options.singleton) { this[chunk.options.singleton] = undefined; }
|
||||
if (chunk.options.added) { this[chunk.options.added] = []; }
|
||||
if (chunk.options.removed) { this[chunk.options.removed] = []; }
|
||||
}
|
||||
}
|
||||
this.updateRoutes();
|
||||
};
|
||||
|
||||
behavior.updateRoutes = function () {
|
||||
var newRoutes = Route.emptyGestalt;
|
||||
for (var i = 0; i < chunks.length; i++) {
|
||||
|
@ -253,7 +243,18 @@ function finalizeActor(behavior, chunks) {
|
|||
}
|
||||
};
|
||||
|
||||
return behavior;
|
||||
if (behavior.boot) { behavior.boot(); }
|
||||
for (var i = 0; i < chunks.length; i++) {
|
||||
var chunk = chunks[i];
|
||||
if (chunk.kind === 'observer') {
|
||||
if (chunk.options.presence) { behavior[chunk.options.presence] = false; }
|
||||
if (chunk.options.name) { behavior[chunk.options.name] = []; }
|
||||
if (chunk.options.singleton) { behavior[chunk.options.singleton] = undefined; }
|
||||
if (chunk.options.added) { behavior[chunk.options.added] = []; }
|
||||
if (chunk.options.removed) { behavior[chunk.options.removed] = []; }
|
||||
}
|
||||
}
|
||||
behavior.updateRoutes();
|
||||
}
|
||||
|
||||
function kwApply(f, thisArg, args) {
|
||||
|
|
Loading…
Reference in New Issue