jQuery driver

This commit is contained in:
Tony Garnock-Jones 2013-10-17 16:38:47 +01:00
parent 5eb836ad32
commit 5265f42beb
4 changed files with 86 additions and 8 deletions

View File

@ -7,6 +7,7 @@
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet">
<!-- <script src="bootstrap/js/bootstrap.min.js"></script> -->
<script src="jquery-2.0.3.min.js"></script>
<script src="marketplace.js"></script>
<script src="index.js"></script>
@ -19,6 +20,9 @@
<p>
Hello, world.
</p>
<p>
<button id="testButton">Test button</button>
</p>
</div>
</div>
</div>

View File

@ -1,15 +1,76 @@
function Spy() {
}
Spy.prototype.boot = function () {
World.updateRoutes([sub(__, 0, 1000), pub(__, 0, 1000)]);
};
Spy.prototype.handleEvent = function (e) {
console.log("SPY", e);
};
function JQueryDriver() {
this.handlerMap = {};
}
JQueryDriver.prototype.updateHandlerMap = function (routes) {
var newMap = {};
for (var i = 0; i < routes.length; i++) {
var selector = routes[i].pattern[1];
var eventName = routes[i].pattern[2];
if (typeof(selector) === 'string' && typeof(eventName) === 'string') {
var key = JSON.stringify([selector, eventName]);
var handler = this.handlerMap[key];
if (!handler) {
handler = (function (selector, eventName) { // JS is broken
return World.wrap(function (e) {
World.send(["jQuery", selector, eventName, e]);
World.current.startStepping();
});
})(selector, eventName);
console.log("jQuery", "installing", selector, eventName);
$(selector).on(eventName, handler);
}
newMap[key] = handler;
}
}
for (var key in this.handlerMap) {
if (hasOwnProperty(this.handlerMap, key) && !hasOwnProperty(newMap, key)) {
var keyArray = JSON.parse(key);
var handler = this.handlerMap[key];
var selector = keyArray[0];
var eventName = keyArray[1];
console.log("jQuery", "removing", selector, eventName);
$(selector).off(eventName, handler);
}
}
this.handlerMap = newMap;
};
JQueryDriver.prototype.boot = function () {
World.updateRoutes([pub(["jQuery", __, __, __], 0, 1)]);
};
JQueryDriver.prototype.handleEvent = function (e) {
if (e.type === "routes") {
this.updateHandlerMap(e.routes);
}
};
var g = new Ground(function () {
console.log('here');
World.spawn(new Spy());
World.spawn(new JQueryDriver());
World.spawn({
step: function () { console.log('step'); },
step: function () { console.log('dummy step'); },
boot: function () {
console.log('boot');
World.updateRoutes([sub(__), sub(__, 1)]);
console.log('dummy boot');
World.updateRoutes([sub(["jQuery", "#testButton", "click", __]), sub(__, 1)]);
World.send({msg: 'hello outer world'}, 1);
World.send({msg: 'hello inner world'}, 0);
},
handleEvent: function (e) {
console.log('handleEvent: ' + JSON.stringify(e));
console.log('dummy handleEvent', e);
}
});
});

6
jquery-2.0.3.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -45,6 +45,7 @@ function unify1(a, b) {
function unify(a, b) {
try {
// console.log("unify", JSON.stringify(a), JSON.stringify(b));
return {result: unify1(a, b)};
} catch (e) {
if (e.unificationFailed) return undefined;
@ -145,7 +146,7 @@ function filterEvent(e, routes) {
}
}
}
return result.length ? result : null;
return result.length ? updateRoutes(result) : null;
case "send":
for (var i = 0; i < routes.length; i++) {
var r = routes[i];
@ -262,7 +263,11 @@ World.prototype.wrap = function (f) {
};
World.prototype.kill = function (pid, exn) {
console.log("Killed process " + pid + (exn ? " with reason " + exn.message : ""));
if (exn && exn.stack) {
console.log("Killed process", pid, exn, exn.stack);
} else {
console.log("Killed process", pid);
}
delete this.processTable[pid];
this.issueRoutingUpdate();
};
@ -342,6 +347,7 @@ World.prototype.dispatchEvent = function (e) {
for (var pid in this.processTable) {
var p = this.processTable[pid];
var e1 = filterEvent(e, p.routes);
// console.log("filtering", e, p.routes, e1);
if (e1) { this.asChild(pid, function () { p.behavior.handleEvent(e1) }); }
}
};
@ -401,8 +407,9 @@ Ground.prototype.stopStepping = World.prototype.stopStepping;
Ground.prototype.enqueueAction = function (action) {
if (action.type === 'routes') {
// Ignore it. These are generated outside user control so it
// shouldn't be an error.
if (action.routes.length > 0) {
console.error("You have subscribed to a nonexistent event source.", action);
}
} else {
console.error("You have sent a message into the outer void.", action);
}