-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (82 loc) · 3.02 KB
/
index.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
import { Elm } from "./elm/src/Main.elm"
window.customElements.define("completion-boundary",
class Boundary extends HTMLElement {
constructor() {
super()
this.elm = null
this.attachShadow({ mode: "open" })
this.shadowRoot.appendChild(document.createElement("div"))
}
connectedCallback() {
this.style.height = "100%"
this.style.width = "100%"
const plantId = this.getAttribute("plantId")
if (plantId) {
this.initializeElm(plantId)
} else {
this.shadowRoot.firstChild.innerText = "Error: plantId not provided!"
}
window.addEventListener("boundary:got-token", function (e) { this.gotToken(e) }.bind(this), false)
}
disconnectedCallback() {
window.removeEventListener("boundary:got-token", this.gotToken)
}
gotToken(e) {
this.elmMsg("gotToken", e.detail)
}
initializeElm(plantId) {
this.elm = Elm.Main.init({
node: this.shadowRoot.firstChild
, flags: { plantId }
})
this.elm.ports.toJs.subscribe(msg => this.update(msg))
}
elmMsg(topic, payload) {
this.elm.ports.fromJs.send({ topic: topic, payload: payload ? payload : null })
}
update(msg) {
switch (msg.topic) {
case "get": this.get(msg.payload); break;
case "put": this.put(msg.payload); break;
case "getToken": this.event("token-requested", { pluginName: "boundary", request: msg.payload }); break;
default: console.log("unknown msg from elm: ", msg)
}
}
event(topic, payload) {
this.dispatchEvent(new CustomEvent(topic, { bubbles: true, detail: payload }))
}
get(key) {
openDb().then(db => {
db.transaction("boundary", "readonly")
.objectStore("boundary")
.get(key)
.onsuccess = (e) => { this.elmMsg("got", { key: key, data: e.target.result }) }
})
}
put({ key, payload }) {
openDb().then(db => {
db.transaction("boundary", "readwrite")
.objectStore("boundary")
.put(payload, key)
})
}
})
function openDb() {
return new Promise((resolve, reject) => {
const request = indexedDB.open("completion-boundary", 1);
request.onupgradeneeded = onDbupgradeneeded()
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
request.onblocked = () => console.log("DB Blocked!");
});
}
function onDbupgradeneeded() {
return function (event) {
let db = event.target.result
if (db.objectStoreNames.contains("boundary")) {
//Do nothing
} else {
db.createObjectStore("boundary")
}
}
}