@@ -31,9 +31,7 @@ Elm.worker = function(module) {
3131
3232function init(display, container, module) {
3333 // defining state needed for an instance of the Elm RTS
34− var signalGraph = null;
3534 var inputs = [];
36− var visualModel = null;
3735
3836 function notify(id, v) {
3937 var timestep = Date.now();
@@ -76,42 +74,42 @@ function init(display, container, module) {
7674 if (e.value.length > 0) { window.location = e.value; }
7775 });
7876
77+ var Module = module(elm);
78+ inputs = ElmRuntime.filterDeadInputs(inputs);
79+
7980 // If graphics are not enabled, escape early, skip over setting up DOM stuff.
80− if (display === ElmRuntime.Display.NONE) {
81− module(elm);
82− return { send : send, recv : recv };
83− }
81+ if (display !== ElmRuntime.Display.NONE) { initGraphics(elm, Module); }
8482
85− var Render = ElmRuntime.use(ElmRuntime.Render.Element);
83+ return { send:send, recv:recv,
84+ __private__ : { inputs:inputs, container:container } };
85+};
8686
87− // evaluate the given module and extract its 'main' value.
88− signalGraph = module(elm).main;
87+function initGraphics(elm, Module) {
88+ if (!('main' in Module)) throw new Error("'main' is missing! What do I display?!");
89+ var signalGraph = Module.main;
8990
90− // make sure the signal graph is actually a signal, extract the visual model,
91− // and filter out any unused inputs.
92− var Signal = Elm.Signal(elm);
93− if (!('recv' in signalGraph)) signalGraph = Signal.constant(signalGraph);
94− visualModel = signalGraph.value;
95− inputs = ElmRuntime.filterDeadInputs(inputs);
91+ // make sure the signal graph is actually a signal & extract the visual model
92+ if (!('recv' in signalGraph)) {
93+ var Signal = Elm.Signal(elm);
94+ signalGraph = Signal.constant(signalGraph);
95+ }
96+ var currentScene = signalGraph.value;
9697
97− // Add the visualModel to the DOM
98− container.appendChild(Render.render(visualModel));
98+ // Add the currentScene to the DOM
99+ var Render = ElmRuntime.use(ElmRuntime.Render.Element);
100+ container.appendChild(Render.render(currentScene));
99101 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
100102
101103 // set up updates so that the DOM is adjusted as necessary.
102− var update = Render.update;
103− function domUpdate(value) {
104+ function domUpdate(newScene) {
104105 ElmRuntime.draw(function(_) {
105− update(container.firstChild, visualModel, value);
106− visualModel = value;
106+ Render.update(container.firstChild, currentScene, newScene);
107+ currentScene = newScene;
107108 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
108109 });
109− return value;
110+ return newScene;
110111 }
111−
112− signalGraph = A2(Signal.lift, domUpdate, signalGraph);
113−
114− return { send : send, recv : recv, node : container };
115−};
112+ A2(Signal.lift, domUpdate, signalGraph);
113+}
116114
117115}());