@@ -29,7 +29,7 @@ Elm.worker = function(module) {
2929 return init(ElmRuntime.Display.NONE, {}, module);
3030};
3131
32−function init(display, container, module) {
32+function init(display, container, module, moduleToReplace) {
3333 // defining state needed for an instance of the Elm RTS
3434 var inputs = [];
3535
@@ -74,18 +74,38 @@ function init(display, container, module) {
7474 if (e.value.length > 0) { window.location = e.value; }
7575 });
7676
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;
84+ return newElm;
85+ }
86+
7787 var Module = module(elm);
7888 inputs = ElmRuntime.filterDeadInputs(inputs);
89+ if (display !== ElmRuntime.Display.NONE) {
90+ var graphicsNode = initGraphics(elm, Module);
91+ }
92+ if (typeof moduleToReplace !== 'undefined') {
93+ ElmRuntime.swap(moduleToReplace, elm);
94+
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);
99+ }
100+ }
79101
80− // If graphics are not enabled, escape early, skip over setting up DOM stuff.
81− if (display !== ElmRuntime.Display.NONE) { initGraphics(elm, Module); }
82−
83− return { send:send, recv:recv,
84− __private__ : { inputs:inputs, container:container } };
102+ return { send:send, recv:recv, swap:swap };
85103};
86104
87105function initGraphics(elm, Module) {
88− if (!('main' in Module)) throw new Error("'main' is missing! What do I display?!");
106+ if (!('main' in Module))
107+ throw new Error("'main' is missing! What do I display?!");
108+
89109 var signalGraph = Module.main;
90110
91111 // make sure the signal graph is actually a signal & extract the visual model
@@ -101,15 +121,14 @@ function initGraphics(elm, Module) {
101121 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
102122
103123 // set up updates so that the DOM is adjusted as necessary.
104− function domUpdate(newScene) {
124+ function domUpdate(newScene, currentScene) {
105125 ElmRuntime.draw(function(_) {
106126 Render.update(elm.node.firstChild, currentScene, newScene);
107− currentScene = newScene;
108127 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
109128 });
110129 return newScene;
111130 }
112− A2(Signal.lift, domUpdate, signalGraph);
131+ return A3(Signal.foldp, F2(domUpdate), currentScene, signalGraph);
113132}
114133
115134}());