@lvce-editor/component-state-worker 0.1.1
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/LICENSE +21 -0
- package/README.md +7 -0
- package/dist/componentStateWorkerMain.js +1428 -0
- package/package.json +17 -0
|
@@ -0,0 +1,1428 @@
|
|
|
1
|
+
const normalizeLine = line => {
|
|
2
|
+
if (line.startsWith('Error: ')) {
|
|
3
|
+
return line.slice('Error: '.length);
|
|
4
|
+
}
|
|
5
|
+
if (line.startsWith('VError: ')) {
|
|
6
|
+
return line.slice('VError: '.length);
|
|
7
|
+
}
|
|
8
|
+
return line;
|
|
9
|
+
};
|
|
10
|
+
const getCombinedMessage = (error, message) => {
|
|
11
|
+
const stringifiedError = normalizeLine(`${error}`);
|
|
12
|
+
if (message) {
|
|
13
|
+
return `${message}: ${stringifiedError}`;
|
|
14
|
+
}
|
|
15
|
+
return stringifiedError;
|
|
16
|
+
};
|
|
17
|
+
const NewLine$2 = '\n';
|
|
18
|
+
const getNewLineIndex$1 = (string, startIndex = undefined) => {
|
|
19
|
+
return string.indexOf(NewLine$2, startIndex);
|
|
20
|
+
};
|
|
21
|
+
const mergeStacks = (parent, child) => {
|
|
22
|
+
if (!child) {
|
|
23
|
+
return parent;
|
|
24
|
+
}
|
|
25
|
+
const parentNewLineIndex = getNewLineIndex$1(parent);
|
|
26
|
+
const childNewLineIndex = getNewLineIndex$1(child);
|
|
27
|
+
if (childNewLineIndex === -1) {
|
|
28
|
+
return parent;
|
|
29
|
+
}
|
|
30
|
+
const parentFirstLine = parent.slice(0, parentNewLineIndex);
|
|
31
|
+
const childRest = child.slice(childNewLineIndex);
|
|
32
|
+
const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
|
|
33
|
+
if (parentFirstLine.includes(childFirstLine)) {
|
|
34
|
+
return parentFirstLine + childRest;
|
|
35
|
+
}
|
|
36
|
+
return child;
|
|
37
|
+
};
|
|
38
|
+
class VError extends Error {
|
|
39
|
+
constructor(error, message) {
|
|
40
|
+
const combinedMessage = getCombinedMessage(error, message);
|
|
41
|
+
super(combinedMessage);
|
|
42
|
+
this.name = 'VError';
|
|
43
|
+
if (error instanceof Error) {
|
|
44
|
+
this.stack = mergeStacks(this.stack, error.stack);
|
|
45
|
+
}
|
|
46
|
+
if (error.codeFrame) {
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
this.codeFrame = error.codeFrame;
|
|
49
|
+
}
|
|
50
|
+
if (error.code) {
|
|
51
|
+
// @ts-ignore
|
|
52
|
+
this.code = error.code;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const isMessagePort = value => {
|
|
58
|
+
return value && value instanceof MessagePort;
|
|
59
|
+
};
|
|
60
|
+
const isMessagePortMain = value => {
|
|
61
|
+
return value && value.constructor && value.constructor.name === 'MessagePortMain';
|
|
62
|
+
};
|
|
63
|
+
const isOffscreenCanvas = value => {
|
|
64
|
+
return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
|
|
65
|
+
};
|
|
66
|
+
const isInstanceOf = (value, constructorName) => {
|
|
67
|
+
return value?.constructor?.name === constructorName;
|
|
68
|
+
};
|
|
69
|
+
const isSocket = value => {
|
|
70
|
+
return isInstanceOf(value, 'Socket');
|
|
71
|
+
};
|
|
72
|
+
const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
|
|
73
|
+
const isTransferrable = value => {
|
|
74
|
+
for (const fn of transferrables) {
|
|
75
|
+
if (fn(value)) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
};
|
|
81
|
+
const walkValue = (value, transferrables, isTransferrable) => {
|
|
82
|
+
if (!value) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (isTransferrable(value)) {
|
|
86
|
+
transferrables.push(value);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (Array.isArray(value)) {
|
|
90
|
+
for (const item of value) {
|
|
91
|
+
walkValue(item, transferrables, isTransferrable);
|
|
92
|
+
}
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (typeof value === 'object') {
|
|
96
|
+
for (const property of Object.values(value)) {
|
|
97
|
+
walkValue(property, transferrables, isTransferrable);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const getTransferrables = value => {
|
|
102
|
+
const transferrables = [];
|
|
103
|
+
walkValue(value, transferrables, isTransferrable);
|
|
104
|
+
return transferrables;
|
|
105
|
+
};
|
|
106
|
+
const attachEvents = that => {
|
|
107
|
+
const handleMessage = (...args) => {
|
|
108
|
+
const data = that.getData(...args);
|
|
109
|
+
that.dispatchEvent(new MessageEvent('message', {
|
|
110
|
+
data
|
|
111
|
+
}));
|
|
112
|
+
};
|
|
113
|
+
that.onMessage(handleMessage);
|
|
114
|
+
const handleClose = event => {
|
|
115
|
+
that.dispatchEvent(new Event('close'));
|
|
116
|
+
};
|
|
117
|
+
that.onClose(handleClose);
|
|
118
|
+
};
|
|
119
|
+
class Ipc extends EventTarget {
|
|
120
|
+
constructor(rawIpc) {
|
|
121
|
+
super();
|
|
122
|
+
this._rawIpc = rawIpc;
|
|
123
|
+
attachEvents(this);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
|
|
127
|
+
const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
|
|
128
|
+
const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
|
|
129
|
+
const NewLine$1 = '\n';
|
|
130
|
+
const joinLines$1 = lines => {
|
|
131
|
+
return lines.join(NewLine$1);
|
|
132
|
+
};
|
|
133
|
+
const RE_AT = /^\s+at/;
|
|
134
|
+
const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
|
|
135
|
+
const isNormalStackLine = line => {
|
|
136
|
+
return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
|
|
137
|
+
};
|
|
138
|
+
const getDetails = lines => {
|
|
139
|
+
const index = lines.findIndex(isNormalStackLine);
|
|
140
|
+
if (index === -1) {
|
|
141
|
+
return {
|
|
142
|
+
actualMessage: joinLines$1(lines),
|
|
143
|
+
rest: []
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
let lastIndex = index - 1;
|
|
147
|
+
while (++lastIndex < lines.length) {
|
|
148
|
+
if (!isNormalStackLine(lines[lastIndex])) {
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
actualMessage: lines[index - 1],
|
|
154
|
+
rest: lines.slice(index, lastIndex)
|
|
155
|
+
};
|
|
156
|
+
};
|
|
157
|
+
const splitLines$1 = lines => {
|
|
158
|
+
return lines.split(NewLine$1);
|
|
159
|
+
};
|
|
160
|
+
const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
|
|
161
|
+
const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
|
|
162
|
+
const isMessageCodeBlockStartIndex = line => {
|
|
163
|
+
return RE_MESSAGE_CODE_BLOCK_START.test(line);
|
|
164
|
+
};
|
|
165
|
+
const isMessageCodeBlockEndIndex = line => {
|
|
166
|
+
return RE_MESSAGE_CODE_BLOCK_END.test(line);
|
|
167
|
+
};
|
|
168
|
+
const getMessageCodeBlock = stderr => {
|
|
169
|
+
const lines = splitLines$1(stderr);
|
|
170
|
+
const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
|
|
171
|
+
const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
|
|
172
|
+
const relevantLines = lines.slice(startIndex, endIndex);
|
|
173
|
+
const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
|
|
174
|
+
return relevantMessage;
|
|
175
|
+
};
|
|
176
|
+
const isModuleNotFoundMessage = line => {
|
|
177
|
+
return line.includes('[ERR_MODULE_NOT_FOUND]');
|
|
178
|
+
};
|
|
179
|
+
const getModuleNotFoundError = stderr => {
|
|
180
|
+
const lines = splitLines$1(stderr);
|
|
181
|
+
const messageIndex = lines.findIndex(isModuleNotFoundMessage);
|
|
182
|
+
const message = lines[messageIndex];
|
|
183
|
+
return {
|
|
184
|
+
code: ERR_MODULE_NOT_FOUND,
|
|
185
|
+
message
|
|
186
|
+
};
|
|
187
|
+
};
|
|
188
|
+
const isModuleNotFoundError = stderr => {
|
|
189
|
+
if (!stderr) {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
return stderr.includes('ERR_MODULE_NOT_FOUND');
|
|
193
|
+
};
|
|
194
|
+
const isModulesSyntaxError = stderr => {
|
|
195
|
+
if (!stderr) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
return stderr.includes('SyntaxError: Cannot use import statement outside a module');
|
|
199
|
+
};
|
|
200
|
+
const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
|
|
201
|
+
const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
|
|
202
|
+
const isUnhelpfulNativeModuleError = stderr => {
|
|
203
|
+
return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
|
|
204
|
+
};
|
|
205
|
+
const getNativeModuleErrorMessage = stderr => {
|
|
206
|
+
const message = getMessageCodeBlock(stderr);
|
|
207
|
+
return {
|
|
208
|
+
code: E_INCOMPATIBLE_NATIVE_MODULE,
|
|
209
|
+
message: `Incompatible native node module: ${message}`
|
|
210
|
+
};
|
|
211
|
+
};
|
|
212
|
+
const getModuleSyntaxError = () => {
|
|
213
|
+
return {
|
|
214
|
+
code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON,
|
|
215
|
+
message: `ES Modules are not supported in electron`
|
|
216
|
+
};
|
|
217
|
+
};
|
|
218
|
+
const getHelpfulChildProcessError = (stdout, stderr) => {
|
|
219
|
+
if (isUnhelpfulNativeModuleError(stderr)) {
|
|
220
|
+
return getNativeModuleErrorMessage(stderr);
|
|
221
|
+
}
|
|
222
|
+
if (isModulesSyntaxError(stderr)) {
|
|
223
|
+
return getModuleSyntaxError();
|
|
224
|
+
}
|
|
225
|
+
if (isModuleNotFoundError(stderr)) {
|
|
226
|
+
return getModuleNotFoundError(stderr);
|
|
227
|
+
}
|
|
228
|
+
const lines = splitLines$1(stderr);
|
|
229
|
+
const {
|
|
230
|
+
actualMessage,
|
|
231
|
+
rest
|
|
232
|
+
} = getDetails(lines);
|
|
233
|
+
return {
|
|
234
|
+
code: '',
|
|
235
|
+
message: actualMessage,
|
|
236
|
+
stack: rest
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
class IpcError extends VError {
|
|
240
|
+
// @ts-ignore
|
|
241
|
+
constructor(betterMessage, stdout = '', stderr = '') {
|
|
242
|
+
if (stdout || stderr) {
|
|
243
|
+
// @ts-ignore
|
|
244
|
+
const {
|
|
245
|
+
code,
|
|
246
|
+
message,
|
|
247
|
+
stack
|
|
248
|
+
} = getHelpfulChildProcessError(stdout, stderr);
|
|
249
|
+
const cause = new Error(message);
|
|
250
|
+
// @ts-ignore
|
|
251
|
+
cause.code = code;
|
|
252
|
+
if (stack) {
|
|
253
|
+
Object.defineProperty(cause, 'stack', {
|
|
254
|
+
configurable: true,
|
|
255
|
+
enumerable: false,
|
|
256
|
+
value: stack,
|
|
257
|
+
writable: true
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
super(cause, betterMessage);
|
|
261
|
+
} else {
|
|
262
|
+
super(betterMessage);
|
|
263
|
+
}
|
|
264
|
+
// @ts-ignore
|
|
265
|
+
this.name = 'IpcError';
|
|
266
|
+
// @ts-ignore
|
|
267
|
+
this.stdout = stdout;
|
|
268
|
+
// @ts-ignore
|
|
269
|
+
this.stderr = stderr;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
const readyMessage = 'ready';
|
|
273
|
+
const getData$2 = event => {
|
|
274
|
+
return event.data;
|
|
275
|
+
};
|
|
276
|
+
const listen$7 = () => {
|
|
277
|
+
// @ts-ignore
|
|
278
|
+
if (typeof WorkerGlobalScope === 'undefined') {
|
|
279
|
+
throw new TypeError('module is not in web worker scope');
|
|
280
|
+
}
|
|
281
|
+
return globalThis;
|
|
282
|
+
};
|
|
283
|
+
const signal$8 = global => {
|
|
284
|
+
global.postMessage(readyMessage);
|
|
285
|
+
};
|
|
286
|
+
class IpcChildWithModuleWorker extends Ipc {
|
|
287
|
+
getData(event) {
|
|
288
|
+
return getData$2(event);
|
|
289
|
+
}
|
|
290
|
+
send(message) {
|
|
291
|
+
// @ts-ignore
|
|
292
|
+
this._rawIpc.postMessage(message);
|
|
293
|
+
}
|
|
294
|
+
sendAndTransfer(message) {
|
|
295
|
+
const transfer = getTransferrables(message);
|
|
296
|
+
// @ts-ignore
|
|
297
|
+
this._rawIpc.postMessage(message, transfer);
|
|
298
|
+
}
|
|
299
|
+
dispose() {
|
|
300
|
+
// ignore
|
|
301
|
+
}
|
|
302
|
+
onClose(callback) {
|
|
303
|
+
// ignore
|
|
304
|
+
}
|
|
305
|
+
onMessage(callback) {
|
|
306
|
+
this._rawIpc.addEventListener('message', callback);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
const wrap$f = global => {
|
|
310
|
+
return new IpcChildWithModuleWorker(global);
|
|
311
|
+
};
|
|
312
|
+
const waitForFirstMessage = async port => {
|
|
313
|
+
const {
|
|
314
|
+
promise,
|
|
315
|
+
resolve
|
|
316
|
+
} = Promise.withResolvers();
|
|
317
|
+
port.addEventListener('message', resolve, {
|
|
318
|
+
once: true
|
|
319
|
+
});
|
|
320
|
+
const event = await promise;
|
|
321
|
+
// @ts-ignore
|
|
322
|
+
return event.data;
|
|
323
|
+
};
|
|
324
|
+
const listen$6 = async () => {
|
|
325
|
+
const parentIpcRaw = listen$7();
|
|
326
|
+
signal$8(parentIpcRaw);
|
|
327
|
+
const parentIpc = wrap$f(parentIpcRaw);
|
|
328
|
+
const firstMessage = await waitForFirstMessage(parentIpc);
|
|
329
|
+
if (firstMessage.method !== 'initialize') {
|
|
330
|
+
throw new IpcError('unexpected first message');
|
|
331
|
+
}
|
|
332
|
+
const type = firstMessage.params[0];
|
|
333
|
+
if (type === 'message-port') {
|
|
334
|
+
parentIpc.send({
|
|
335
|
+
id: firstMessage.id,
|
|
336
|
+
jsonrpc: '2.0',
|
|
337
|
+
result: null
|
|
338
|
+
});
|
|
339
|
+
parentIpc.dispose();
|
|
340
|
+
const port = firstMessage.params[1];
|
|
341
|
+
return port;
|
|
342
|
+
}
|
|
343
|
+
return globalThis;
|
|
344
|
+
};
|
|
345
|
+
class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
|
|
346
|
+
getData(event) {
|
|
347
|
+
return getData$2(event);
|
|
348
|
+
}
|
|
349
|
+
send(message) {
|
|
350
|
+
this._rawIpc.postMessage(message);
|
|
351
|
+
}
|
|
352
|
+
sendAndTransfer(message) {
|
|
353
|
+
const transfer = getTransferrables(message);
|
|
354
|
+
this._rawIpc.postMessage(message, transfer);
|
|
355
|
+
}
|
|
356
|
+
dispose() {
|
|
357
|
+
if (this._rawIpc.close) {
|
|
358
|
+
this._rawIpc.close();
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
onClose(callback) {
|
|
362
|
+
// ignore
|
|
363
|
+
}
|
|
364
|
+
onMessage(callback) {
|
|
365
|
+
this._rawIpc.addEventListener('message', callback);
|
|
366
|
+
this._rawIpc.start();
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
const wrap$e = port => {
|
|
370
|
+
return new IpcChildWithModuleWorkerAndMessagePort(port);
|
|
371
|
+
};
|
|
372
|
+
const IpcChildWithModuleWorkerAndMessagePort$1 = {
|
|
373
|
+
__proto__: null,
|
|
374
|
+
listen: listen$6,
|
|
375
|
+
wrap: wrap$e
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
class CommandNotFoundError extends Error {
|
|
379
|
+
constructor(command) {
|
|
380
|
+
super(`Command not found ${command}`);
|
|
381
|
+
this.name = 'CommandNotFoundError';
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
const commands = Object.create(null);
|
|
385
|
+
const register = commandMap => {
|
|
386
|
+
Object.assign(commands, commandMap);
|
|
387
|
+
};
|
|
388
|
+
const getCommand = key => {
|
|
389
|
+
return commands[key];
|
|
390
|
+
};
|
|
391
|
+
const execute = (command, ...args) => {
|
|
392
|
+
const fn = getCommand(command);
|
|
393
|
+
if (!fn) {
|
|
394
|
+
throw new CommandNotFoundError(command);
|
|
395
|
+
}
|
|
396
|
+
return fn(...args);
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
const Two$1 = '2.0';
|
|
400
|
+
const callbacks = Object.create(null);
|
|
401
|
+
const get$2 = id => {
|
|
402
|
+
return callbacks[id];
|
|
403
|
+
};
|
|
404
|
+
const remove$1 = id => {
|
|
405
|
+
delete callbacks[id];
|
|
406
|
+
};
|
|
407
|
+
class JsonRpcError extends Error {
|
|
408
|
+
constructor(message) {
|
|
409
|
+
super(message);
|
|
410
|
+
this.name = 'JsonRpcError';
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
const NewLine = '\n';
|
|
414
|
+
const DomException = 'DOMException';
|
|
415
|
+
const ReferenceError$1 = 'ReferenceError';
|
|
416
|
+
const SyntaxError$1 = 'SyntaxError';
|
|
417
|
+
const TypeError$1 = 'TypeError';
|
|
418
|
+
const getErrorConstructor = (message, type) => {
|
|
419
|
+
if (type) {
|
|
420
|
+
switch (type) {
|
|
421
|
+
case DomException:
|
|
422
|
+
return DOMException;
|
|
423
|
+
case ReferenceError$1:
|
|
424
|
+
return ReferenceError;
|
|
425
|
+
case SyntaxError$1:
|
|
426
|
+
return SyntaxError;
|
|
427
|
+
case TypeError$1:
|
|
428
|
+
return TypeError;
|
|
429
|
+
default:
|
|
430
|
+
return Error;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
if (message.startsWith('TypeError: ')) {
|
|
434
|
+
return TypeError;
|
|
435
|
+
}
|
|
436
|
+
if (message.startsWith('SyntaxError: ')) {
|
|
437
|
+
return SyntaxError;
|
|
438
|
+
}
|
|
439
|
+
if (message.startsWith('ReferenceError: ')) {
|
|
440
|
+
return ReferenceError;
|
|
441
|
+
}
|
|
442
|
+
return Error;
|
|
443
|
+
};
|
|
444
|
+
const constructError = (message, type, name) => {
|
|
445
|
+
const ErrorConstructor = getErrorConstructor(message, type);
|
|
446
|
+
if (ErrorConstructor === DOMException && name) {
|
|
447
|
+
return new ErrorConstructor(message, name);
|
|
448
|
+
}
|
|
449
|
+
if (ErrorConstructor === Error) {
|
|
450
|
+
const error = new Error(message);
|
|
451
|
+
if (name && name !== 'VError') {
|
|
452
|
+
Object.defineProperty(error, 'name', {
|
|
453
|
+
configurable: true,
|
|
454
|
+
value: name
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
return error;
|
|
458
|
+
}
|
|
459
|
+
return new ErrorConstructor(message);
|
|
460
|
+
};
|
|
461
|
+
const joinLines = lines => {
|
|
462
|
+
return lines.join(NewLine);
|
|
463
|
+
};
|
|
464
|
+
const splitLines = lines => {
|
|
465
|
+
return lines.split(NewLine);
|
|
466
|
+
};
|
|
467
|
+
const getCurrentStack = () => {
|
|
468
|
+
const stackLinesToSkip = 3;
|
|
469
|
+
const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
|
|
470
|
+
return currentStack;
|
|
471
|
+
};
|
|
472
|
+
const getNewLineIndex = (string, startIndex) => {
|
|
473
|
+
{
|
|
474
|
+
return string.indexOf(NewLine);
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
const getParentStack = error => {
|
|
478
|
+
let parentStack = error.stack || error.data || error.message || '';
|
|
479
|
+
if (parentStack.startsWith(' at')) {
|
|
480
|
+
parentStack = error.message + NewLine + parentStack;
|
|
481
|
+
}
|
|
482
|
+
return parentStack;
|
|
483
|
+
};
|
|
484
|
+
const MethodNotFound = -32601;
|
|
485
|
+
const Custom = -32001;
|
|
486
|
+
const setStack = (error, stack) => {
|
|
487
|
+
const descriptor = Object.getOwnPropertyDescriptor(error, 'stack');
|
|
488
|
+
if (descriptor) {
|
|
489
|
+
if (!descriptor.configurable && !descriptor.writable) {
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
if (!descriptor.configurable && descriptor.writable) {
|
|
493
|
+
error.stack = stack;
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
Object.defineProperty(error, 'stack', {
|
|
498
|
+
configurable: true,
|
|
499
|
+
value: stack,
|
|
500
|
+
writable: true
|
|
501
|
+
});
|
|
502
|
+
};
|
|
503
|
+
const restoreExistingError = (error, currentStack) => {
|
|
504
|
+
if (typeof error.stack === 'string') {
|
|
505
|
+
setStack(error, `${error.stack}${NewLine}${currentStack}`);
|
|
506
|
+
}
|
|
507
|
+
return error;
|
|
508
|
+
};
|
|
509
|
+
const restoreMethodNotFoundError = (error, currentStack) => {
|
|
510
|
+
const restoredError = new JsonRpcError(error.message);
|
|
511
|
+
const parentStack = getParentStack(error);
|
|
512
|
+
setStack(restoredError, `${parentStack}${NewLine}${currentStack}`);
|
|
513
|
+
return restoredError;
|
|
514
|
+
};
|
|
515
|
+
const restoreStackFromData = (restoredError, error, currentStack) => {
|
|
516
|
+
if (error.data.stack && error.data.type && error.message) {
|
|
517
|
+
setStack(restoredError, `${error.data.type}: ${error.message}${NewLine}${error.data.stack}${NewLine}${currentStack}`);
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
if (error.data.stack) {
|
|
521
|
+
setStack(restoredError, error.data.stack);
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
const applyDataProperties = (restoredError, error) => {
|
|
525
|
+
restoreStackFromData(restoredError, error, getCurrentStack());
|
|
526
|
+
if (error.data.codeFrame) {
|
|
527
|
+
// @ts-ignore
|
|
528
|
+
restoredError.codeFrame = error.data.codeFrame;
|
|
529
|
+
}
|
|
530
|
+
if (error.data.code) {
|
|
531
|
+
// @ts-ignore
|
|
532
|
+
restoredError.code = error.data.code;
|
|
533
|
+
}
|
|
534
|
+
if (error.data.type) {
|
|
535
|
+
// @ts-ignore
|
|
536
|
+
restoredError.name = error.data.type;
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
const applyDirectProperties = (restoredError, error) => {
|
|
540
|
+
if (error.stack) {
|
|
541
|
+
const lowerStack = restoredError.stack || '';
|
|
542
|
+
const indexNewLine = getNewLineIndex(lowerStack);
|
|
543
|
+
const parentStack = getParentStack(error);
|
|
544
|
+
// @ts-ignore
|
|
545
|
+
setStack(restoredError, `${parentStack}${lowerStack.slice(indexNewLine)}`);
|
|
546
|
+
}
|
|
547
|
+
if (error.codeFrame) {
|
|
548
|
+
// @ts-ignore
|
|
549
|
+
restoredError.codeFrame = error.codeFrame;
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
const restoreMessageError = (error, _currentStack) => {
|
|
553
|
+
const restoredError = constructError(error.message, error.type, error.name);
|
|
554
|
+
if (error.data) {
|
|
555
|
+
applyDataProperties(restoredError, error);
|
|
556
|
+
} else {
|
|
557
|
+
applyDirectProperties(restoredError, error);
|
|
558
|
+
}
|
|
559
|
+
return restoredError;
|
|
560
|
+
};
|
|
561
|
+
const restoreJsonRpcError = error => {
|
|
562
|
+
const currentStack = getCurrentStack();
|
|
563
|
+
if (error && error instanceof Error) {
|
|
564
|
+
return restoreExistingError(error, currentStack);
|
|
565
|
+
}
|
|
566
|
+
if (error && error.code && error.code === MethodNotFound) {
|
|
567
|
+
return restoreMethodNotFoundError(error, currentStack);
|
|
568
|
+
}
|
|
569
|
+
if (error && error.message) {
|
|
570
|
+
return restoreMessageError(error);
|
|
571
|
+
}
|
|
572
|
+
if (typeof error === 'string') {
|
|
573
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
574
|
+
}
|
|
575
|
+
return new Error(`JsonRpc Error: ${error}`);
|
|
576
|
+
};
|
|
577
|
+
const unwrapJsonRpcResult = responseMessage => {
|
|
578
|
+
if ('error' in responseMessage) {
|
|
579
|
+
const restoredError = restoreJsonRpcError(responseMessage.error);
|
|
580
|
+
throw restoredError;
|
|
581
|
+
}
|
|
582
|
+
if ('result' in responseMessage) {
|
|
583
|
+
return responseMessage.result;
|
|
584
|
+
}
|
|
585
|
+
throw new JsonRpcError('unexpected response message');
|
|
586
|
+
};
|
|
587
|
+
const warn = (...args) => {
|
|
588
|
+
console.warn(...args);
|
|
589
|
+
};
|
|
590
|
+
const resolve = (id, response) => {
|
|
591
|
+
const fn = get$2(id);
|
|
592
|
+
if (!fn) {
|
|
593
|
+
console.log(response);
|
|
594
|
+
warn(`callback ${id} may already be disposed`);
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
fn(response);
|
|
598
|
+
remove$1(id);
|
|
599
|
+
};
|
|
600
|
+
const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
|
|
601
|
+
const getErrorType = prettyError => {
|
|
602
|
+
if (prettyError && prettyError.type) {
|
|
603
|
+
return prettyError.type;
|
|
604
|
+
}
|
|
605
|
+
if (prettyError && prettyError.constructor && prettyError.constructor.name) {
|
|
606
|
+
return prettyError.constructor.name;
|
|
607
|
+
}
|
|
608
|
+
return undefined;
|
|
609
|
+
};
|
|
610
|
+
const isAlreadyStack = line => {
|
|
611
|
+
return line.trim().startsWith('at ');
|
|
612
|
+
};
|
|
613
|
+
const getStack = prettyError => {
|
|
614
|
+
const stackString = prettyError.stack || '';
|
|
615
|
+
const newLineIndex = stackString.indexOf('\n');
|
|
616
|
+
if (newLineIndex !== -1 && !isAlreadyStack(stackString.slice(0, newLineIndex))) {
|
|
617
|
+
return stackString.slice(newLineIndex + 1);
|
|
618
|
+
}
|
|
619
|
+
return stackString;
|
|
620
|
+
};
|
|
621
|
+
const getErrorProperty = (error, prettyError) => {
|
|
622
|
+
if (error && error.code === E_COMMAND_NOT_FOUND) {
|
|
623
|
+
return {
|
|
624
|
+
code: MethodNotFound,
|
|
625
|
+
data: error.stack,
|
|
626
|
+
message: error.message
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
return {
|
|
630
|
+
code: Custom,
|
|
631
|
+
data: {
|
|
632
|
+
code: prettyError.code,
|
|
633
|
+
codeFrame: prettyError.codeFrame,
|
|
634
|
+
name: prettyError.name,
|
|
635
|
+
stack: getStack(prettyError),
|
|
636
|
+
type: getErrorType(prettyError)
|
|
637
|
+
},
|
|
638
|
+
message: prettyError.message
|
|
639
|
+
};
|
|
640
|
+
};
|
|
641
|
+
const create$1$1 = (id, error) => {
|
|
642
|
+
return {
|
|
643
|
+
error,
|
|
644
|
+
id,
|
|
645
|
+
jsonrpc: Two$1
|
|
646
|
+
};
|
|
647
|
+
};
|
|
648
|
+
const getErrorResponse = (id, error, preparePrettyError, logError) => {
|
|
649
|
+
const prettyError = preparePrettyError(error);
|
|
650
|
+
logError(error, prettyError);
|
|
651
|
+
const errorProperty = getErrorProperty(error, prettyError);
|
|
652
|
+
return create$1$1(id, errorProperty);
|
|
653
|
+
};
|
|
654
|
+
const create$7 = (message, result) => {
|
|
655
|
+
return {
|
|
656
|
+
id: message.id,
|
|
657
|
+
jsonrpc: Two$1,
|
|
658
|
+
result: result ?? null
|
|
659
|
+
};
|
|
660
|
+
};
|
|
661
|
+
const getSuccessResponse = (message, result) => {
|
|
662
|
+
const resultProperty = result ?? null;
|
|
663
|
+
return create$7(message, resultProperty);
|
|
664
|
+
};
|
|
665
|
+
const getErrorResponseSimple = (id, error) => {
|
|
666
|
+
return {
|
|
667
|
+
error: {
|
|
668
|
+
code: Custom,
|
|
669
|
+
data: error,
|
|
670
|
+
// @ts-ignore
|
|
671
|
+
message: error.message
|
|
672
|
+
},
|
|
673
|
+
id,
|
|
674
|
+
jsonrpc: Two$1
|
|
675
|
+
};
|
|
676
|
+
};
|
|
677
|
+
const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
|
|
678
|
+
try {
|
|
679
|
+
const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
|
|
680
|
+
return getSuccessResponse(message, result);
|
|
681
|
+
} catch (error) {
|
|
682
|
+
if (ipc.canUseSimpleErrorResponse) {
|
|
683
|
+
return getErrorResponseSimple(message.id, error);
|
|
684
|
+
}
|
|
685
|
+
return getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
686
|
+
}
|
|
687
|
+
};
|
|
688
|
+
const defaultPreparePrettyError = error => {
|
|
689
|
+
return error;
|
|
690
|
+
};
|
|
691
|
+
const defaultLogError = () => {
|
|
692
|
+
// ignore
|
|
693
|
+
};
|
|
694
|
+
const defaultRequiresSocket = () => {
|
|
695
|
+
return false;
|
|
696
|
+
};
|
|
697
|
+
const defaultResolve = resolve;
|
|
698
|
+
|
|
699
|
+
// TODO maybe remove this in v6 or v7, only accept options object to simplify the code
|
|
700
|
+
const normalizeParams = args => {
|
|
701
|
+
if (args.length === 1) {
|
|
702
|
+
const options = args[0];
|
|
703
|
+
return {
|
|
704
|
+
execute: options.execute,
|
|
705
|
+
ipc: options.ipc,
|
|
706
|
+
logError: options.logError || defaultLogError,
|
|
707
|
+
message: options.message,
|
|
708
|
+
preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
|
|
709
|
+
requiresSocket: options.requiresSocket || defaultRequiresSocket,
|
|
710
|
+
resolve: options.resolve || defaultResolve
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
return {
|
|
714
|
+
execute: args[2],
|
|
715
|
+
ipc: args[0],
|
|
716
|
+
logError: args[5],
|
|
717
|
+
message: args[1],
|
|
718
|
+
preparePrettyError: args[4],
|
|
719
|
+
requiresSocket: args[6],
|
|
720
|
+
resolve: args[3]
|
|
721
|
+
};
|
|
722
|
+
};
|
|
723
|
+
const handleJsonRpcMessage = async (...args) => {
|
|
724
|
+
const options = normalizeParams(args);
|
|
725
|
+
const {
|
|
726
|
+
execute,
|
|
727
|
+
ipc,
|
|
728
|
+
logError,
|
|
729
|
+
message,
|
|
730
|
+
preparePrettyError,
|
|
731
|
+
requiresSocket,
|
|
732
|
+
resolve
|
|
733
|
+
} = options;
|
|
734
|
+
if ('id' in message) {
|
|
735
|
+
if ('method' in message) {
|
|
736
|
+
const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
737
|
+
try {
|
|
738
|
+
ipc.send(response);
|
|
739
|
+
} catch (error) {
|
|
740
|
+
const errorResponse = getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
741
|
+
ipc.send(errorResponse);
|
|
742
|
+
}
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
resolve(message.id, message);
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
if ('method' in message) {
|
|
749
|
+
await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
throw new JsonRpcError('unexpected message');
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
const Two = '2.0';
|
|
756
|
+
|
|
757
|
+
const create$6 = (method, params) => {
|
|
758
|
+
return {
|
|
759
|
+
jsonrpc: Two,
|
|
760
|
+
method,
|
|
761
|
+
params
|
|
762
|
+
};
|
|
763
|
+
};
|
|
764
|
+
|
|
765
|
+
const create$5 = (id, method, params) => {
|
|
766
|
+
const message = {
|
|
767
|
+
id,
|
|
768
|
+
jsonrpc: Two,
|
|
769
|
+
method,
|
|
770
|
+
params
|
|
771
|
+
};
|
|
772
|
+
return message;
|
|
773
|
+
};
|
|
774
|
+
|
|
775
|
+
let id = 0;
|
|
776
|
+
const create$4 = () => {
|
|
777
|
+
return ++id;
|
|
778
|
+
};
|
|
779
|
+
|
|
780
|
+
const registerPromise = map => {
|
|
781
|
+
const id = create$4();
|
|
782
|
+
const {
|
|
783
|
+
promise,
|
|
784
|
+
resolve
|
|
785
|
+
} = Promise.withResolvers();
|
|
786
|
+
map[id] = resolve;
|
|
787
|
+
return {
|
|
788
|
+
id,
|
|
789
|
+
promise
|
|
790
|
+
};
|
|
791
|
+
};
|
|
792
|
+
|
|
793
|
+
const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer) => {
|
|
794
|
+
const {
|
|
795
|
+
id,
|
|
796
|
+
promise
|
|
797
|
+
} = registerPromise(callbacks);
|
|
798
|
+
const message = create$5(id, method, params);
|
|
799
|
+
if (useSendAndTransfer && ipc.sendAndTransfer) {
|
|
800
|
+
ipc.sendAndTransfer(message);
|
|
801
|
+
} else {
|
|
802
|
+
ipc.send(message);
|
|
803
|
+
}
|
|
804
|
+
const responseMessage = await promise;
|
|
805
|
+
return unwrapJsonRpcResult(responseMessage);
|
|
806
|
+
};
|
|
807
|
+
const createRpc = ipc => {
|
|
808
|
+
const callbacks = Object.create(null);
|
|
809
|
+
ipc._resolve = (id, response) => {
|
|
810
|
+
const fn = callbacks[id];
|
|
811
|
+
if (!fn) {
|
|
812
|
+
console.warn(`callback ${id} may already be disposed`);
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
fn(response);
|
|
816
|
+
delete callbacks[id];
|
|
817
|
+
};
|
|
818
|
+
const rpc = {
|
|
819
|
+
async dispose() {
|
|
820
|
+
await ipc?.dispose();
|
|
821
|
+
},
|
|
822
|
+
invoke(method, ...params) {
|
|
823
|
+
return invokeHelper(callbacks, ipc, method, params, false);
|
|
824
|
+
},
|
|
825
|
+
invokeAndTransfer(method, ...params) {
|
|
826
|
+
return invokeHelper(callbacks, ipc, method, params, true);
|
|
827
|
+
},
|
|
828
|
+
// @ts-ignore
|
|
829
|
+
ipc,
|
|
830
|
+
/**
|
|
831
|
+
* @deprecated
|
|
832
|
+
*/
|
|
833
|
+
send(method, ...params) {
|
|
834
|
+
const message = create$6(method, params);
|
|
835
|
+
ipc.send(message);
|
|
836
|
+
}
|
|
837
|
+
};
|
|
838
|
+
return rpc;
|
|
839
|
+
};
|
|
840
|
+
|
|
841
|
+
const requiresSocket = () => {
|
|
842
|
+
return false;
|
|
843
|
+
};
|
|
844
|
+
const preparePrettyError = error => {
|
|
845
|
+
return error;
|
|
846
|
+
};
|
|
847
|
+
const logError = () => {
|
|
848
|
+
// handled by renderer worker
|
|
849
|
+
};
|
|
850
|
+
const handleMessage = event => {
|
|
851
|
+
const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
|
|
852
|
+
const actualExecute = event?.target?.execute || execute;
|
|
853
|
+
return handleJsonRpcMessage(event.target, event.data, actualExecute, event.target._resolve, preparePrettyError, logError, actualRequiresSocket);
|
|
854
|
+
};
|
|
855
|
+
|
|
856
|
+
const handleIpc = ipc => {
|
|
857
|
+
if ('addEventListener' in ipc) {
|
|
858
|
+
ipc.addEventListener('message', handleMessage);
|
|
859
|
+
} else if ('on' in ipc) {
|
|
860
|
+
// deprecated
|
|
861
|
+
ipc.on('message', handleMessage);
|
|
862
|
+
}
|
|
863
|
+
};
|
|
864
|
+
|
|
865
|
+
const listen$1 = async (module, options) => {
|
|
866
|
+
const rawIpc = await module.listen(options);
|
|
867
|
+
if (module.signal) {
|
|
868
|
+
module.signal(rawIpc);
|
|
869
|
+
}
|
|
870
|
+
const ipc = module.wrap(rawIpc);
|
|
871
|
+
return ipc;
|
|
872
|
+
};
|
|
873
|
+
|
|
874
|
+
const create$3 = async ({
|
|
875
|
+
commandMap
|
|
876
|
+
}) => {
|
|
877
|
+
// TODO create a commandMap per rpc instance
|
|
878
|
+
register(commandMap);
|
|
879
|
+
const ipc = await listen$1(IpcChildWithModuleWorkerAndMessagePort$1);
|
|
880
|
+
handleIpc(ipc);
|
|
881
|
+
const rpc = createRpc(ipc);
|
|
882
|
+
return rpc;
|
|
883
|
+
};
|
|
884
|
+
|
|
885
|
+
const createMockRpc = ({
|
|
886
|
+
commandMap
|
|
887
|
+
}) => {
|
|
888
|
+
const invocations = [];
|
|
889
|
+
const invoke = (method, ...params) => {
|
|
890
|
+
invocations.push([method, ...params]);
|
|
891
|
+
const command = commandMap[method];
|
|
892
|
+
if (!command) {
|
|
893
|
+
throw new Error(`command ${method} not found`);
|
|
894
|
+
}
|
|
895
|
+
return command(...params);
|
|
896
|
+
};
|
|
897
|
+
const mockRpc = {
|
|
898
|
+
invocations,
|
|
899
|
+
invoke,
|
|
900
|
+
invokeAndTransfer: invoke
|
|
901
|
+
};
|
|
902
|
+
return mockRpc;
|
|
903
|
+
};
|
|
904
|
+
|
|
905
|
+
const rpcs = Object.create(null);
|
|
906
|
+
const set$2 = (id, rpc) => {
|
|
907
|
+
rpcs[id] = rpc;
|
|
908
|
+
};
|
|
909
|
+
const get$1 = id => {
|
|
910
|
+
return rpcs[id];
|
|
911
|
+
};
|
|
912
|
+
const remove = id => {
|
|
913
|
+
delete rpcs[id];
|
|
914
|
+
};
|
|
915
|
+
|
|
916
|
+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
|
917
|
+
const create$2 = rpcId => {
|
|
918
|
+
return {
|
|
919
|
+
async dispose() {
|
|
920
|
+
const rpc = get$1(rpcId);
|
|
921
|
+
await rpc.dispose();
|
|
922
|
+
},
|
|
923
|
+
// @ts-ignore
|
|
924
|
+
invoke(method, ...params) {
|
|
925
|
+
const rpc = get$1(rpcId);
|
|
926
|
+
// @ts-ignore
|
|
927
|
+
return rpc.invoke(method, ...params);
|
|
928
|
+
},
|
|
929
|
+
// @ts-ignore
|
|
930
|
+
invokeAndTransfer(method, ...params) {
|
|
931
|
+
const rpc = get$1(rpcId);
|
|
932
|
+
// @ts-ignore
|
|
933
|
+
return rpc.invokeAndTransfer(method, ...params);
|
|
934
|
+
},
|
|
935
|
+
registerMockRpc(commandMap) {
|
|
936
|
+
const mockRpc = createMockRpc({
|
|
937
|
+
commandMap
|
|
938
|
+
});
|
|
939
|
+
set$2(rpcId, mockRpc);
|
|
940
|
+
// @ts-ignore
|
|
941
|
+
mockRpc[Symbol.dispose] = () => {
|
|
942
|
+
remove(rpcId);
|
|
943
|
+
};
|
|
944
|
+
// @ts-ignore
|
|
945
|
+
return mockRpc;
|
|
946
|
+
},
|
|
947
|
+
set(rpc) {
|
|
948
|
+
set$2(rpcId, rpc);
|
|
949
|
+
}
|
|
950
|
+
};
|
|
951
|
+
};
|
|
952
|
+
|
|
953
|
+
const File = 7;
|
|
954
|
+
|
|
955
|
+
const Button = 1;
|
|
956
|
+
const Div = 4;
|
|
957
|
+
const Span = 8;
|
|
958
|
+
const Text = 12;
|
|
959
|
+
const H2 = 22;
|
|
960
|
+
const Strong = 70;
|
|
961
|
+
|
|
962
|
+
const RendererWorker = 1;
|
|
963
|
+
|
|
964
|
+
const SetDom2 = 'Viewlet.setDom2';
|
|
965
|
+
|
|
966
|
+
const {
|
|
967
|
+
invoke,
|
|
968
|
+
set: set$1
|
|
969
|
+
} = create$2(RendererWorker);
|
|
970
|
+
|
|
971
|
+
const toCommandId = key => {
|
|
972
|
+
const dotIndex = key.indexOf('.');
|
|
973
|
+
return key.slice(dotIndex + 1);
|
|
974
|
+
};
|
|
975
|
+
const create$1 = () => {
|
|
976
|
+
const commandQueues = new Map();
|
|
977
|
+
const generations = Object.create(null);
|
|
978
|
+
const states = Object.create(null);
|
|
979
|
+
const commandMapRef = Object.create(null);
|
|
980
|
+
const commandsById = Object.create(null);
|
|
981
|
+
const getGeneration = uid => generations[uid] || 0;
|
|
982
|
+
const isCurrentGeneration = (uid, generation) => {
|
|
983
|
+
return states[uid] !== undefined && getGeneration(uid) === generation;
|
|
984
|
+
};
|
|
985
|
+
const updateState = (uid, generation, fallbackState, updater) => {
|
|
986
|
+
if (!isCurrentGeneration(uid, generation)) {
|
|
987
|
+
return Promise.resolve(fallbackState);
|
|
988
|
+
}
|
|
989
|
+
const current = states[uid];
|
|
990
|
+
const updatedState = updater(current.newState);
|
|
991
|
+
if (updatedState !== current.newState) {
|
|
992
|
+
states[uid] = {
|
|
993
|
+
newState: updatedState,
|
|
994
|
+
oldState: current.oldState,
|
|
995
|
+
scheduledState: updatedState
|
|
996
|
+
};
|
|
997
|
+
}
|
|
998
|
+
return Promise.resolve(updatedState);
|
|
999
|
+
};
|
|
1000
|
+
const createAsyncCommandContext = (uid, generation) => {
|
|
1001
|
+
let latestState = states[uid].newState;
|
|
1002
|
+
return {
|
|
1003
|
+
getState: () => {
|
|
1004
|
+
if (isCurrentGeneration(uid, generation)) {
|
|
1005
|
+
latestState = states[uid].newState;
|
|
1006
|
+
}
|
|
1007
|
+
return latestState;
|
|
1008
|
+
},
|
|
1009
|
+
updateState: async updater => {
|
|
1010
|
+
latestState = await updateState(uid, generation, latestState, updater);
|
|
1011
|
+
return latestState;
|
|
1012
|
+
}
|
|
1013
|
+
};
|
|
1014
|
+
};
|
|
1015
|
+
const enqueueCommand = async (uid, command) => {
|
|
1016
|
+
const previous = commandQueues.get(uid) || Promise.resolve();
|
|
1017
|
+
const run = async () => {
|
|
1018
|
+
try {
|
|
1019
|
+
await previous;
|
|
1020
|
+
} catch {
|
|
1021
|
+
// The previous caller receives its error; later commands must still run.
|
|
1022
|
+
}
|
|
1023
|
+
await command();
|
|
1024
|
+
};
|
|
1025
|
+
const current = run();
|
|
1026
|
+
commandQueues.set(uid, current);
|
|
1027
|
+
try {
|
|
1028
|
+
await current;
|
|
1029
|
+
} finally {
|
|
1030
|
+
if (commandQueues.get(uid) === current) {
|
|
1031
|
+
commandQueues.delete(uid);
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
};
|
|
1035
|
+
return {
|
|
1036
|
+
clear() {
|
|
1037
|
+
commandQueues.clear();
|
|
1038
|
+
for (const key of Object.keys(states)) {
|
|
1039
|
+
delete states[key];
|
|
1040
|
+
}
|
|
1041
|
+
},
|
|
1042
|
+
createDirectEventCommandMap(requestRender) {
|
|
1043
|
+
return {
|
|
1044
|
+
async 'Viewlet.executeViewletCommand'(uid, command, ...args) {
|
|
1045
|
+
const fn = commandsById[command];
|
|
1046
|
+
if (!fn) {
|
|
1047
|
+
throw new Error(`Viewlet command not found: ${command}`);
|
|
1048
|
+
}
|
|
1049
|
+
await fn(uid, ...args);
|
|
1050
|
+
await requestRender(uid);
|
|
1051
|
+
}
|
|
1052
|
+
};
|
|
1053
|
+
},
|
|
1054
|
+
diff(uid, modules, numbers) {
|
|
1055
|
+
const {
|
|
1056
|
+
oldState,
|
|
1057
|
+
scheduledState
|
|
1058
|
+
} = states[uid];
|
|
1059
|
+
const diffResult = [];
|
|
1060
|
+
for (let i = 0; i < modules.length; i++) {
|
|
1061
|
+
const fn = modules[i];
|
|
1062
|
+
if (!fn(oldState, scheduledState)) {
|
|
1063
|
+
diffResult.push(numbers[i]);
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
return diffResult;
|
|
1067
|
+
},
|
|
1068
|
+
dispose(uid) {
|
|
1069
|
+
commandQueues.delete(uid);
|
|
1070
|
+
delete states[uid];
|
|
1071
|
+
},
|
|
1072
|
+
get(uid) {
|
|
1073
|
+
return states[uid];
|
|
1074
|
+
},
|
|
1075
|
+
getCommandIds() {
|
|
1076
|
+
const keys = Object.keys(commandMapRef);
|
|
1077
|
+
const ids = keys.map(toCommandId);
|
|
1078
|
+
return ids;
|
|
1079
|
+
},
|
|
1080
|
+
getKeys() {
|
|
1081
|
+
return Object.keys(states).map(Number);
|
|
1082
|
+
},
|
|
1083
|
+
registerCommands(commandMap) {
|
|
1084
|
+
Object.assign(commandMapRef, commandMap);
|
|
1085
|
+
for (const [key, fn] of Object.entries(commandMap)) {
|
|
1086
|
+
commandsById[toCommandId(key)] = fn;
|
|
1087
|
+
}
|
|
1088
|
+
},
|
|
1089
|
+
set(uid, oldState, newState, scheduledState) {
|
|
1090
|
+
const current = states[uid];
|
|
1091
|
+
if (!current || oldState === newState && newState !== current.newState) {
|
|
1092
|
+
generations[uid] = getGeneration(uid) + 1;
|
|
1093
|
+
}
|
|
1094
|
+
states[uid] = {
|
|
1095
|
+
newState,
|
|
1096
|
+
oldState,
|
|
1097
|
+
scheduledState: scheduledState ?? newState
|
|
1098
|
+
};
|
|
1099
|
+
},
|
|
1100
|
+
wrapAsyncCommand(fn) {
|
|
1101
|
+
const wrapped = async (uid, ...args) => {
|
|
1102
|
+
const generation = getGeneration(uid);
|
|
1103
|
+
const context = createAsyncCommandContext(uid, generation);
|
|
1104
|
+
await fn(context, ...args);
|
|
1105
|
+
};
|
|
1106
|
+
return wrapped;
|
|
1107
|
+
},
|
|
1108
|
+
wrapCommand(fn) {
|
|
1109
|
+
const wrapped = async (uid, ...args) => {
|
|
1110
|
+
const generation = getGeneration(uid);
|
|
1111
|
+
const {
|
|
1112
|
+
newState,
|
|
1113
|
+
oldState
|
|
1114
|
+
} = states[uid];
|
|
1115
|
+
const newerState = await fn(newState, ...args);
|
|
1116
|
+
if (oldState === newerState || newState === newerState) {
|
|
1117
|
+
return;
|
|
1118
|
+
}
|
|
1119
|
+
if (!isCurrentGeneration(uid, generation)) {
|
|
1120
|
+
return;
|
|
1121
|
+
}
|
|
1122
|
+
const latestOld = states[uid];
|
|
1123
|
+
const latestNew = {
|
|
1124
|
+
...latestOld.newState,
|
|
1125
|
+
...newerState
|
|
1126
|
+
};
|
|
1127
|
+
states[uid] = {
|
|
1128
|
+
newState: latestNew,
|
|
1129
|
+
oldState: latestOld.oldState,
|
|
1130
|
+
scheduledState: latestNew
|
|
1131
|
+
};
|
|
1132
|
+
};
|
|
1133
|
+
return wrapped;
|
|
1134
|
+
},
|
|
1135
|
+
wrapGetter(fn) {
|
|
1136
|
+
const wrapped = (uid, ...args) => {
|
|
1137
|
+
const {
|
|
1138
|
+
newState
|
|
1139
|
+
} = states[uid];
|
|
1140
|
+
return fn(newState, ...args);
|
|
1141
|
+
};
|
|
1142
|
+
return wrapped;
|
|
1143
|
+
},
|
|
1144
|
+
wrapLoadContent(fn) {
|
|
1145
|
+
const wrapped = async (uid, ...args) => {
|
|
1146
|
+
const generation = getGeneration(uid);
|
|
1147
|
+
const {
|
|
1148
|
+
newState,
|
|
1149
|
+
oldState
|
|
1150
|
+
} = states[uid];
|
|
1151
|
+
const result = await fn(newState, ...args);
|
|
1152
|
+
const {
|
|
1153
|
+
error,
|
|
1154
|
+
state
|
|
1155
|
+
} = result;
|
|
1156
|
+
if (oldState === state || newState === state) {
|
|
1157
|
+
return {
|
|
1158
|
+
error
|
|
1159
|
+
};
|
|
1160
|
+
}
|
|
1161
|
+
if (!isCurrentGeneration(uid, generation)) {
|
|
1162
|
+
return {
|
|
1163
|
+
error
|
|
1164
|
+
};
|
|
1165
|
+
}
|
|
1166
|
+
const latestOld = states[uid];
|
|
1167
|
+
const latestNew = {
|
|
1168
|
+
...latestOld.newState,
|
|
1169
|
+
...state
|
|
1170
|
+
};
|
|
1171
|
+
states[uid] = {
|
|
1172
|
+
newState: latestNew,
|
|
1173
|
+
oldState: latestOld.oldState,
|
|
1174
|
+
scheduledState: latestNew
|
|
1175
|
+
};
|
|
1176
|
+
return {
|
|
1177
|
+
error
|
|
1178
|
+
};
|
|
1179
|
+
};
|
|
1180
|
+
return wrapped;
|
|
1181
|
+
},
|
|
1182
|
+
wrapSerialAsyncCommand(fn) {
|
|
1183
|
+
const wrapped = async (uid, ...args) => {
|
|
1184
|
+
await enqueueCommand(uid, async () => {
|
|
1185
|
+
if (!states[uid]) {
|
|
1186
|
+
return;
|
|
1187
|
+
}
|
|
1188
|
+
const generation = getGeneration(uid);
|
|
1189
|
+
const context = createAsyncCommandContext(uid, generation);
|
|
1190
|
+
await fn(context, ...args);
|
|
1191
|
+
});
|
|
1192
|
+
};
|
|
1193
|
+
return wrapped;
|
|
1194
|
+
},
|
|
1195
|
+
wrapSerialCommand(fn) {
|
|
1196
|
+
const wrapped = async (uid, ...args) => {
|
|
1197
|
+
await enqueueCommand(uid, async () => {
|
|
1198
|
+
if (!states[uid]) {
|
|
1199
|
+
return;
|
|
1200
|
+
}
|
|
1201
|
+
const generation = getGeneration(uid);
|
|
1202
|
+
const {
|
|
1203
|
+
newState,
|
|
1204
|
+
oldState
|
|
1205
|
+
} = states[uid];
|
|
1206
|
+
const newerState = await fn(newState, ...args);
|
|
1207
|
+
if (oldState === newerState || newState === newerState) {
|
|
1208
|
+
return;
|
|
1209
|
+
}
|
|
1210
|
+
if (!isCurrentGeneration(uid, generation)) {
|
|
1211
|
+
return;
|
|
1212
|
+
}
|
|
1213
|
+
const latestOld = states[uid];
|
|
1214
|
+
const latestNew = {
|
|
1215
|
+
...latestOld.newState,
|
|
1216
|
+
...newerState
|
|
1217
|
+
};
|
|
1218
|
+
states[uid] = {
|
|
1219
|
+
newState: latestNew,
|
|
1220
|
+
oldState: latestOld.oldState,
|
|
1221
|
+
scheduledState: latestNew
|
|
1222
|
+
};
|
|
1223
|
+
});
|
|
1224
|
+
};
|
|
1225
|
+
return wrapped;
|
|
1226
|
+
}
|
|
1227
|
+
};
|
|
1228
|
+
};
|
|
1229
|
+
|
|
1230
|
+
const {
|
|
1231
|
+
dispose,
|
|
1232
|
+
get,
|
|
1233
|
+
getCommandIds,
|
|
1234
|
+
registerCommands: registerCommands$1,
|
|
1235
|
+
set,
|
|
1236
|
+
wrapCommand
|
|
1237
|
+
} = create$1();
|
|
1238
|
+
|
|
1239
|
+
const create = (uid, x, y, width, height) => {
|
|
1240
|
+
const state = {
|
|
1241
|
+
components: [],
|
|
1242
|
+
height,
|
|
1243
|
+
loaded: false,
|
|
1244
|
+
uid,
|
|
1245
|
+
width,
|
|
1246
|
+
x,
|
|
1247
|
+
y
|
|
1248
|
+
};
|
|
1249
|
+
set(uid, state, state);
|
|
1250
|
+
};
|
|
1251
|
+
|
|
1252
|
+
const diff2 = uid => {
|
|
1253
|
+
const {
|
|
1254
|
+
oldState,
|
|
1255
|
+
scheduledState
|
|
1256
|
+
} = get(uid);
|
|
1257
|
+
return oldState.components === scheduledState.components && oldState.loaded === scheduledState.loaded ? [] : [1];
|
|
1258
|
+
};
|
|
1259
|
+
|
|
1260
|
+
const pattern = /^live-component-state:\/\/\/(\d+)\.json$/;
|
|
1261
|
+
const getUid = uri => {
|
|
1262
|
+
const match = pattern.exec(uri);
|
|
1263
|
+
if (!match) {
|
|
1264
|
+
throw new Error(`Invalid live component state URI: ${uri}`);
|
|
1265
|
+
}
|
|
1266
|
+
return Number(match[1]);
|
|
1267
|
+
};
|
|
1268
|
+
const toUri = uid => `live-component-state:///${uid}.json`;
|
|
1269
|
+
|
|
1270
|
+
const assertObject = value => {
|
|
1271
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
1272
|
+
throw new TypeError('Component state must be a JSON object');
|
|
1273
|
+
}
|
|
1274
|
+
};
|
|
1275
|
+
const readFile = async uri => {
|
|
1276
|
+
const uid = getUid(uri);
|
|
1277
|
+
const state = await invoke('ComponentState.getState', uid);
|
|
1278
|
+
return `${JSON.stringify(state, null, 2)}\n`;
|
|
1279
|
+
};
|
|
1280
|
+
const writeFile = async (uri, content) => {
|
|
1281
|
+
const uid = getUid(uri);
|
|
1282
|
+
const state = JSON.parse(content);
|
|
1283
|
+
assertObject(state);
|
|
1284
|
+
await invoke('ComponentState.setState', uid, state);
|
|
1285
|
+
};
|
|
1286
|
+
const readDirWithFileTypes = async () => {
|
|
1287
|
+
const components = await invoke('ComponentState.getComponents');
|
|
1288
|
+
return components.filter(component => component.editable).map(component => ({
|
|
1289
|
+
name: `${component.uid}.json`,
|
|
1290
|
+
type: File
|
|
1291
|
+
}));
|
|
1292
|
+
};
|
|
1293
|
+
const isReadonly = () => false;
|
|
1294
|
+
const exists = async uri => {
|
|
1295
|
+
const uid = getUid(uri);
|
|
1296
|
+
const components = await invoke('ComponentState.getComponents');
|
|
1297
|
+
return components.some(component => component.uid === uid && component.editable);
|
|
1298
|
+
};
|
|
1299
|
+
|
|
1300
|
+
const handleClick = async (state, uid) => {
|
|
1301
|
+
await invoke('Main.openUri', toUri(Number(uid)));
|
|
1302
|
+
return state;
|
|
1303
|
+
};
|
|
1304
|
+
|
|
1305
|
+
const loadContent = async state => {
|
|
1306
|
+
const components = await invoke('ComponentState.getComponents');
|
|
1307
|
+
return {
|
|
1308
|
+
...state,
|
|
1309
|
+
components,
|
|
1310
|
+
loaded: true
|
|
1311
|
+
};
|
|
1312
|
+
};
|
|
1313
|
+
|
|
1314
|
+
const text = data => {
|
|
1315
|
+
return {
|
|
1316
|
+
childCount: 0,
|
|
1317
|
+
text: data,
|
|
1318
|
+
type: Text
|
|
1319
|
+
};
|
|
1320
|
+
};
|
|
1321
|
+
|
|
1322
|
+
const getCard = component => {
|
|
1323
|
+
const status = component.editable ? 'Open JSON state' : 'State API unavailable';
|
|
1324
|
+
return [{
|
|
1325
|
+
childCount: 3,
|
|
1326
|
+
className: 'ComponentStateCard',
|
|
1327
|
+
'data-uid': String(component.uid),
|
|
1328
|
+
disabled: !component.editable,
|
|
1329
|
+
onClick: 1,
|
|
1330
|
+
type: Button
|
|
1331
|
+
}, {
|
|
1332
|
+
childCount: 1,
|
|
1333
|
+
className: 'ComponentStateCardTitle',
|
|
1334
|
+
type: Strong
|
|
1335
|
+
}, text(component.moduleId), {
|
|
1336
|
+
childCount: 1,
|
|
1337
|
+
className: 'ComponentStateCardUid',
|
|
1338
|
+
type: Span
|
|
1339
|
+
}, text(`uid ${component.uid}`), {
|
|
1340
|
+
childCount: 1,
|
|
1341
|
+
className: 'ComponentStateCardStatus',
|
|
1342
|
+
type: Span
|
|
1343
|
+
}, text(status)];
|
|
1344
|
+
};
|
|
1345
|
+
const getComponentStateVirtualDom = (components, loaded) => {
|
|
1346
|
+
const description = loaded ? `${components.length} live components` : 'Loading live components…';
|
|
1347
|
+
return [{
|
|
1348
|
+
childCount: 3,
|
|
1349
|
+
className: 'ComponentStateView',
|
|
1350
|
+
type: Div
|
|
1351
|
+
}, {
|
|
1352
|
+
childCount: 1,
|
|
1353
|
+
className: 'ComponentStateHeading',
|
|
1354
|
+
type: H2
|
|
1355
|
+
}, text('Live Component State'), {
|
|
1356
|
+
childCount: 1,
|
|
1357
|
+
className: 'ComponentStateDescription',
|
|
1358
|
+
type: Div
|
|
1359
|
+
}, text(description), {
|
|
1360
|
+
childCount: components.length,
|
|
1361
|
+
className: 'ComponentStateGrid',
|
|
1362
|
+
type: Div
|
|
1363
|
+
}, ...components.flatMap(getCard)];
|
|
1364
|
+
};
|
|
1365
|
+
|
|
1366
|
+
const render2 = (uid, diffResult) => {
|
|
1367
|
+
const {
|
|
1368
|
+
newState
|
|
1369
|
+
} = get(uid);
|
|
1370
|
+
set(uid, newState, newState);
|
|
1371
|
+
if (diffResult.length === 0) {
|
|
1372
|
+
return [];
|
|
1373
|
+
}
|
|
1374
|
+
return [[SetDom2, uid, getComponentStateVirtualDom(newState.components, newState.loaded)]];
|
|
1375
|
+
};
|
|
1376
|
+
|
|
1377
|
+
const renderEventListeners = () => [{
|
|
1378
|
+
name: 1,
|
|
1379
|
+
params: ['handleClick', 'event.currentTarget.dataset.uid'],
|
|
1380
|
+
preventDefault: true
|
|
1381
|
+
}];
|
|
1382
|
+
|
|
1383
|
+
const resize = (state, dimensions) => ({
|
|
1384
|
+
...state,
|
|
1385
|
+
...dimensions
|
|
1386
|
+
});
|
|
1387
|
+
|
|
1388
|
+
const viewCommandMap = {
|
|
1389
|
+
'ComponentState.handleClick': wrapCommand(handleClick)
|
|
1390
|
+
};
|
|
1391
|
+
const commandMap = {
|
|
1392
|
+
'ComponentState.create': create,
|
|
1393
|
+
'ComponentState.diff2': diff2,
|
|
1394
|
+
'ComponentState.dispose': dispose,
|
|
1395
|
+
'ComponentState.exists': exists,
|
|
1396
|
+
'ComponentState.getCommandIds': getCommandIds,
|
|
1397
|
+
...viewCommandMap,
|
|
1398
|
+
'ComponentState.isReadonly': isReadonly,
|
|
1399
|
+
'ComponentState.loadContent': wrapCommand(loadContent),
|
|
1400
|
+
'ComponentState.readDirWithFileTypes': readDirWithFileTypes,
|
|
1401
|
+
'ComponentState.readFile': readFile,
|
|
1402
|
+
'ComponentState.render2': render2,
|
|
1403
|
+
'ComponentState.renderEventListeners': renderEventListeners,
|
|
1404
|
+
'ComponentState.resize': wrapCommand(resize),
|
|
1405
|
+
'ComponentState.writeFile': writeFile
|
|
1406
|
+
};
|
|
1407
|
+
|
|
1408
|
+
const initializeRendererWorker = async () => {
|
|
1409
|
+
const rpc = await create$3({
|
|
1410
|
+
commandMap: commandMap
|
|
1411
|
+
});
|
|
1412
|
+
set$1(rpc);
|
|
1413
|
+
};
|
|
1414
|
+
|
|
1415
|
+
const registerCommands = () => {
|
|
1416
|
+
registerCommands$1(viewCommandMap);
|
|
1417
|
+
};
|
|
1418
|
+
|
|
1419
|
+
const listen = async () => {
|
|
1420
|
+
registerCommands();
|
|
1421
|
+
await initializeRendererWorker();
|
|
1422
|
+
};
|
|
1423
|
+
|
|
1424
|
+
const main = async () => {
|
|
1425
|
+
await listen();
|
|
1426
|
+
};
|
|
1427
|
+
|
|
1428
|
+
main();
|