57 lines
1.7 KiB
JavaScript
57 lines
1.7 KiB
JavaScript
/* Web Worker interface */
|
|
var Ground = require("./ground.js").Ground;
|
|
var Util = require("./util.js");
|
|
var Codec = require("./codec.js");
|
|
|
|
var Minimart = require("./minimart.js");
|
|
var World = Minimart.World;
|
|
var sub = Minimart.sub;
|
|
var pub = Minimart.pub;
|
|
var __ = Minimart.__;
|
|
|
|
var BuiltinWorker = typeof window !== 'undefined' && window.Worker;
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
function Worker(scriptUrl) {
|
|
this.scriptUrl = scriptUrl;
|
|
this.w = new BuiltinWorker(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));
|
|
};
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
function WorkerGround(bootFn) {
|
|
var self = 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;
|