on master help sign in
Switch from old FFI to new port interface for default output ports

Also add default handlers for stdout and stderr

commit7e64531d2e30657bf46f03d3a44f875ec47d4319 authorEvan Czaplicki <info@elm-lang.org> dateSat 4 Jan 2014, 11:41 (13 years ago) parent7cf5a56
1 file changed+27−21
runtime/Init.js+27−21
runtime/Init.js+27−21
@@ -71,31 +71,11 @@ function init(display, container, module, moduleToReplace, ports) {
7171 ports_in:ports
7272 };
7373
74− // Set up methods to communicate with Elm program from JS.
75− function send(name, value) {
76− if (typeof value === 'undefined') return function(v) { return send(name,v); };
77− var e = document.createEvent('Event');
78− e.initEvent(name + '_' + elm.id, true, true);
79− e.value = value;
80− document.dispatchEvent(e);
81− }
82− function recv(name, handler) {
83− document.addEventListener(name + '_' + elm.id, handler);
84− }
85−
86− recv('log', function(e) {console.log(e.value)});
87− recv('title', function(e) {document.title = e.value});
88− recv('redirect', function(e) {
89− if (e.value.length > 0) { window.location = e.value; }
90− });
91−
9274 function swap(newModule) {
9375 removeListeners(listeners);
9476 var div = document.createElement('div');
9577 var newElm = init(display, div, newModule, elm);
9678 inputs = [];
97− // elm.send = newElm.send;
98− // elm.recv = newElm.recv;
9979 // elm.swap = newElm.swap;
10080 return newElm;
10181 }
@@ -111,6 +91,7 @@ function init(display, container, module, moduleToReplace, ports) {
11191 }
11292 inputs = ElmRuntime.filterDeadInputs(inputs);
11393 filterListeners(inputs, listeners);
94+ addReceivers(elm.ports_out);
11495 if (display !== ElmRuntime.Display.NONE) {
11596 var graphicsNode = initGraphics(elm, Module);
11697 }
@@ -124,7 +105,7 @@ function init(display, container, module, moduleToReplace, ports) {
124105 }
125106
126107 reportAnyErrors();
127− return { send:send, recv:recv, swap:swap, ports:elm.ports_out };
108+ return { swap:swap, ports:elm.ports_out };
128109};
129110
130111function filterListeners(inputs, listeners) {
@@ -147,6 +128,31 @@ function removeListeners(listeners) {
147128 }
148129}
149130
131+// add receivers for built-in ports if they are defined
132+function addReceivers(ports) {
133+ if ('log' in ports) {
134+ ports.log.subscribe(function(v) { console.log(v) });
135+ }
136+ if ('stdout' in ports) {
137+ var handler = process ? function(v) { process.stdout.write(v); }
138+ : function(v) { console.log(v); };
139+ ports.stdout.subscribe(handler);
140+ }
141+ if ('stderr' in ports) {
142+ var handler = process ? function(v) { process.stderr.write(v); }
143+ : function(v) { console.log('Error:' + v); };
144+ ports.stderr.subscribe(handler);
145+ }
146+ if ('title' in ports) {
147+ ports.title.subscribe(function(v) { document.title = v; });
148+ }
149+ if ('redirect' in ports) {
150+ ports.redirect.subscribe(function(v) {
151+ if (v.length > 0) window.location = v;
152+ });
153+ }
154+}
155+
150156function initGraphics(elm, Module) {
151157 if (!('main' in Module))
152158 throw new Error("'main' is missing! What do I display?!");