on master help sign in
Switch from old FFI to new port interface for default output ports
7e64531Evan Czaplicki
runtime/Init.js+27−21
@@ -71,31 +71,11 @@ function init(display, container, module, moduleToReplace, ports) {
71 ports_in:ports
71 ports_in:ports
72 };
72 };
73
73
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−
92 function swap(newModule) {
74 function swap(newModule) {
93 removeListeners(listeners);
75 removeListeners(listeners);
94 var div = document.createElement('div');
76 var div = document.createElement('div');
95 var newElm = init(display, div, newModule, elm);
77 var newElm = init(display, div, newModule, elm);
96 inputs = [];
78 inputs = [];
97− // elm.send = newElm.send;
98− // elm.recv = newElm.recv;
99 // elm.swap = newElm.swap;
79 // elm.swap = newElm.swap;
100 return newElm;
80 return newElm;
101 }
81 }
@@ -111,6 +91,7 @@ function init(display, container, module, moduleToReplace, ports) {
111 }
91 }
112 inputs = ElmRuntime.filterDeadInputs(inputs);
92 inputs = ElmRuntime.filterDeadInputs(inputs);
113 filterListeners(inputs, listeners);
93 filterListeners(inputs, listeners);
94+ addReceivers(elm.ports_out);
114 if (display !== ElmRuntime.Display.NONE) {
95 if (display !== ElmRuntime.Display.NONE) {
115 var graphicsNode = initGraphics(elm, Module);
96 var graphicsNode = initGraphics(elm, Module);
116 }
97 }
@@ -124,7 +105,7 @@ function init(display, container, module, moduleToReplace, ports) {
124 }
105 }
125
106
126 reportAnyErrors();
107 reportAnyErrors();
127− return { send:send, recv:recv, swap:swap, ports:elm.ports_out };
108+ return { swap:swap, ports:elm.ports_out };
128};
109};
129
110
130function filterListeners(inputs, listeners) {
111function filterListeners(inputs, listeners) {
@@ -147,6 +128,31 @@ function removeListeners(listeners) {
147 }
128 }
148}
129}
149
130
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+
150function initGraphics(elm, Module) {
156function initGraphics(elm, Module) {
151 if (!('main' in Module))
157 if (!('main' in Module))
152 throw new Error("'main' is missing! What do I display?!");
158 throw new Error("'main' is missing! What do I display?!");