on master help sign in
Handle values sent to incoming ports, still need to properly define Elm.input though
502cfc0Evan Czaplicki
libraries/Native/Utils.js+35−7
@@ -224,15 +224,47 @@ Elm.Native.Utils.make = function(elm) {
224 if (result < 0) extra.push(key);
224 if (result < 0) extra.push(key);
225 }
225 }
226 if (missing.length > 0) {
226 if (missing.length > 0) {
227− throw new Error("Module " + moduleName +
227+ throw new Error("Initialization Error: module " + moduleName +
228− " requires inputs for these ports: " + missing.join(', '));
228+ " was not given inputs for the following ports: " + missing.join(', '));
229 }
229 }
230 if (extra.length > 0) {
230 if (extra.length > 0) {
231− throw new Error("Module " + moduleName +
231+ throw new Error("Initialization Error: module " + moduleName +
232 " has been given ports that do not exist: " + extra.join(', '));
232 " has been given ports that do not exist: " + extra.join(', '));
233 }
233 }
234 }
234 }
235
235
236+ // On failure, return message. On success, return the value in an array.
237+ // Wrapping in an array allows the programmer to pass in a null value.
238+ function processInput(converter, v) {
239+ try { var elmValue = converter(v); }
240+ catch(e) { return "The given value caused a runtime error!\n" + e.toString(); }
241+
242+ var ctor = elmValue.ctor;
243+ if (ctor === 'Nothing' || ctor === 'Left') {
244+ return "The port's conversion function failed.";
245+ } else if (ctor === 'Just' || ctor === 'Right') {
246+ return [elmValue._0];
247+ }
248+ return [elmValue];
249+ }
250+ function portIn(name, converter) {
251+ var port = elm.ports_in[name];
252+ var result = processInput(converter, port.defaultValue);
253+ if (typeof result === 'string') {
254+ throw new Error("Initialization Error: The default value for port '" +
255+ name + "' is invalid.\n" + result);
256+ }
257+ var signal = Signal.constant(result[0]);
258+ port.subscribe(function(v) {
259+ var result = processInput(converter, v);
260+ if (typeof result === 'string') {
261+ port.errorHandler(v)
262+ } else {
263+ elm.notify(signal.id, result[0]);
264+ }
265+ });
266+ return signal;
267+ }
236 function portOut(name, signal) {
268 function portOut(name, signal) {
237 var handlers = []
269 var handlers = []
238 function subscribe(handler) {
270 function subscribe(handler) {
@@ -250,10 +282,6 @@ Elm.Native.Utils.make = function(elm) {
250 elm.ports_out[name] = { subscribe:subscribe, unsubscribe:unsubscribe };
282 elm.ports_out[name] = { subscribe:subscribe, unsubscribe:unsubscribe };
251 return signal;
283 return signal;
252 }
284 }
253− function portIn(name, handler) {
254− var port = elm.ports_in[name];
255− throw new Error('need to decide on Elm.input definition.');
256− }
257
285
258 return elm.Native.Utils.values = {
286 return elm.Native.Utils.values = {
259 eq:eq,
287 eq:eq,
runtime/Init.js+3
@@ -27,6 +27,9 @@ Elm.worker = function(module, ports) {
27 return init(ElmRuntime.Display.NONE, {}, module, ports);
27 return init(ElmRuntime.Display.NONE, {}, module, ports);
28};
28};
29
29
30+Elm.input = function(defaultValue, errorHandler) {
31+}
32+
30function init(display, container, module, moduleToReplace, ports) {
33function init(display, container, module, moduleToReplace, ports) {
31 // defining state needed for an instance of the Elm RTS
34 // defining state needed for an instance of the Elm RTS
32 var inputs = [];
35 var inputs = [];