@kbve/droid 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/droid.mjs +76 -0
- package/index.js +2972 -0
- package/lib/workers/canvas-worker.js +303 -0
- package/lib/workers/db-worker.js +3639 -0
- package/lib/workers/ws-worker.js +274 -0
- package/package.json +13 -9
- package/reference.js +259 -0
- package/src/lib/workers/main.ts +177 -134
- package/workers/main.js +616 -582
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2019 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
const A = Symbol("Comlink.proxy"), F = Symbol("Comlink.endpoint"), O = Symbol("Comlink.releaseProxy"), k = Symbol("Comlink.finalizer"), d = Symbol("Comlink.thrown"), C = (t) => typeof t == "object" && t !== null || typeof t == "function", z = {
|
|
7
|
+
canHandle: (t) => C(t) && t[A],
|
|
8
|
+
serialize(t) {
|
|
9
|
+
const { port1: e, port2: r } = new MessageChannel();
|
|
10
|
+
return S(t, e), [r, [r]];
|
|
11
|
+
},
|
|
12
|
+
deserialize(t) {
|
|
13
|
+
return t.start(), H(t);
|
|
14
|
+
}
|
|
15
|
+
}, N = {
|
|
16
|
+
canHandle: (t) => C(t) && d in t,
|
|
17
|
+
serialize({ value: t }) {
|
|
18
|
+
let e;
|
|
19
|
+
return t instanceof Error ? e = {
|
|
20
|
+
isError: !0,
|
|
21
|
+
value: {
|
|
22
|
+
message: t.message,
|
|
23
|
+
name: t.name,
|
|
24
|
+
stack: t.stack
|
|
25
|
+
}
|
|
26
|
+
} : e = { isError: !1, value: t }, [e, []];
|
|
27
|
+
},
|
|
28
|
+
deserialize(t) {
|
|
29
|
+
throw t.isError ? Object.assign(new Error(t.value.message), t.value) : t.value;
|
|
30
|
+
}
|
|
31
|
+
}, P = /* @__PURE__ */ new Map([
|
|
32
|
+
["proxy", z],
|
|
33
|
+
["throw", N]
|
|
34
|
+
]);
|
|
35
|
+
function W(t, e) {
|
|
36
|
+
for (const r of t)
|
|
37
|
+
if (e === r || r === "*" || r instanceof RegExp && r.test(e))
|
|
38
|
+
return !0;
|
|
39
|
+
return !1;
|
|
40
|
+
}
|
|
41
|
+
function S(t, e = globalThis, r = ["*"]) {
|
|
42
|
+
e.addEventListener("message", function l(n) {
|
|
43
|
+
if (!n || !n.data)
|
|
44
|
+
return;
|
|
45
|
+
if (!W(r, n.origin)) {
|
|
46
|
+
console.warn(`Invalid origin '${n.origin}' for comlink proxy`);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const { id: s, type: m, path: c } = Object.assign({ path: [] }, n.data), u = (n.data.argumentList || []).map(h);
|
|
50
|
+
let a;
|
|
51
|
+
try {
|
|
52
|
+
const o = c.slice(0, -1).reduce((i, y) => i[y], t), f = c.reduce((i, y) => i[y], t);
|
|
53
|
+
switch (m) {
|
|
54
|
+
case "GET":
|
|
55
|
+
a = f;
|
|
56
|
+
break;
|
|
57
|
+
case "SET":
|
|
58
|
+
o[c.slice(-1)[0]] = h(n.data.value), a = !0;
|
|
59
|
+
break;
|
|
60
|
+
case "APPLY":
|
|
61
|
+
a = f.apply(o, u);
|
|
62
|
+
break;
|
|
63
|
+
case "CONSTRUCT":
|
|
64
|
+
{
|
|
65
|
+
const i = new f(...u);
|
|
66
|
+
a = $(i);
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
case "ENDPOINT":
|
|
70
|
+
{
|
|
71
|
+
const { port1: i, port2: y } = new MessageChannel();
|
|
72
|
+
S(t, y), a = _(i, [i]);
|
|
73
|
+
}
|
|
74
|
+
break;
|
|
75
|
+
case "RELEASE":
|
|
76
|
+
a = void 0;
|
|
77
|
+
break;
|
|
78
|
+
default:
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
} catch (o) {
|
|
82
|
+
a = { value: o, [d]: 0 };
|
|
83
|
+
}
|
|
84
|
+
Promise.resolve(a).catch((o) => ({ value: o, [d]: 0 })).then((o) => {
|
|
85
|
+
const [f, i] = b(o);
|
|
86
|
+
e.postMessage(Object.assign(Object.assign({}, f), { id: s }), i), m === "RELEASE" && (e.removeEventListener("message", l), R(e), k in t && typeof t[k] == "function" && t[k]());
|
|
87
|
+
}).catch((o) => {
|
|
88
|
+
const [f, i] = b({
|
|
89
|
+
value: new TypeError("Unserializable return value"),
|
|
90
|
+
[d]: 0
|
|
91
|
+
});
|
|
92
|
+
e.postMessage(Object.assign(Object.assign({}, f), { id: s }), i);
|
|
93
|
+
});
|
|
94
|
+
}), e.start && e.start();
|
|
95
|
+
}
|
|
96
|
+
function D(t) {
|
|
97
|
+
return t.constructor.name === "MessagePort";
|
|
98
|
+
}
|
|
99
|
+
function R(t) {
|
|
100
|
+
D(t) && t.close();
|
|
101
|
+
}
|
|
102
|
+
function H(t, e) {
|
|
103
|
+
const r = /* @__PURE__ */ new Map();
|
|
104
|
+
return t.addEventListener("message", function(n) {
|
|
105
|
+
const { data: s } = n;
|
|
106
|
+
if (!s || !s.id)
|
|
107
|
+
return;
|
|
108
|
+
const m = r.get(s.id);
|
|
109
|
+
if (m)
|
|
110
|
+
try {
|
|
111
|
+
m(s);
|
|
112
|
+
} finally {
|
|
113
|
+
r.delete(s.id);
|
|
114
|
+
}
|
|
115
|
+
}), M(t, r, [], e);
|
|
116
|
+
}
|
|
117
|
+
function w(t) {
|
|
118
|
+
if (t)
|
|
119
|
+
throw new Error("Proxy has been released and is not useable");
|
|
120
|
+
}
|
|
121
|
+
function v(t) {
|
|
122
|
+
return g(t, /* @__PURE__ */ new Map(), {
|
|
123
|
+
type: "RELEASE"
|
|
124
|
+
}).then(() => {
|
|
125
|
+
R(t);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const E = /* @__PURE__ */ new WeakMap(), x = "FinalizationRegistry" in globalThis && new FinalizationRegistry((t) => {
|
|
129
|
+
const e = (E.get(t) || 0) - 1;
|
|
130
|
+
E.set(t, e), e === 0 && v(t);
|
|
131
|
+
});
|
|
132
|
+
function L(t, e) {
|
|
133
|
+
const r = (E.get(e) || 0) + 1;
|
|
134
|
+
E.set(e, r), x && x.register(t, e, t);
|
|
135
|
+
}
|
|
136
|
+
function U(t) {
|
|
137
|
+
x && x.unregister(t);
|
|
138
|
+
}
|
|
139
|
+
function M(t, e, r = [], l = function() {
|
|
140
|
+
}) {
|
|
141
|
+
let n = !1;
|
|
142
|
+
const s = new Proxy(l, {
|
|
143
|
+
get(m, c) {
|
|
144
|
+
if (w(n), c === O)
|
|
145
|
+
return () => {
|
|
146
|
+
U(s), v(t), e.clear(), n = !0;
|
|
147
|
+
};
|
|
148
|
+
if (c === "then") {
|
|
149
|
+
if (r.length === 0)
|
|
150
|
+
return { then: () => s };
|
|
151
|
+
const u = g(t, e, {
|
|
152
|
+
type: "GET",
|
|
153
|
+
path: r.map((a) => a.toString())
|
|
154
|
+
}).then(h);
|
|
155
|
+
return u.then.bind(u);
|
|
156
|
+
}
|
|
157
|
+
return M(t, e, [...r, c]);
|
|
158
|
+
},
|
|
159
|
+
set(m, c, u) {
|
|
160
|
+
w(n);
|
|
161
|
+
const [a, o] = b(u);
|
|
162
|
+
return g(t, e, {
|
|
163
|
+
type: "SET",
|
|
164
|
+
path: [...r, c].map((f) => f.toString()),
|
|
165
|
+
value: a
|
|
166
|
+
}, o).then(h);
|
|
167
|
+
},
|
|
168
|
+
apply(m, c, u) {
|
|
169
|
+
w(n);
|
|
170
|
+
const a = r[r.length - 1];
|
|
171
|
+
if (a === F)
|
|
172
|
+
return g(t, e, {
|
|
173
|
+
type: "ENDPOINT"
|
|
174
|
+
}).then(h);
|
|
175
|
+
if (a === "bind")
|
|
176
|
+
return M(t, e, r.slice(0, -1));
|
|
177
|
+
const [o, f] = p(u);
|
|
178
|
+
return g(t, e, {
|
|
179
|
+
type: "APPLY",
|
|
180
|
+
path: r.map((i) => i.toString()),
|
|
181
|
+
argumentList: o
|
|
182
|
+
}, f).then(h);
|
|
183
|
+
},
|
|
184
|
+
construct(m, c) {
|
|
185
|
+
w(n);
|
|
186
|
+
const [u, a] = p(c);
|
|
187
|
+
return g(t, e, {
|
|
188
|
+
type: "CONSTRUCT",
|
|
189
|
+
path: r.map((o) => o.toString()),
|
|
190
|
+
argumentList: u
|
|
191
|
+
}, a).then(h);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
return L(s, t), s;
|
|
195
|
+
}
|
|
196
|
+
function V(t) {
|
|
197
|
+
return Array.prototype.concat.apply([], t);
|
|
198
|
+
}
|
|
199
|
+
function p(t) {
|
|
200
|
+
const e = t.map(b);
|
|
201
|
+
return [e.map((r) => r[0]), V(e.map((r) => r[1]))];
|
|
202
|
+
}
|
|
203
|
+
const T = /* @__PURE__ */ new WeakMap();
|
|
204
|
+
function _(t, e) {
|
|
205
|
+
return T.set(t, e), t;
|
|
206
|
+
}
|
|
207
|
+
function $(t) {
|
|
208
|
+
return Object.assign(t, { [A]: !0 });
|
|
209
|
+
}
|
|
210
|
+
function b(t) {
|
|
211
|
+
for (const [e, r] of P)
|
|
212
|
+
if (r.canHandle(t)) {
|
|
213
|
+
const [l, n] = r.serialize(t);
|
|
214
|
+
return [
|
|
215
|
+
{
|
|
216
|
+
type: "HANDLER",
|
|
217
|
+
name: e,
|
|
218
|
+
value: l
|
|
219
|
+
},
|
|
220
|
+
n
|
|
221
|
+
];
|
|
222
|
+
}
|
|
223
|
+
return [
|
|
224
|
+
{
|
|
225
|
+
type: "RAW",
|
|
226
|
+
value: t
|
|
227
|
+
},
|
|
228
|
+
T.get(t) || []
|
|
229
|
+
];
|
|
230
|
+
}
|
|
231
|
+
function h(t) {
|
|
232
|
+
switch (t.type) {
|
|
233
|
+
case "HANDLER":
|
|
234
|
+
return P.get(t.name).deserialize(t.value);
|
|
235
|
+
case "RAW":
|
|
236
|
+
return t.value;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
function g(t, e, r, l) {
|
|
240
|
+
return new Promise((n) => {
|
|
241
|
+
const s = j();
|
|
242
|
+
e.set(s, n), t.start && t.start(), t.postMessage(Object.assign({ id: s }, r), l);
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
function j() {
|
|
246
|
+
return new Array(4).fill(0).map(() => Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(16)).join("-");
|
|
247
|
+
}
|
|
248
|
+
const q = {
|
|
249
|
+
bindings: /* @__PURE__ */ new Map(),
|
|
250
|
+
async bindCanvas(t, e, r = "animated") {
|
|
251
|
+
const l = e.getContext("2d");
|
|
252
|
+
if (!l) {
|
|
253
|
+
console.error(`[CanvasWorker] Failed to get 2D context for panel ${t}`);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
console.log(`[CanvasWorker] Successfully bound canvas for panel ${t} with mode ${r}`), this.bindings.set(t, { ctx: l, canvas: e, panelId: t, mode: r }), this.startAnimation(t);
|
|
257
|
+
},
|
|
258
|
+
startAnimation(t) {
|
|
259
|
+
const e = this.bindings.get(t);
|
|
260
|
+
if (e)
|
|
261
|
+
switch (e.mode) {
|
|
262
|
+
case "static":
|
|
263
|
+
this.drawStatic(e);
|
|
264
|
+
break;
|
|
265
|
+
case "animated":
|
|
266
|
+
this.drawAnimated(e);
|
|
267
|
+
break;
|
|
268
|
+
case "dynamic":
|
|
269
|
+
this.drawDynamic(e);
|
|
270
|
+
break;
|
|
271
|
+
default:
|
|
272
|
+
console.warn(`[CanvasWorker] Unknown draw mode for panel ${t}`);
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
drawStatic(t) {
|
|
276
|
+
t.ctx.fillStyle = "gray", t.ctx.fillRect(0, 0, t.canvas.width, t.canvas.height);
|
|
277
|
+
},
|
|
278
|
+
drawAnimated(t) {
|
|
279
|
+
let e = 0;
|
|
280
|
+
const r = () => {
|
|
281
|
+
e = (e + 1) % 360, t.ctx.fillStyle = `hsl(${e}, 100%, 50%)`, t.ctx.fillRect(0, 0, t.canvas.width, t.canvas.height), t.animationFrame = requestAnimationFrame(r);
|
|
282
|
+
};
|
|
283
|
+
r();
|
|
284
|
+
},
|
|
285
|
+
drawDynamic(t) {
|
|
286
|
+
let e = 0;
|
|
287
|
+
const r = () => {
|
|
288
|
+
e += 0.05, t.ctx.clearRect(0, 0, t.canvas.width, t.canvas.height), t.ctx.beginPath(), t.ctx.arc(
|
|
289
|
+
t.canvas.width / 2 + Math.sin(e) * 50,
|
|
290
|
+
t.canvas.height / 2 + Math.cos(e) * 50,
|
|
291
|
+
30,
|
|
292
|
+
0,
|
|
293
|
+
Math.PI * 2
|
|
294
|
+
), t.ctx.fillStyle = "orange", t.ctx.fill(), t.animationFrame = requestAnimationFrame(r);
|
|
295
|
+
};
|
|
296
|
+
r();
|
|
297
|
+
},
|
|
298
|
+
async unbindCanvas(t) {
|
|
299
|
+
const e = this.bindings.get(t);
|
|
300
|
+
e?.animationFrame && cancelAnimationFrame(e.animationFrame), this.bindings.delete(t), console.log(`[CanvasWorker] Unbound canvas for panel ${t}`);
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
S(q);
|