This repository was archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (56 loc) · 1.49 KB
/
index.js
File metadata and controls
70 lines (56 loc) · 1.49 KB
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
const { Main } = require('./main.js').Elm;
const initialize = model => new Promise((resolve, reject) => {
const isObject = (x = null) => {
if (x === null) {
return false;
}
return (x.constructor === Object);
};
if (isObject(model)) {
resolve(Main.init({ flags: model }));
} else {
reject(new TypeError('Initial model passed to Elm must be an object.'));
}
});
const getModel = machine => new Promise((resolve, reject) => {
const callback = (m) => {
if (m.resolve) {
machine.ports.outgoing.unsubscribe(callback);
resolve(m.value);
} else {
reject(new TypeError(m.error));
}
};
machine.ports.outgoing.subscribe(callback);
machine.ports.getModel.send({});
});
const getKey = (machine, key) => new Promise((resolve, reject) => {
const callback = (m) => {
if (m.resolve) {
machine.ports.outgoing.unsubscribe(callback);
resolve(m.value);
} else {
reject(new TypeError(m.error));
}
};
machine.ports.outgoing.subscribe(callback);
machine.ports.getKey.send({ key });
});
const updateKey = (machine, key, f, ...args) => new Promise((resolve, reject) => {
const callback = (m) => {
if (m.resolve) {
machine.ports.outgoing.unsubscribe(callback);
resolve(m.value);
} else {
reject(new TypeError(m.error));
}
};
machine.ports.outgoing.subscribe(callback);
machine.ports.updateKey.send({ key, f, args });
});
module.exports = {
initialize,
getModel,
getKey,
updateKey,
};