From d777418d5c80bcb34ad4b6afe336f2773946d32a Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 25 Aug 2014 18:39:45 -0700 Subject: [PATCH] Rearrange Actor instances so exceptions during construction are correctly blamed on the new actor --- src/actor.js | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/actor.js b/src/actor.js index 2d0de4d..c2a772e 100644 --- a/src/actor.js +++ b/src/actor.js @@ -5,16 +5,22 @@ var Route = Minimart.Route; Actor._chunks = null; -function Actor(ctor) { - var oldChunks = Actor._chunks; - try { +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); - } catch (e) { + bootfn.call(this); + finalizeActor(this, Actor._chunks); + Actor._chunks = oldChunks; + } catch (e) { Actor._chunks = oldChunks; throw e; + } } + }; } function checkChunks(type) { @@ -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) {