-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
45 lines (41 loc) · 1.07 KB
/
script.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
import { fetchEventSource } from "/fetch-event-source.js";
const uid = `${Math.floor(Date.now() / 1000)}-${Math.random()}`;
function searchForInit(obj) {
if (obj.init) {
return obj.init;
} else {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
const result = searchForInit(obj[key]);
if (result) {
return result;
}
}
}
}
}
let app = searchForInit(Elm)({
node: document.getElementById("elm"),
flags: { uid },
});
app.ports.createEventSource.subscribe((url) => {
fetchEventSource(url, {
headers: {
Accept: "application/x-urb-jam",
"Access-Control-Request-Headers": "X-Requested-With",
"x-channel-format": "application/x-urb-jam",
"content-type": "application/x-urb-jam",
},
credentials: "include",
responseTimeout: 25000,
openWhenHidden: true,
onmessage(ev) {
console.log(ev);
app.ports.onEventSourceMessage.send({ message: ev.data });
},
onerror(err) {
console.log(err);
app.ports.onEventSourceMessage.send({ error: err });
},
});
});