!function(e){if("object"==typeofexports)module.exports=e();elseif("function"==typeofdefine&&define.amd)define(e);else{varf;"undefined"!=typeofwindow?f=window:"undefined"!=typeofglobal?f=global:"undefined"!=typeofself&&(f=self),f.Minimart=e()}}(function(){vardefine,module,exports;returnfunctione(t,n,r){functions(o,u){if(!n[o]){if(!t[o]){vara=typeofrequire=="function"&&require;if(!u&&a)returna(o,!0);if(i)returni(o,!0);thrownewError("Cannot find module '"+o+"'")}varf=n[o]={exports:{}};t[o][0].call(f.exports,function(e){varn=t[o][1][e];returns(n?n:e)},f,f.exports,e,t,n,r)}returnn[o].exports}vari=typeofrequire=="function"&&require;for(varo=0;o<r.length;o++)s(r[o]);returns}({1:[function(_dereq_,module,exports){varMinimart=_dereq_("./minimart.js");varutil=_dereq_("./util.js");varWorld=Minimart.World;varRoute=Minimart.Route;Actor._chunks=null;functionActor(bootfn){return{boot:function(){deletethis.boot;varoldChunks=Actor._chunks;try{Actor._chunks=[];bootfn.call(this);varinitialGestalt=finalizeActor(this,Actor._chunks);Actor._chunks=oldChunks;return[initialGestalt]}catch(e){Actor._chunks=oldChunks;throwe}}}}functioncheckChunks(type){if(!Actor._chunks){thrownewError("Call to Actor."+type+" outside of Actor constructor")}}functionextractChunk(type,kind,defaultOptions,args){varrawProjectionFn=args[0];varoptions=null;varhandler=null;if(typeofrawProjectionFn!=="function"){thrownewError("Actor."+type+" expects a function producing a pattern as first argument")}for(vari=1;i<args.length;i++){if(typeofargs[i]==="function"){if(handler!==null){thrownewError("Too many handler functions in Actor."+type)}handler=args[i]}elseif(typeofargs[i]==="object"){if(options!==null){thrownewError("Too many options arguments in Actor."+type)}options=args[i]}else{thrownewError("Unrecognised argument in Actor."+type)}}options=options||{};for(varkinoptions){if(!(kindefaultOptions)){thrownewError("Unrecognised option '"+k+"' in Actor."+type)}}for(varkindefaultOptions){if(!(kinoptions)){options[k]=defaultOptions[k]}}return{type:type,kind:kind,rawProjectionFn:rawProjectionFn,options:options,handler:handler}}functionrecordChunk(chunk){Actor._chunks.push(chunk)}functionchunkExtractor(type,kind,defaultOptions){returnfunction(){checkChunks(type);recordChunk(extractChunk(type,kind,defaultOptions,Array.prototype.slice.call(arguments)))}}varparticipantDefaults={metaLevel:0,when:function(){returntrue}};varobserverDefaults={metaLevel:0,level:0,when:function(){returntrue},presence:null,name:null,singleton:null,set:null,added:null,removed:null};Actor.advertise=chunkExtractor("advertise","participant",participantDefaults);Actor.subscribe=chunkExtractor("subscribe","participant",participantDefaults);Actor.observeAdvertisers=chunkExtractor("observeAdvertisers","observer",observerDefaults);Actor.observeSubscribers=chunkExtractor("observeSubscribers","observer",observerDefaults);Actor.observeGestalt=function(gestaltFn,eventHandlerFn){checkChunks("observeGestalt");recordChunk({type:"observeGestalt",kind:"raw",gestaltFn:gestaltFn,options:{when:function(){returntrue}},eventHandlerFn:eventHandlerFn})};functionfinalizeActor(behavior,chunks){varoldHandleEvent=behavior.handleEvent;varprojections={};varcompiledProjections={};varpreviousObjs={};behavior._computeRoutes=function(){varnewRoutes=Route.emptyGestalt;for(vari=0;i<chunks.length;i++){varchunk=chunks[i];if(chunk.options.when.call(this)){switch(chunk.kind){case"raw":newRoutes=newRoutes.union(chunk.gestaltFn.call(this));break;case"participant":varproj=chunk.rawProjectionFn.call(this);projections[i]=proj;varg=Route.simpleGestalt(chunk.type==="advertise",Route.projectionToPattern(proj),chunk.options.metaLevel,0);newRoutes=newRoutes.union(g);break;case"observer":varproj=chunk.rawProjectionFn.call(this);projections[i]=proj;compiledProjections[i]=Route.compileProjection(proj);varg=Route.simpleGestalt(chunk.type==="observeSubscribers",Route.projectionToPattern(proj),chunk.options.metaLevel,chunk.options.level+1);newRoutes=newRoutes.union(g);if(chunk.op
}};WebSocketConnection.prototype.handleEvent=function(e){switch(e.type){case"routes":varnLevels=e.gestalt.levelCount(0);varrelayGestalt=Route.fullGestalt(1,nLevels).label(World.activePid());varg=e.gestalt.erasePath(relayGestalt);this.localGestalt=Route.emptyGestalt;for(varlevel=0;level<nLevels;level++){this.collectMatchers(false,level,g);this.collectMatchers(true,level,g)}this.sendLocalRoutes();break;case"message":varm=e.message;if(m.length&&m.length===3&&m[0]===this.label){varencoded=JSON.stringify(Codec.encodeEvent(Minimart.sendMessage(m[2],m[1],e.isFeedback)));if(this.deduplicator.accept(encoded)){this.safeSend(encoded)}}break}};WebSocketConnection.prototype.forceclose=function(keepReconnectDelay){if(!keepReconnectDelay){this.reconnectDelay=DEFAULT_RECONNECT_DELAY}this.clearHeartbeatTimers();if(this.sock){console.log("WebSocketConnection.forceclose called");this.sock.close();this.sock=null}};WebSocketConnection.prototype.reconnect=function(){varself=this;this.forceclose(true);this.connectionCount++;this.sock=newWebSocket(this.wsurl);this.sock.onopen=World.wrap(function(e){returnself.onopen(e)});this.sock.onmessage=World.wrap(function(e){self.receiveCount++;returnself.onmessage(e)});this.sock.onclose=World.wrap(function(e){returnself.onclose(e)})};WebSocketConnection.prototype.onopen=function(e){console.log("connected to "+this.sock.url);this.reconnectDelay=DEFAULT_RECONNECT_DELAY;this.prevLocalRoutesMessage=null;this.sendLocalRoutes()};WebSocketConnection.prototype.onmessage=function(wse){this.recordActivity();varj=JSON.parse(wse.data);if(j==="ping"){this.safeSend(JSON.stringify("pong"));return}elseif(j==="pong"){return}vare=Codec.decodeAction(j);switch(e.type){case"routes":if(this.prevPeerRoutesMessage!==wse.data){this.prevPeerRoutesMessage=wse.data;this.peerGestalt=e.gestalt;World.updateRoutes([this.aggregateGestalt()])}break;case"message":if(this.deduplicator.accept(wse.data)){World.send([this.label,e.metaLevel,e.message],0,e.isFeedback)}break}};WebSocketConnection.prototype.onclose=function(e){varself=this;console.log("onclose",e);World.updateRoutes([this.aggregateGestalt()]);if(this.shouldReconnect){console.log("reconnecting to "+this.wsurl+" in "+this.reconnectDelay+"ms");setTimeout(World.wrap(function(){self.reconnect()}),this.reconnectDelay);this.reconnectDelay=this.reconnectDelay*1.618+Math.random()*1e3;this.reconnectDelay=this.reconnectDelay>MAX_RECONNECT_DELAY?MAX_RECONNECT_DELAY+Math.random()*1e3:this.reconnectDelay}};module.exports.WebSocketConnection=WebSocketConnection},{"./codec.js":2,"./minimart.js":7}],15:[function(_dereq_,module,exports){varGround=_dereq_("./ground.js").Ground;varUtil=_dereq_("./util.js");varCodec=_dereq_("./codec.js");varMinimart=_dereq_("./minimart.js");varWorld=Minimart.World;varsub=Minimart.sub;varpub=Minimart.pub;var__=Minimart.__;varBuiltinWorker=typeofwindow!=="undefined"&&window.Worker;functionWorker(scriptUrl){this.scriptUrl=scriptUrl;this.w=newBuiltinWorker(scriptUrl)}Worker.prototype.boot=function(){this.w.onmessage=World.wrap(function(e){console.log("Received from worker",JSON.stringify(e.data));World.current().enqueueAction(World.activePid(),Codec.decodeAction(e.data))})};Worker.prototype.handleEvent=function(e){console.log("Sending to worker",JSON.stringify(Codec.encodeEvent(e)));this.w.postMessage(Codec.encodeEvent(e))};functionWorkerGround(bootFn){varself=this;Ground.call(this,bootFn);onmessage=function(e){console.log("Received from main page",JSON.stringify(e.data));self.world.handleEvent(Codec.decodeEvent(e.data));self.startStepping()}}WorkerGround.prototype=Util.extend({},Ground.prototype);WorkerGround.prototype.enqueueAction=function(pid,action){console.log("Sending to main page",JSON.stringify(Codec.encodeAction(action)));postMessage(Codec.encodeAction(action));console.log("Sent to main page")};module.exports.Worker=Worker;module.exports.WorkerGround=WorkerGround},{"./codec.js":2,"./ground.js":4,"./minimart.js":7,"./util.js":12}]},{},[6])(6)});