forked from elm/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ports.js
92 lines (83 loc) · 2.83 KB
/
Ports.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Elm.Native.Ports = {};
Elm.Native.Ports.make = function(localRuntime) {
localRuntime.Native = localRuntime.Native || {};
localRuntime.Native.Ports = localRuntime.Native.Ports || {};
if (localRuntime.Native.Ports.values) {
return localRuntime.Native.Ports.values;
}
var Signal;
function incomingSignal(converter) {
converter.isSignal = true;
return converter;
}
function outgoingSignal(converter) {
if (!Signal) {
Signal = Elm.Signal.make(localRuntime);
}
return function(signal) {
var subscribers = []
function subscribe(handler) {
subscribers.push(handler);
}
function unsubscribe(handler) {
subscribers.pop(subscribers.indexOf(handler));
}
A2( Signal.map, function(value) {
var val = converter(value);
var len = subscribers.length;
for (var i = 0; i < len; ++i) {
subscribers[i](val);
}
}, signal);
return { subscribe:subscribe, unsubscribe:unsubscribe };
}
}
function portIn(name, converter) {
var jsValue = localRuntime.ports.incoming[name];
if (jsValue === undefined) {
throw new Error("Initialization Error: port '" + name +
"' was not given an input!");
}
localRuntime.ports.uses[name] += 1;
try {
var elmValue = converter(jsValue);
} catch(e) {
throw new Error("Initialization Error on port '" + name + "': \n" + e.message);
}
// just return a static value if it is not a signal
if (!converter.isSignal) {
return elmValue;
}
// create a signal if necessary
if (!Signal) {
Signal = Elm.Signal.make(localRuntime);
}
var signal = Signal.constant(elmValue);
function send(jsValue) {
try {
var elmValue = converter(jsValue);
} catch(e) {
throw new Error("Error sending to port '" + name + "': \n" + e.message);
}
setTimeout(function() {
localRuntime.notify(signal.id, elmValue);
}, 0);
}
localRuntime.ports.outgoing[name] = { send:send };
return signal;
}
function portOut(name, converter, value) {
try {
localRuntime.ports.outgoing[name] = converter(value);
} catch(e) {
throw new Error("Initialization Error on port '" + name + "': \n" + e.message);
}
return value;
}
return localRuntime.Native.Ports.values = {
incomingSignal: incomingSignal,
outgoingSignal: outgoingSignal,
portOut: portOut,
portIn: portIn
};
};