on master help sign in
Move hot-swap.js code into core.js
commit4202ce22bbc17fdf3f9bfb12c50c34bdde4f21c7 authorEvan Czaplicki <info@elm-lang.org> dateTue 4 Nov 2014, 14:57 (12 years ago) parent9c61b17
2 files changed+46−43
runtime/core.js+46−1 runtime/hot-swap.js−42
runtime/core.js+46−1
@@ -136,7 +136,7 @@ function init(display, container, module, ports, moduleToReplace) {
136136 var graphicsNode = initGraphics(elm, Module);
137137 }
138138 if (typeof moduleToReplace !== 'undefined') {
139− ElmRuntime.swap(moduleToReplace, elm);
139+ hotSwap(moduleToReplace, elm);
140140
141141 // rerender scene if graphics are enabled.
142142 if (typeof graphicsNode !== 'undefined') {
@@ -330,4 +330,49 @@ if (win.requestAnimationFrame && win.cancelAnimationFrame) {
330330 };
331331}
332332
333+
334+//// HOT SWAPPING ////
335+
336+// Returns boolean indicating if the swap was successful.
337+// Requires that the two signal graphs have exactly the same
338+// structure.
339+function hotSwap(from, to) {
340+ function similar(nodeOld,nodeNew) {
341+ var idOkay = nodeOld.id === nodeNew.id;
342+ var lengthOkay = nodeOld.kids.length === nodeNew.kids.length;
343+ return idOkay && lengthOkay;
344+ }
345+ function swap(nodeOld,nodeNew) {
346+ nodeNew.value = nodeOld.value;
347+ return true;
348+ }
349+ var canSwap = depthFirstTraversals(similar, from.inputs, to.inputs);
350+ if (canSwap) {
351+ depthFirstTraversals(swap, from.inputs, to.inputs);
352+ }
353+ from.node.parentNode.replaceChild(to.node, from.node);
354+
355+ return canSwap;
356+}
357+
358+// Returns false if the node operation f ever fails.
359+function depthFirstTraversals(f, queueOld, queueNew) {
360+ if (queueOld.length !== queueNew.length) return false;
361+ queueOld = queueOld.slice(0);
362+ queueNew = queueNew.slice(0);
363+
364+ var seen = [];
365+ while (queueOld.length > 0 && queueNew.length > 0) {
366+ var nodeOld = queueOld.pop();
367+ var nodeNew = queueNew.pop();
368+ if (seen.indexOf(nodeOld.id) < 0) {
369+ if (!f(nodeOld, nodeNew)) return false;
370+ queueOld = queueOld.concat(nodeOld.kids);
371+ queueNew = queueNew.concat(nodeNew.kids);
372+ seen.push(nodeOld.id);
373+ }
374+ }
375+ return true;
376+}
377+
333378}());
runtime/hot-swap.jsdeleted−42
@@ -1,42 +0,0 @@
1−(function() {
2−
3−// Returns boolean indicating if the swap was successful.
4−// Requires that the two signal graphs have exactly the same
5−// structure.
6−ElmRuntime.swap = function(from, to) {
7− function similar(nodeOld,nodeNew) {
8− var idOkay = nodeOld.id === nodeNew.id;
9− var lengthOkay = nodeOld.kids.length === nodeNew.kids.length;
10− return idOkay && lengthOkay;
11− }
12− function swap(nodeOld,nodeNew) {
13− nodeNew.value = nodeOld.value;
14− return true;
15− }
16− var canSwap = depthFirstTraversals(similar, from.inputs, to.inputs);
17− if (canSwap) { depthFirstTraversals(swap, from.inputs, to.inputs); }
18− from.node.parentNode.replaceChild(to.node, from.node);
19− return canSwap;
20−}
21−
22−// Returns false if the node operation f ever fails.
23−function depthFirstTraversals(f, queueOld, queueNew) {
24− if (queueOld.length !== queueNew.length) return false;
25− queueOld = queueOld.slice(0);
26− queueNew = queueNew.slice(0);
27−
28− var seen = [];
29− while (queueOld.length > 0 && queueNew.length > 0) {
30− var nodeOld = queueOld.pop();
31− var nodeNew = queueNew.pop();
32− if (seen.indexOf(nodeOld.id) < 0) {
33− if (!f(nodeOld, nodeNew)) return false;
34− queueOld = queueOld.concat(nodeOld.kids);
35− queueNew = queueNew.concat(nodeNew.kids);
36− seen.push(nodeOld.id);
37− }
38− }
39− return true;
40−}
41−
42−}());