-
Notifications
You must be signed in to change notification settings - Fork 209
/
engine-worker.js
147 lines (140 loc) · 3.87 KB
/
engine-worker.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import {
bindCanvas,
} from './renderer.js';
window.addEventListener('message', e => {
const method = e.data?.method;
if (method === 'initializeEngine') {
const {port} = e.data;
_bindPort(port);
}
});
const isTransferable = o => {
const ctor = o?.constructor;
return ctor === MessagePort ||
ctor === ImageBitmap ||
ctor === ImageData ||
// ctor === AudioData ||
// ctor === OffscreenCanvas ||
ctor === ArrayBuffer ||
ctor === Uint8Array ||
ctor === Int8Array ||
ctor === Uint16Array ||
ctor === Int16Array ||
ctor === Uint32Array ||
ctor === Int32Array ||
ctor === Float32Array ||
ctor === Float64Array;
};
const getTransferables = o => {
const result = [];
const _recurse = o => {
if (Array.isArray(o)) {
for (const e of o) {
_recurse(e);
}
} else if (o && typeof o === 'object') {
if (isTransferable(o)) {
result.push(o);
} else {
for (const k in o) {
_recurse(o[k]);
}
}
}
};
_recurse(o);
return result;
};
async function import2(s) {
if (/^(?:ipfs:\/\/|https?:\/\/|weba:\/\/|data:)/.test(s)) {
const prefix = location.protocol + '//' + location.host + '/@proxy/';
if (s.startsWith(prefix)) {
s = s.slice(prefix.length);
}
s = `/@proxy/${encodeURI(s)}`;
}
try {
const m = await import(s);
return m;
} catch(err) {
console.warn('error loading', JSON.stringify(s), err.stack);
return null;
}
}
const _bindPort = port => {
const handlers = new Map();
port.addEventListener('message', async e => {
const {method, id} = e.data;
const respond = (error = null, result = null, transfers = []) => {
port.postMessage({
method: 'response',
id,
error,
result,
}, transfers);
};
if (method) {
switch (method) {
case 'registerHandler': {
const {handlerId, src} = e.data;
let error = null;
let result = null;
try {
const u = `data:application/javascript;charset=utf-8,${encodeURIComponent(src)}`;
const module = await import2(u);
if (typeof module.default === 'function') {
const fn = module.default;
handlers.set(handlerId, fn);
result = 'ok';
} else {
console.warn('engine worker bad module', module);
throw new Error('engine worker module default export is not a function');
}
} catch(err) {
error = err?.stack ?? (err + '');
} finally {
respond(error, result);
}
break;
}
case 'callHandler': {
const {handlerId, args} = e.data;
const handler = handlers.get(handlerId);
if (handler) {
let error;
let result;
let transfers;
try {
result = await handler.apply(null, args);
transfers = getTransferables(result);
} catch(err) {
error = err?.stack ?? (err + '');
} finally {
respond(error, result, transfers);
}
} else {
respond(new Error('no handler registered: ' + handlerId));
}
break;
}
default: {
console.warn(`Unknown method: ${method}`);``
break;
}
}
}
});
port.start();
};
const canvas = document.getElementById('canvas');
window.innerWidth = canvas.width;
window.innerHeight = canvas.height;
window.devicePixelRatio = 1;
bindCanvas(canvas);
const match = location.hash.match(/^#id=(.+)$/);
const id = match ? match[1] : null;
if (id) {
// nothing
} else {
throw new Error('no id in engine worker');
}