@@ -29,7 +29,7 @@ Elm.worker = function(module) {
29 return init(ElmRuntime.Display.NONE, {}, module);
29 return init(ElmRuntime.Display.NONE, {}, module);
32−function init(display, container, module) {
32+function init(display, container, module, moduleToReplace) {
33 // defining state needed for an instance of the Elm RTS
33 // defining state needed for an instance of the Elm RTS
34 var inputs = [];
34 var inputs = [];
@@ -74,18 +74,38 @@ function init(display, container, module) {
74 if (e.value.length > 0) { window.location = e.value; }
74 if (e.value.length > 0) { window.location = e.value; }
77+ function swap(newModule) {
78+ var div = document.createElement('div');
79+ if (container.id) { div.id = container.id; }
80+ var newElm = init(display, div, newModule, elm);
81+ // elm.send = newElm.send;
82+ // elm.recv = newElm.recv;
83+ // elm.swap = newElm.swap;
77 var Module = module(elm);
87 var Module = module(elm);
78 inputs = ElmRuntime.filterDeadInputs(inputs);
88 inputs = ElmRuntime.filterDeadInputs(inputs);
89+ if (display !== ElmRuntime.Display.NONE) {
90+ var graphicsNode = initGraphics(elm, Module);
92+ if (typeof moduleToReplace !== 'undefined') {
93+ ElmRuntime.swap(moduleToReplace, elm);
95+ // rerender scene if graphics are enabled.
96+ if (typeof graphicsNode !== 'undefined') {
97+ graphicsNode.value = A2( Elm.Graphics.Element(elm).spacer, 0, 0 );
98+ graphicsNode.recv(0, true, 0);
80− // If graphics are not enabled, escape early, skip over setting up DOM stuff.
81− if (display !== ElmRuntime.Display.NONE) { initGraphics(elm, Module); }
83− return { send:send, recv:recv,
102+ return { send:send, recv:recv, swap:swap };
84− __private__ : { inputs:inputs, container:container } };
87function initGraphics(elm, Module) {
105function initGraphics(elm, Module) {
106+ if (!('main' in Module))
88− if (!('main' in Module)) throw new Error("'main' is missing! What do I display?!");
107+ throw new Error("'main' is missing! What do I display?!");
89 var signalGraph = Module.main;
109 var signalGraph = Module.main;
91 // make sure the signal graph is actually a signal & extract the visual model
111 // make sure the signal graph is actually a signal & extract the visual model
@@ -101,15 +121,14 @@ function initGraphics(elm, Module) {
101 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
121 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
103 // set up updates so that the DOM is adjusted as necessary.
123 // set up updates so that the DOM is adjusted as necessary.
104− function domUpdate(newScene) {
124+ function domUpdate(newScene, currentScene) {
105 ElmRuntime.draw(function(_) {
125 ElmRuntime.draw(function(_) {
106 Render.update(elm.node.firstChild, currentScene, newScene);
126 Render.update(elm.node.firstChild, currentScene, newScene);
107− currentScene = newScene;
108 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
127 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
110 return newScene;
129 return newScene;
112− A2(Signal.lift, domUpdate, signalGraph);
131+ return A3(Signal.foldp, F2(domUpdate), currentScene, signalGraph);