elm/runtime/Init.js
Evan Czaplicki 4b1474dcdc Correctly update model, accounting for possibly dropped frames
Thanks to @johnpmayer for discovering this issue!

Since draw is async, it was possible to update the model without
updating the underlying DOM stuff. This could cause issues on its own,
but if you need to pass info from model to model, that is also lost
when this happens.
2014-01-20 19:57:31 +01:00

240 lines
7.5 KiB
JavaScript

(function() {
'use strict';
Elm.fullscreen = function(module, ports) {
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = "html,head,body { padding:0; margin:0; }" +
"body { font-family: calibri, helvetica, arial, sans-serif; }";
document.head.appendChild(style);
var container = document.createElement('div');
document.body.appendChild(container);
return init(ElmRuntime.Display.FULLSCREEN, container, module, ports || {});
};
Elm.embed = function(module, container, ports) {
var tag = container.tagName;
if (tag !== 'DIV') {
throw new Error('Elm.node must be given a DIV, not a ' + tag + '.');
} else if (container.hasChildNodes()) {
throw new Error('Elm.node must be given an empty DIV. No children allowed!');
}
return init(ElmRuntime.Display.COMPONENT, container, module, ports || {});
};
Elm.worker = function(module, ports) {
return init(ElmRuntime.Display.NONE, {}, module, ports || {});
};
function init(display, container, module, ports, moduleToReplace) {
// defining state needed for an instance of the Elm RTS
var inputs = [];
var updateInProgress = false;
function notify(id, v) {
if (updateInProgress) {
throw new Error(
'The notify function has been called synchronously!\n' +
'This can lead to frames being dropped.\n' +
'Definitely report this to <https://github.com/evancz/Elm/issues>\n');
}
updateInProgress = true;
var timestep = Date.now();
for (var i = inputs.length; i--; ) {
inputs[i].recv(timestep, id, v);
}
updateInProgress = false;
}
var listeners = [];
function addListener(relevantInputs, domNode, eventName, func) {
domNode.addEventListener(eventName, func);
var listener = {
relevantInputs: relevantInputs,
domNode: domNode,
eventName: eventName,
func: func
};
listeners.push(listener);
}
var portUses = {}
for (var key in ports) {
portUses[key] = 0;
}
// create the actual RTS. Any impure modules will attach themselves to this
// object. This permits many Elm programs to be embedded per document.
var elm = {
notify:notify,
node:container,
display:display,
id:ElmRuntime.guid(),
addListener:addListener,
inputs:inputs,
ports: { incoming:ports, outgoing:{}, uses:portUses }
};
function swap(newModule) {
removeListeners(listeners);
var div = document.createElement('div');
var newElm = init(display, div, newModule, ports, elm);
inputs = [];
// elm.swap = newElm.swap;
return newElm;
}
var Module = {};
var reportAnyErrors = function() {};
try {
Module = module.make(elm);
checkPorts(elm);
} catch(e) {
var directions = "<br/>&nbsp; &nbsp; Open the developer console for more details."
Module.main = Elm.Text.make(elm).text('<code>' + e.message + directions + '</code>');
reportAnyErrors = function() { throw e; }
}
inputs = ElmRuntime.filterDeadInputs(inputs);
filterListeners(inputs, listeners);
addReceivers(elm.ports.outgoing);
if (display !== ElmRuntime.Display.NONE) {
var graphicsNode = initGraphics(elm, Module);
}
if (typeof moduleToReplace !== 'undefined') {
ElmRuntime.swap(moduleToReplace, elm);
// rerender scene if graphics are enabled.
if (typeof graphicsNode !== 'undefined') {
graphicsNode.recv(0, true, 0);
}
}
reportAnyErrors();
return { swap:swap, ports:elm.ports.outgoing };
};
function checkPorts(elm) {
var portUses = elm.ports.uses;
for (var key in portUses) {
var uses = portUses[key]
if (uses === 0) {
throw new Error(
"Initialization Error: provided port '" + key +
"' to a module that does not take it as in input.\n" +
"Remove '" + key + "' from the module initialization code.");
} else if (uses > 1) {
throw new Error(
"Initialization Error: port '" + key +
"' has been declared multiple times in the Elm code.\n" +
"Remove declarations until there is exactly one.");
}
}
}
function filterListeners(inputs, listeners) {
loop:
for (var i = listeners.length; i--; ) {
var listener = listeners[i];
for (var j = inputs.length; j--; ) {
if (listener.relevantInputs.indexOf(inputs[j].id) >= 0) {
continue loop;
}
}
listener.domNode.removeEventListener(listener.eventName, listener.func);
}
}
function removeListeners(listeners) {
for (var i = listeners.length; i--; ) {
var listener = listeners[i];
listener.domNode.removeEventListener(listener.eventName, listener.func);
}
}
// add receivers for built-in ports if they are defined
function addReceivers(ports) {
if ('log' in ports) {
ports.log.subscribe(function(v) { console.log(v) });
}
if ('stdout' in ports) {
var process = process || {};
var handler = process.stdout
? function(v) { process.stdout.write(v); }
: function(v) { console.log(v); };
ports.stdout.subscribe(handler);
}
if ('stderr' in ports) {
var process = process || {};
var handler = process.stderr
? function(v) { process.stderr.write(v); }
: function(v) { console.log('Error:' + v); };
ports.stderr.subscribe(handler);
}
if ('title' in ports) {
if (typeof ports.title === 'string') {
document.title = ports.title;
} else {
ports.title.subscribe(function(v) { document.title = v; });
}
}
if ('redirect' in ports) {
ports.redirect.subscribe(function(v) {
if (v.length > 0) window.location = v;
});
}
if ('favicon' in ports) {
if (typeof ports.favicon === 'string') {
changeFavicon(ports.favicon);
} else {
ports.favicon.subscribe(changeFavicon);
}
}
function changeFavicon(src) {
var link = document.createElement('link');
var oldLink = document.getElementById('elm-favicon');
link.id = 'elm-favicon';
link.rel = 'shortcut icon';
link.href = src;
if (oldLink) {
document.head.removeChild(oldLink);
}
document.head.appendChild(link);
}
}
function initGraphics(elm, Module) {
if (!('main' in Module))
throw new Error("'main' is missing! What do I display?!");
var signalGraph = Module.main;
// make sure the signal graph is actually a signal & extract the visual model
var Signal = Elm.Signal.make(elm);
if (!('recv' in signalGraph)) {
signalGraph = Signal.constant(signalGraph);
}
var currentScene = signalGraph.value;
// Add the currentScene to the DOM
var Render = ElmRuntime.use(ElmRuntime.Render.Element);
elm.node.appendChild(Render.render(currentScene));
// set up updates so that the DOM is adjusted as necessary.
var savedScene = currentScene;
function domUpdate(newScene) {
ElmRuntime.draw(function(_) {
Render.update(elm.node.firstChild, savedScene, newScene);
if (elm.Native.Window) elm.Native.Window.values.resizeIfNeeded();
savedScene = newScene;
});
}
var renderer = A2(Signal.lift, domUpdate, signalGraph);
// must check for resize after 'renderer' is created so
// that changes show up.
if (elm.Native.Window) elm.Native.Window.values.resizeIfNeeded();
return renderer;
}
}());