on master help sign in
Handle values sent to incoming ports, still need to properly define Elm.input though
commit502cfc0465b4db22c43fd4c7d580343285b9e896 authorEvan Czaplicki <info@elm-lang.org> dateSun 5 Jan 2014, 13:47 (13 years ago) parenta97b995
2 files changed+38−7
libraries/Native/Utils.js+35−7 runtime/Init.js+3
libraries/Native/Utils.js+35−7
@@ -224,15 +224,47 @@ Elm.Native.Utils.make = function(elm) {
224224 if (result < 0) extra.push(key);
225225 }
226226 if (missing.length > 0) {
227− throw new Error("Module " + moduleName +
228− " requires inputs for these ports: " + missing.join(', '));
227+ throw new Error("Initialization Error: module " + moduleName +
228+ " was not given inputs for the following ports: " + missing.join(', '));
229229 }
230230 if (extra.length > 0) {
231− throw new Error("Module " + moduleName +
231+ throw new Error("Initialization Error: module " + moduleName +
232232 " has been given ports that do not exist: " + extra.join(', '));
233233 }
234234 }
235235
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+ }
236268 function portOut(name, signal) {
237269 var handlers = []
238270 function subscribe(handler) {
@@ -250,10 +282,6 @@ Elm.Native.Utils.make = function(elm) {
250282 elm.ports_out[name] = { subscribe:subscribe, unsubscribe:unsubscribe };
251283 return signal;
252284 }
253− function portIn(name, handler) {
254− var port = elm.ports_in[name];
255− throw new Error('need to decide on Elm.input definition.');
256− }
257285
258286 return elm.Native.Utils.values = {
259287 eq:eq,
runtime/Init.js+3
@@ -27,6 +27,9 @@ Elm.worker = function(module, ports) {
2727 return init(ElmRuntime.Display.NONE, {}, module, ports);
2828};
2929
30+Elm.input = function(defaultValue, errorHandler) {
31+}
32+
3033function init(display, container, module, moduleToReplace, ports) {
3134 // defining state needed for an instance of the Elm RTS
3235 var inputs = [];