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