Initialize Actor presence/name/added/removed state variables

This commit is contained in:
Tony Garnock-Jones 2014-07-28 17:43:08 -07:00
parent 9965ce760e
commit e0def26f6c
1 changed files with 29 additions and 21 deletions

View File

@ -23,7 +23,7 @@ function checkChunks(type) {
} }
} }
function extractChunk(type, defaultOptions, args) { function extractChunk(type, kind, defaultOptions, args) {
var rawProjectionFn = args[0] var rawProjectionFn = args[0]
var options = null; var options = null;
var handler = null; var handler = null;
@ -54,6 +54,7 @@ function extractChunk(type, defaultOptions, args) {
} }
return { return {
type: type, type: type,
kind: kind,
rawProjectionFn: rawProjectionFn, rawProjectionFn: rawProjectionFn,
options: options, options: options,
handler: handler handler: handler
@ -64,16 +65,17 @@ function recordChunk(chunk) {
Actor._chunks.push(chunk); Actor._chunks.push(chunk);
} }
function chunkExtractor(type, defaultOptions) { function chunkExtractor(type, kind, defaultOptions) {
return function (/* ... */) { return function (/* ... */) {
checkChunks(type); checkChunks(type);
recordChunk(extractChunk(type, recordChunk(extractChunk(type,
kind,
defaultOptions, defaultOptions,
Array.prototype.slice.call(arguments))); Array.prototype.slice.call(arguments)));
}; };
} }
var participatorDefaults = { var participantDefaults = {
metaLevel: 0, metaLevel: 0,
when: function () { return true; } when: function () { return true; }
}; };
@ -89,16 +91,17 @@ var observerDefaults = {
removed: null removed: null
}; };
Actor.advertise = chunkExtractor('advertise', participatorDefaults); Actor.advertise = chunkExtractor('advertise', 'participant', participantDefaults);
Actor.subscribe = chunkExtractor('subscribe', participatorDefaults); Actor.subscribe = chunkExtractor('subscribe', 'participant', participantDefaults);
Actor.observeAdvertisers = chunkExtractor('observeAdvertisers', observerDefaults); Actor.observeAdvertisers = chunkExtractor('observeAdvertisers', 'observer', observerDefaults);
Actor.observeSubscribers = chunkExtractor('observeSubscribers', observerDefaults); Actor.observeSubscribers = chunkExtractor('observeSubscribers', 'observer', observerDefaults);
Actor.observeGestalt = function (gestaltFn, eventHandlerFn) { Actor.observeGestalt = function (gestaltFn, eventHandlerFn) {
checkChunks('observeGestalt'); checkChunks('observeGestalt');
recordChunk({ recordChunk({
type: 'observeGestalt', type: 'observeGestalt',
kind: 'raw',
gestaltFn: gestaltFn, gestaltFn: gestaltFn,
options: { options: {
when: function () { return true; } when: function () { return true; }
@ -116,6 +119,15 @@ function finalizeActor(behavior, chunks) {
behavior.boot = function () { behavior.boot = function () {
if (oldBoot) { oldBoot.call(this); } 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.added) { this[chunk.options.added] = []; }
if (chunk.options.removed) { this[chunk.options.removed] = []; }
}
}
this.updateRoutes(); this.updateRoutes();
}; };
@ -124,12 +136,11 @@ function finalizeActor(behavior, chunks) {
for (var i = 0; i < chunks.length; i++) { for (var i = 0; i < chunks.length; i++) {
var chunk = chunks[i]; var chunk = chunks[i];
if (chunk.options.when.call(this)) { if (chunk.options.when.call(this)) {
switch (chunk.type) { switch (chunk.kind) {
case 'observeGestalt': case 'raw':
newRoutes = newRoutes.union(chunk.gestaltFn.call(this)); newRoutes = newRoutes.union(chunk.gestaltFn.call(this));
break; break;
case 'advertise': // fall through case 'participant':
case 'subscribe':
var proj = chunk.rawProjectionFn.call(this); var proj = chunk.rawProjectionFn.call(this);
projections[i] = proj; projections[i] = proj;
var g = Route.simpleGestalt(chunk.type === 'advertise', var g = Route.simpleGestalt(chunk.type === 'advertise',
@ -138,8 +149,7 @@ function finalizeActor(behavior, chunks) {
0); 0);
newRoutes = newRoutes.union(g); newRoutes = newRoutes.union(g);
break; break;
case 'observeSubscribers': // fall through case 'observer':
case 'observeAdvertisers':
var proj = chunk.rawProjectionFn.call(this); var proj = chunk.rawProjectionFn.call(this);
projections[i] = proj; projections[i] = proj;
compiledProjections[i] = Route.compileProjection(proj); compiledProjections[i] = Route.compileProjection(proj);
@ -153,7 +163,7 @@ function finalizeActor(behavior, chunks) {
} }
break; break;
default: default:
throw new Error("Unsupported chunk type: "+chunk.type); throw new Error("Unsupported chunk type/kind: "+chunk.type+"/"+chunk.kind);
} }
} }
} }
@ -164,12 +174,11 @@ function finalizeActor(behavior, chunks) {
if (oldHandleEvent) { oldHandleEvent.call(this, e); } if (oldHandleEvent) { oldHandleEvent.call(this, e); }
for (var i = 0; i < chunks.length; i++) { for (var i = 0; i < chunks.length; i++) {
var chunk = chunks[i]; var chunk = chunks[i];
switch (chunk.type) { switch (chunk.kind) {
case 'observeGestalt': case 'raw':
chunk.eventHandlerFn.call(this, e); chunk.eventHandlerFn.call(this, e);
break; break;
case 'advertise': // fall through case 'participant':
case 'subscribe':
if (chunk.handler if (chunk.handler
&& (e.type === 'message') && (e.type === 'message')
&& (e.isFeedback === (chunk.type === 'advertise'))) && (e.isFeedback === (chunk.type === 'advertise')))
@ -180,8 +189,7 @@ function finalizeActor(behavior, chunks) {
} }
} }
break; break;
case 'observeSubscribers': // fall through case 'observer':
case 'observeAdvertisers':
if (e.type === 'routes') { if (e.type === 'routes') {
var projectionResult = e.gestalt.project(compiledProjections[i], var projectionResult = e.gestalt.project(compiledProjections[i],
chunk.type !== 'observeSubscribers', chunk.type !== 'observeSubscribers',
@ -234,7 +242,7 @@ function finalizeActor(behavior, chunks) {
} }
break; break;
default: default:
throw new Error("Unsupported chunk type: "+chunk.type); throw new Error("Unsupported chunk type/kind: "+chunk.type+"/"+chunk.kind);
} }
} }
}; };