@lvce-editor/editor-worker 19.25.0 → 19.26.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.
@@ -1,248 +1,58 @@
1
- const toCommandId = key => {
2
- const dotIndex = key.indexOf('.');
3
- return key.slice(dotIndex + 1);
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;
4
9
  };
5
- const create$j = () => {
6
- const commandQueues = new Map();
7
- const generations = Object.create(null);
8
- const states = Object.create(null);
9
- const commandMapRef = {};
10
- const getGeneration = uid => generations[uid] || 0;
11
- const isCurrentGeneration = (uid, generation) => {
12
- return states[uid] !== undefined && getGeneration(uid) === generation;
13
- };
14
- const updateState = (uid, generation, fallbackState, updater) => {
15
- if (!isCurrentGeneration(uid, generation)) {
16
- return Promise.resolve(fallbackState);
17
- }
18
- const current = states[uid];
19
- const updatedState = updater(current.newState);
20
- if (updatedState !== current.newState) {
21
- states[uid] = {
22
- newState: updatedState,
23
- oldState: current.oldState,
24
- scheduledState: updatedState
25
- };
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$3 = '\n';
18
+ const getNewLineIndex$1 = (string, startIndex = undefined) => {
19
+ return string.indexOf(NewLine$3, 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);
26
45
  }
27
- return Promise.resolve(updatedState);
28
- };
29
- const createAsyncCommandContext = (uid, generation) => {
30
- let latestState = states[uid].newState;
31
- return {
32
- getState: () => {
33
- if (isCurrentGeneration(uid, generation)) {
34
- latestState = states[uid].newState;
35
- }
36
- return latestState;
37
- },
38
- updateState: async updater => {
39
- latestState = await updateState(uid, generation, latestState, updater);
40
- return latestState;
41
- }
42
- };
43
- };
44
- const enqueueCommand = async (uid, command) => {
45
- const previous = commandQueues.get(uid) || Promise.resolve();
46
- const run = async () => {
47
- try {
48
- await previous;
49
- } catch {
50
- // The previous caller receives its error; later commands must still run.
51
- }
52
- await command();
53
- };
54
- const current = run();
55
- commandQueues.set(uid, current);
56
- try {
57
- await current;
58
- } finally {
59
- if (commandQueues.get(uid) === current) {
60
- commandQueues.delete(uid);
61
- }
46
+ if (error.codeFrame) {
47
+ // @ts-ignore
48
+ this.codeFrame = error.codeFrame;
62
49
  }
63
- };
64
- return {
65
- clear() {
66
- commandQueues.clear();
67
- for (const key of Object.keys(states)) {
68
- delete states[key];
69
- }
70
- },
71
- diff(uid, modules, numbers) {
72
- const {
73
- oldState,
74
- scheduledState
75
- } = states[uid];
76
- const diffResult = [];
77
- for (let i = 0; i < modules.length; i++) {
78
- const fn = modules[i];
79
- if (!fn(oldState, scheduledState)) {
80
- diffResult.push(numbers[i]);
81
- }
82
- }
83
- return diffResult;
84
- },
85
- dispose(uid) {
86
- commandQueues.delete(uid);
87
- delete states[uid];
88
- },
89
- get(uid) {
90
- return states[uid];
91
- },
92
- getCommandIds() {
93
- const keys = Object.keys(commandMapRef);
94
- const ids = keys.map(toCommandId);
95
- return ids;
96
- },
97
- getKeys() {
98
- return Object.keys(states).map(Number);
99
- },
100
- registerCommands(commandMap) {
101
- Object.assign(commandMapRef, commandMap);
102
- },
103
- set(uid, oldState, newState, scheduledState) {
104
- const current = states[uid];
105
- if (!current || oldState === newState && newState !== current.newState) {
106
- generations[uid] = getGeneration(uid) + 1;
107
- }
108
- states[uid] = {
109
- newState,
110
- oldState,
111
- scheduledState: scheduledState ?? newState
112
- };
113
- },
114
- wrapAsyncCommand(fn) {
115
- const wrapped = async (uid, ...args) => {
116
- const generation = getGeneration(uid);
117
- const context = createAsyncCommandContext(uid, generation);
118
- await fn(context, ...args);
119
- };
120
- return wrapped;
121
- },
122
- wrapCommand(fn) {
123
- const wrapped = async (uid, ...args) => {
124
- const generation = getGeneration(uid);
125
- const {
126
- newState,
127
- oldState
128
- } = states[uid];
129
- const newerState = await fn(newState, ...args);
130
- if (oldState === newerState || newState === newerState) {
131
- return;
132
- }
133
- if (!isCurrentGeneration(uid, generation)) {
134
- return;
135
- }
136
- const latestOld = states[uid];
137
- const latestNew = {
138
- ...latestOld.newState,
139
- ...newerState
140
- };
141
- states[uid] = {
142
- newState: latestNew,
143
- oldState: latestOld.oldState,
144
- scheduledState: latestNew
145
- };
146
- };
147
- return wrapped;
148
- },
149
- wrapGetter(fn) {
150
- const wrapped = (uid, ...args) => {
151
- const {
152
- newState
153
- } = states[uid];
154
- return fn(newState, ...args);
155
- };
156
- return wrapped;
157
- },
158
- wrapLoadContent(fn) {
159
- const wrapped = async (uid, ...args) => {
160
- const generation = getGeneration(uid);
161
- const {
162
- newState,
163
- oldState
164
- } = states[uid];
165
- const result = await fn(newState, ...args);
166
- const {
167
- error,
168
- state
169
- } = result;
170
- if (oldState === state || newState === state) {
171
- return {
172
- error
173
- };
174
- }
175
- if (!isCurrentGeneration(uid, generation)) {
176
- return {
177
- error
178
- };
179
- }
180
- const latestOld = states[uid];
181
- const latestNew = {
182
- ...latestOld.newState,
183
- ...state
184
- };
185
- states[uid] = {
186
- newState: latestNew,
187
- oldState: latestOld.oldState,
188
- scheduledState: latestNew
189
- };
190
- return {
191
- error
192
- };
193
- };
194
- return wrapped;
195
- },
196
- wrapSerialAsyncCommand(fn) {
197
- const wrapped = async (uid, ...args) => {
198
- await enqueueCommand(uid, async () => {
199
- if (!states[uid]) {
200
- return;
201
- }
202
- const generation = getGeneration(uid);
203
- const context = createAsyncCommandContext(uid, generation);
204
- await fn(context, ...args);
205
- });
206
- };
207
- return wrapped;
208
- },
209
- wrapSerialCommand(fn) {
210
- const wrapped = async (uid, ...args) => {
211
- await enqueueCommand(uid, async () => {
212
- if (!states[uid]) {
213
- return;
214
- }
215
- const generation = getGeneration(uid);
216
- const {
217
- newState,
218
- oldState
219
- } = states[uid];
220
- const newerState = await fn(newState, ...args);
221
- if (oldState === newerState || newState === newerState) {
222
- return;
223
- }
224
- if (!isCurrentGeneration(uid, generation)) {
225
- return;
226
- }
227
- const latestOld = states[uid];
228
- const latestNew = {
229
- ...latestOld.newState,
230
- ...newerState
231
- };
232
- states[uid] = {
233
- newState: latestNew,
234
- oldState: latestOld.oldState,
235
- scheduledState: latestNew
236
- };
237
- });
238
- };
239
- return wrapped;
50
+ if (error.code) {
51
+ // @ts-ignore
52
+ this.code = error.code;
240
53
  }
241
- };
242
- };
243
- const terminate = () => {
244
- globalThis.close();
245
- };
54
+ }
55
+ }
246
56
 
247
57
  class AssertionError extends Error {
248
58
  constructor(message) {
@@ -311,62 +121,6 @@ const boolean = value => {
311
121
  }
312
122
  };
313
123
 
314
- const normalizeLine = line => {
315
- if (line.startsWith('Error: ')) {
316
- return line.slice('Error: '.length);
317
- }
318
- if (line.startsWith('VError: ')) {
319
- return line.slice('VError: '.length);
320
- }
321
- return line;
322
- };
323
- const getCombinedMessage = (error, message) => {
324
- const stringifiedError = normalizeLine(`${error}`);
325
- if (message) {
326
- return `${message}: ${stringifiedError}`;
327
- }
328
- return stringifiedError;
329
- };
330
- const NewLine$3 = '\n';
331
- const getNewLineIndex$1 = (string, startIndex = undefined) => {
332
- return string.indexOf(NewLine$3, startIndex);
333
- };
334
- const mergeStacks = (parent, child) => {
335
- if (!child) {
336
- return parent;
337
- }
338
- const parentNewLineIndex = getNewLineIndex$1(parent);
339
- const childNewLineIndex = getNewLineIndex$1(child);
340
- if (childNewLineIndex === -1) {
341
- return parent;
342
- }
343
- const parentFirstLine = parent.slice(0, parentNewLineIndex);
344
- const childRest = child.slice(childNewLineIndex);
345
- const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
346
- if (parentFirstLine.includes(childFirstLine)) {
347
- return parentFirstLine + childRest;
348
- }
349
- return child;
350
- };
351
- class VError extends Error {
352
- constructor(error, message) {
353
- const combinedMessage = getCombinedMessage(error, message);
354
- super(combinedMessage);
355
- this.name = 'VError';
356
- if (error instanceof Error) {
357
- this.stack = mergeStacks(this.stack, error.stack);
358
- }
359
- if (error.codeFrame) {
360
- // @ts-ignore
361
- this.codeFrame = error.codeFrame;
362
- }
363
- if (error.code) {
364
- // @ts-ignore
365
- this.code = error.code;
366
- }
367
- }
368
- }
369
-
370
124
  const isMessagePort = value => {
371
125
  return value && value instanceof MessagePort;
372
126
  };
@@ -1058,7 +812,7 @@ const getErrorResponse = (id, error, preparePrettyError, logError) => {
1058
812
  const errorProperty = getErrorProperty(error, prettyError);
1059
813
  return create$1$1(id, errorProperty);
1060
814
  };
1061
- const create$i = (message, result) => {
815
+ const create$j = (message, result) => {
1062
816
  return {
1063
817
  id: message.id,
1064
818
  jsonrpc: Two$1,
@@ -1067,7 +821,7 @@ const create$i = (message, result) => {
1067
821
  };
1068
822
  const getSuccessResponse = (message, result) => {
1069
823
  const resultProperty = result ?? null;
1070
- return create$i(message, resultProperty);
824
+ return create$j(message, resultProperty);
1071
825
  };
1072
826
  const getErrorResponseSimple = (id, error) => {
1073
827
  return {
@@ -1161,7 +915,7 @@ const handleJsonRpcMessage = async (...args) => {
1161
915
 
1162
916
  const Two = '2.0';
1163
917
 
1164
- const create$h = (method, params) => {
918
+ const create$i = (method, params) => {
1165
919
  return {
1166
920
  jsonrpc: Two,
1167
921
  method,
@@ -1169,7 +923,7 @@ const create$h = (method, params) => {
1169
923
  };
1170
924
  };
1171
925
 
1172
- const create$g = (id, method, params) => {
926
+ const create$h = (id, method, params) => {
1173
927
  const message = {
1174
928
  id,
1175
929
  jsonrpc: Two,
@@ -1180,12 +934,12 @@ const create$g = (id, method, params) => {
1180
934
  };
1181
935
 
1182
936
  let id = 0;
1183
- const create$f = () => {
937
+ const create$g = () => {
1184
938
  return ++id;
1185
939
  };
1186
940
 
1187
941
  const registerPromise = map => {
1188
- const id = create$f();
942
+ const id = create$g();
1189
943
  const {
1190
944
  promise,
1191
945
  resolve
@@ -1202,7 +956,7 @@ const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer)
1202
956
  id,
1203
957
  promise
1204
958
  } = registerPromise(callbacks);
1205
- const message = create$g(id, method, params);
959
+ const message = create$h(id, method, params);
1206
960
  if (useSendAndTransfer && ipc.sendAndTransfer) {
1207
961
  ipc.sendAndTransfer(message);
1208
962
  } else {
@@ -1238,7 +992,7 @@ const createRpc = ipc => {
1238
992
  * @deprecated
1239
993
  */
1240
994
  send(method, ...params) {
1241
- const message = create$h(method, params);
995
+ const message = create$i(method, params);
1242
996
  ipc.send(message);
1243
997
  }
1244
998
  };
@@ -1278,7 +1032,7 @@ const listen$1 = async (module, options) => {
1278
1032
  return ipc;
1279
1033
  };
1280
1034
 
1281
- const create$e = async ({
1035
+ const create$f = async ({
1282
1036
  commandMap,
1283
1037
  isMessagePortOpen = true,
1284
1038
  messagePort
@@ -1296,7 +1050,7 @@ const create$e = async ({
1296
1050
  return rpc;
1297
1051
  };
1298
1052
 
1299
- const create$d = async ({
1053
+ const create$e = async ({
1300
1054
  commandMap,
1301
1055
  isMessagePortOpen,
1302
1056
  send
@@ -1306,7 +1060,7 @@ const create$d = async ({
1306
1060
  port2
1307
1061
  } = new MessageChannel();
1308
1062
  await send(port1);
1309
- return create$e({
1063
+ return create$f({
1310
1064
  commandMap,
1311
1065
  isMessagePortOpen,
1312
1066
  messagePort: port2
@@ -1341,13 +1095,13 @@ const createSharedLazyRpc = factory => {
1341
1095
  };
1342
1096
  };
1343
1097
 
1344
- const create$c = async ({
1098
+ const create$d = async ({
1345
1099
  commandMap,
1346
1100
  isMessagePortOpen,
1347
1101
  send
1348
1102
  }) => {
1349
1103
  return createSharedLazyRpc(() => {
1350
- return create$d({
1104
+ return create$e({
1351
1105
  commandMap,
1352
1106
  isMessagePortOpen,
1353
1107
  send
@@ -1355,17 +1109,17 @@ const create$c = async ({
1355
1109
  });
1356
1110
  };
1357
1111
 
1358
- const create$b = async ({
1112
+ const create$c = async ({
1359
1113
  commandMap,
1360
1114
  messagePort
1361
1115
  }) => {
1362
- return create$e({
1116
+ return create$f({
1363
1117
  commandMap,
1364
1118
  messagePort
1365
1119
  });
1366
1120
  };
1367
1121
 
1368
- const create$a = async ({
1122
+ const create$b = async ({
1369
1123
  commandMap
1370
1124
  }) => {
1371
1125
  // TODO create a commandMap per rpc instance
@@ -1397,7 +1151,7 @@ const createMockRpc = ({
1397
1151
  };
1398
1152
 
1399
1153
  const rpcs = Object.create(null);
1400
- const set$h = (id, rpc) => {
1154
+ const set$i = (id, rpc) => {
1401
1155
  rpcs[id] = rpc;
1402
1156
  };
1403
1157
  const get$8 = id => {
@@ -1408,7 +1162,7 @@ const remove$8 = id => {
1408
1162
  };
1409
1163
 
1410
1164
  /* eslint-disable @typescript-eslint/explicit-function-return-type */
1411
- const create$9 = rpcId => {
1165
+ const create$a = rpcId => {
1412
1166
  return {
1413
1167
  async dispose() {
1414
1168
  const rpc = get$8(rpcId);
@@ -1430,7 +1184,7 @@ const create$9 = rpcId => {
1430
1184
  const mockRpc = createMockRpc({
1431
1185
  commandMap
1432
1186
  });
1433
- set$h(rpcId, mockRpc);
1187
+ set$i(rpcId, mockRpc);
1434
1188
  // @ts-ignore
1435
1189
  mockRpc[Symbol.dispose] = () => {
1436
1190
  remove$8(rpcId);
@@ -1439,7 +1193,7 @@ const create$9 = rpcId => {
1439
1193
  return mockRpc;
1440
1194
  },
1441
1195
  set(rpc) {
1442
- set$h(rpcId, rpc);
1196
+ set$i(rpcId, rpc);
1443
1197
  }
1444
1198
  };
1445
1199
  };
@@ -1679,6 +1433,7 @@ const Electron = 2;
1679
1433
 
1680
1434
  const CompletionWorker = 301;
1681
1435
  const DebugWorker = 55;
1436
+ const DialogWorker = 7014;
1682
1437
  const EditorWorker = 99;
1683
1438
  const ErrorWorker = 3308;
1684
1439
  const ExtensionHostWorker = 44;
@@ -1695,15 +1450,20 @@ const SetPatches = 'Viewlet.setPatches';
1695
1450
 
1696
1451
  const FocusEditorText$1 = 12;
1697
1452
 
1453
+ const {
1454
+ invoke: invoke$h,
1455
+ set: set$h
1456
+ } = create$a(DialogWorker);
1457
+
1698
1458
  const {
1699
1459
  invoke: invoke$g,
1700
1460
  set: set$g
1701
- } = create$9(ErrorWorker);
1461
+ } = create$a(ErrorWorker);
1702
1462
 
1703
1463
  const {
1704
1464
  invoke: invoke$f,
1705
1465
  set: set$f
1706
- } = create$9(ExtensionHostWorker);
1466
+ } = create$a(ExtensionHostWorker);
1707
1467
 
1708
1468
  const ExtensionHost = {
1709
1469
  __proto__: null,
@@ -1714,15 +1474,88 @@ const ExtensionHost = {
1714
1474
  const {
1715
1475
  invoke: invoke$e,
1716
1476
  set: set$e
1717
- } = create$9(ExtensionManagementWorker);
1477
+ } = create$a(ExtensionManagementWorker);
1718
1478
  const getLanguages$1 = (platform, assetDir) => {
1719
1479
  return invoke$e('Extensions.getLanguages', platform, assetDir);
1720
1480
  };
1721
1481
 
1482
+ const createLazyRpc = rpcId => {
1483
+ let activeRpc;
1484
+ let factory;
1485
+ let generation = 0;
1486
+ let rpcPromise;
1487
+ const resetListeners = new Set();
1488
+ const createRpc = async expectedGeneration => {
1489
+ const rpc = await factory();
1490
+ if (generation !== expectedGeneration) {
1491
+ await rpc.dispose();
1492
+ throw new Error('Lazy RPC launch was superseded');
1493
+ }
1494
+ activeRpc = rpc;
1495
+ set$i(rpcId, rpc);
1496
+ return rpc;
1497
+ };
1498
+ const ensureRpc = async () => {
1499
+ if (activeRpc) {
1500
+ return activeRpc;
1501
+ }
1502
+ if (!rpcPromise) {
1503
+ const pending = createRpc(generation);
1504
+ rpcPromise = pending;
1505
+ void pending.catch(() => {
1506
+ if (rpcPromise === pending) {
1507
+ rpcPromise = undefined;
1508
+ }
1509
+ });
1510
+ }
1511
+ return rpcPromise;
1512
+ };
1513
+ const clear = async (reason, notify) => {
1514
+ generation++;
1515
+ const rpc = activeRpc;
1516
+ activeRpc = undefined;
1517
+ rpcPromise = undefined;
1518
+ if (rpc && get$8(rpcId) === rpc) {
1519
+ remove$8(rpcId);
1520
+ }
1521
+ if (notify) {
1522
+ for (const listener of resetListeners) {
1523
+ listener(reason);
1524
+ }
1525
+ }
1526
+ await rpc?.dispose();
1527
+ };
1528
+ return {
1529
+ dispose() {
1530
+ return clear(undefined, false);
1531
+ },
1532
+ async invoke(method, ...params) {
1533
+ const rpc = await ensureRpc();
1534
+ return rpc.invoke(method, ...params);
1535
+ },
1536
+ async invokeAndTransfer(method, ...params) {
1537
+ const rpc = await ensureRpc();
1538
+ return rpc.invokeAndTransfer(method, ...params);
1539
+ },
1540
+ onDidReset(listener) {
1541
+ resetListeners.add(listener);
1542
+ return () => {
1543
+ resetListeners.delete(listener);
1544
+ };
1545
+ },
1546
+ reset(reason) {
1547
+ return clear(reason, true);
1548
+ },
1549
+ setFactory(value) {
1550
+ factory = value;
1551
+ }
1552
+ };
1553
+ };
1554
+
1722
1555
  const {
1723
1556
  invoke: invoke$d,
1724
1557
  set: set$d
1725
- } = create$9(OpenerWorker);
1558
+ } = create$a(OpenerWorker);
1726
1559
  const openUrl = async (url, platform) => {
1727
1560
  return invoke$d('Open.openUrl', url, platform);
1728
1561
  };
@@ -1730,13 +1563,13 @@ const openUrl = async (url, platform) => {
1730
1563
  const {
1731
1564
  invoke: invoke$c,
1732
1565
  set: set$c
1733
- } = create$9(IconThemeWorker);
1566
+ } = create$a(IconThemeWorker);
1734
1567
 
1735
1568
  const {
1736
1569
  invoke: invoke$b,
1737
1570
  invokeAndTransfer,
1738
1571
  set: set$b
1739
- } = create$9(RendererWorker$1);
1572
+ } = create$a(RendererWorker$1);
1740
1573
  const showContextMenu2 = async (uid, menuId, x, y, args) => {
1741
1574
  number(uid);
1742
1575
  number(menuId);
@@ -1748,6 +1581,10 @@ const sendMessagePortToOpenerWorker = async (port, rpcId) => {
1748
1581
  const command = 'HandleMessagePort.handleMessagePort';
1749
1582
  await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToOpenerWorker', port, command, rpcId);
1750
1583
  };
1584
+ const sendMessagePortToDialogWorker = async port => {
1585
+ const command = 'HandleMessagePort.handleMessagePort';
1586
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToDialogWorker', port, command);
1587
+ };
1751
1588
  const sendMessagePortToErrorWorker = async (port, rpcId) => {
1752
1589
  const command = 'Errors.handleMessagePort';
1753
1590
  await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToErrorWorker', port, command, rpcId);
@@ -1799,6 +1636,7 @@ const RendererWorker = {
1799
1636
  openUri,
1800
1637
  readClipBoardText,
1801
1638
  readFile,
1639
+ sendMessagePortToDialogWorker,
1802
1640
  sendMessagePortToErrorWorker,
1803
1641
  sendMessagePortToExtensionHostWorker,
1804
1642
  sendMessagePortToExtensionManagementWorker: sendMessagePortToExtensionManagementWorker$1,
@@ -1813,7 +1651,7 @@ const RendererWorker = {
1813
1651
  const {
1814
1652
  invoke: invoke$a,
1815
1653
  set: set$a
1816
- } = create$9(MarkdownWorker);
1654
+ } = create$a(MarkdownWorker);
1817
1655
 
1818
1656
  const SyntaxHighlightingWorker = {
1819
1657
  __proto__: null,
@@ -1821,35 +1659,251 @@ const SyntaxHighlightingWorker = {
1821
1659
  set: set$a
1822
1660
  };
1823
1661
 
1824
- const createLazyRpc = rpcId => {
1825
- let rpcPromise;
1826
- let factory;
1827
- const createRpc = async () => {
1828
- const rpc = await factory();
1829
- set$h(rpcId, rpc);
1662
+ const toCommandId = key => {
1663
+ const dotIndex = key.indexOf('.');
1664
+ return key.slice(dotIndex + 1);
1665
+ };
1666
+ const create$9 = () => {
1667
+ const commandQueues = new Map();
1668
+ const generations = Object.create(null);
1669
+ const states = Object.create(null);
1670
+ const commandMapRef = {};
1671
+ const getGeneration = uid => generations[uid] || 0;
1672
+ const isCurrentGeneration = (uid, generation) => {
1673
+ return states[uid] !== undefined && getGeneration(uid) === generation;
1674
+ };
1675
+ const updateState = (uid, generation, fallbackState, updater) => {
1676
+ if (!isCurrentGeneration(uid, generation)) {
1677
+ return Promise.resolve(fallbackState);
1678
+ }
1679
+ const current = states[uid];
1680
+ const updatedState = updater(current.newState);
1681
+ if (updatedState !== current.newState) {
1682
+ states[uid] = {
1683
+ newState: updatedState,
1684
+ oldState: current.oldState,
1685
+ scheduledState: updatedState
1686
+ };
1687
+ }
1688
+ return Promise.resolve(updatedState);
1830
1689
  };
1831
- const ensureRpc = async () => {
1832
- if (!rpcPromise) {
1833
- rpcPromise = createRpc();
1690
+ const createAsyncCommandContext = (uid, generation) => {
1691
+ let latestState = states[uid].newState;
1692
+ return {
1693
+ getState: () => {
1694
+ if (isCurrentGeneration(uid, generation)) {
1695
+ latestState = states[uid].newState;
1696
+ }
1697
+ return latestState;
1698
+ },
1699
+ updateState: async updater => {
1700
+ latestState = await updateState(uid, generation, latestState, updater);
1701
+ return latestState;
1702
+ }
1703
+ };
1704
+ };
1705
+ const enqueueCommand = async (uid, command) => {
1706
+ const previous = commandQueues.get(uid) || Promise.resolve();
1707
+ const run = async () => {
1708
+ try {
1709
+ await previous;
1710
+ } catch {
1711
+ // The previous caller receives its error; later commands must still run.
1712
+ }
1713
+ await command();
1714
+ };
1715
+ const current = run();
1716
+ commandQueues.set(uid, current);
1717
+ try {
1718
+ await current;
1719
+ } finally {
1720
+ if (commandQueues.get(uid) === current) {
1721
+ commandQueues.delete(uid);
1722
+ }
1834
1723
  }
1835
- await rpcPromise;
1836
1724
  };
1837
1725
  return {
1838
- async invoke(method, ...params) {
1839
- await ensureRpc();
1840
- const rpc = get$8(rpcId);
1841
- return rpc.invoke(method, ...params);
1726
+ clear() {
1727
+ commandQueues.clear();
1728
+ for (const key of Object.keys(states)) {
1729
+ delete states[key];
1730
+ }
1842
1731
  },
1843
- async invokeAndTransfer(method, ...params) {
1844
- await ensureRpc();
1845
- const rpc = get$8(rpcId);
1846
- return rpc.invokeAndTransfer(method, ...params);
1732
+ diff(uid, modules, numbers) {
1733
+ const {
1734
+ oldState,
1735
+ scheduledState
1736
+ } = states[uid];
1737
+ const diffResult = [];
1738
+ for (let i = 0; i < modules.length; i++) {
1739
+ const fn = modules[i];
1740
+ if (!fn(oldState, scheduledState)) {
1741
+ diffResult.push(numbers[i]);
1742
+ }
1743
+ }
1744
+ return diffResult;
1847
1745
  },
1848
- setFactory(value) {
1849
- factory = value;
1746
+ dispose(uid) {
1747
+ commandQueues.delete(uid);
1748
+ delete states[uid];
1749
+ },
1750
+ get(uid) {
1751
+ return states[uid];
1752
+ },
1753
+ getCommandIds() {
1754
+ const keys = Object.keys(commandMapRef);
1755
+ const ids = keys.map(toCommandId);
1756
+ return ids;
1757
+ },
1758
+ getKeys() {
1759
+ return Object.keys(states).map(Number);
1760
+ },
1761
+ registerCommands(commandMap) {
1762
+ Object.assign(commandMapRef, commandMap);
1763
+ },
1764
+ set(uid, oldState, newState, scheduledState) {
1765
+ const current = states[uid];
1766
+ if (!current || oldState === newState && newState !== current.newState) {
1767
+ generations[uid] = getGeneration(uid) + 1;
1768
+ }
1769
+ states[uid] = {
1770
+ newState,
1771
+ oldState,
1772
+ scheduledState: scheduledState ?? newState
1773
+ };
1774
+ },
1775
+ wrapAsyncCommand(fn) {
1776
+ const wrapped = async (uid, ...args) => {
1777
+ const generation = getGeneration(uid);
1778
+ const context = createAsyncCommandContext(uid, generation);
1779
+ await fn(context, ...args);
1780
+ };
1781
+ return wrapped;
1782
+ },
1783
+ wrapCommand(fn) {
1784
+ const wrapped = async (uid, ...args) => {
1785
+ const generation = getGeneration(uid);
1786
+ const {
1787
+ newState,
1788
+ oldState
1789
+ } = states[uid];
1790
+ const newerState = await fn(newState, ...args);
1791
+ if (oldState === newerState || newState === newerState) {
1792
+ return;
1793
+ }
1794
+ if (!isCurrentGeneration(uid, generation)) {
1795
+ return;
1796
+ }
1797
+ const latestOld = states[uid];
1798
+ const latestNew = {
1799
+ ...latestOld.newState,
1800
+ ...newerState
1801
+ };
1802
+ states[uid] = {
1803
+ newState: latestNew,
1804
+ oldState: latestOld.oldState,
1805
+ scheduledState: latestNew
1806
+ };
1807
+ };
1808
+ return wrapped;
1809
+ },
1810
+ wrapGetter(fn) {
1811
+ const wrapped = (uid, ...args) => {
1812
+ const {
1813
+ newState
1814
+ } = states[uid];
1815
+ return fn(newState, ...args);
1816
+ };
1817
+ return wrapped;
1818
+ },
1819
+ wrapLoadContent(fn) {
1820
+ const wrapped = async (uid, ...args) => {
1821
+ const generation = getGeneration(uid);
1822
+ const {
1823
+ newState,
1824
+ oldState
1825
+ } = states[uid];
1826
+ const result = await fn(newState, ...args);
1827
+ const {
1828
+ error,
1829
+ state
1830
+ } = result;
1831
+ if (oldState === state || newState === state) {
1832
+ return {
1833
+ error
1834
+ };
1835
+ }
1836
+ if (!isCurrentGeneration(uid, generation)) {
1837
+ return {
1838
+ error
1839
+ };
1840
+ }
1841
+ const latestOld = states[uid];
1842
+ const latestNew = {
1843
+ ...latestOld.newState,
1844
+ ...state
1845
+ };
1846
+ states[uid] = {
1847
+ newState: latestNew,
1848
+ oldState: latestOld.oldState,
1849
+ scheduledState: latestNew
1850
+ };
1851
+ return {
1852
+ error
1853
+ };
1854
+ };
1855
+ return wrapped;
1856
+ },
1857
+ wrapSerialAsyncCommand(fn) {
1858
+ const wrapped = async (uid, ...args) => {
1859
+ await enqueueCommand(uid, async () => {
1860
+ if (!states[uid]) {
1861
+ return;
1862
+ }
1863
+ const generation = getGeneration(uid);
1864
+ const context = createAsyncCommandContext(uid, generation);
1865
+ await fn(context, ...args);
1866
+ });
1867
+ };
1868
+ return wrapped;
1869
+ },
1870
+ wrapSerialCommand(fn) {
1871
+ const wrapped = async (uid, ...args) => {
1872
+ await enqueueCommand(uid, async () => {
1873
+ if (!states[uid]) {
1874
+ return;
1875
+ }
1876
+ const generation = getGeneration(uid);
1877
+ const {
1878
+ newState,
1879
+ oldState
1880
+ } = states[uid];
1881
+ const newerState = await fn(newState, ...args);
1882
+ if (oldState === newerState || newState === newerState) {
1883
+ return;
1884
+ }
1885
+ if (!isCurrentGeneration(uid, generation)) {
1886
+ return;
1887
+ }
1888
+ const latestOld = states[uid];
1889
+ const latestNew = {
1890
+ ...latestOld.newState,
1891
+ ...newerState
1892
+ };
1893
+ states[uid] = {
1894
+ newState: latestNew,
1895
+ oldState: latestOld.oldState,
1896
+ scheduledState: latestNew
1897
+ };
1898
+ });
1899
+ };
1900
+ return wrapped;
1850
1901
  }
1851
1902
  };
1852
1903
  };
1904
+ const terminate = () => {
1905
+ globalThis.close();
1906
+ };
1853
1907
 
1854
1908
  // TODO add tests for this
1855
1909
  const activateByEvent = async (event, assetDir, platform) => {
@@ -1867,7 +1921,7 @@ const codeGeneratorAccept = state => {
1867
1921
  const ModuleWorkerAndWorkaroundForChromeDevtoolsBug = 6;
1868
1922
 
1869
1923
  const launchWorker = async (name, url, intializeCommand) => {
1870
- const rpc = await create$d({
1924
+ const rpc = await create$e({
1871
1925
  commandMap: {},
1872
1926
  isMessagePortOpen: true,
1873
1927
  async send(port) {
@@ -1922,7 +1976,7 @@ const loadContent$3 = async (state, parentUid) => {
1922
1976
  };
1923
1977
  };
1924
1978
 
1925
- const editorStates = create$j();
1979
+ const editorStates = create$9();
1926
1980
  const {
1927
1981
  dispose: dispose$1,
1928
1982
  getCommandIds,
@@ -5275,7 +5329,7 @@ const isPermissionDeniedError = error => {
5275
5329
  };
5276
5330
  const showSaveErrorDialog = async error => {
5277
5331
  if (isPermissionDeniedError(error)) {
5278
- await invoke$b('ElectronDialog.showMessageBox', {
5332
+ await invoke$h('ElectronDialog.showMessageBox', {
5279
5333
  buttons: ['OK'],
5280
5334
  defaultId: 0,
5281
5335
  message: "You don't have permission to save changes to this file.",
@@ -5284,7 +5338,7 @@ const showSaveErrorDialog = async error => {
5284
5338
  });
5285
5339
  return;
5286
5340
  }
5287
- await invoke$b('ElectronDialog.showMessageBox', {
5341
+ await invoke$h('ElectronDialog.showMessageBox', {
5288
5342
  buttons: ['OK'],
5289
5343
  defaultId: 0,
5290
5344
  detail: error.message,
@@ -8260,7 +8314,7 @@ const launch = async () => {
8260
8314
  return;
8261
8315
  }
8262
8316
  const rpc = await launchFindWidgetWorker();
8263
- set$h(rpcId, rpc);
8317
+ set$i(rpcId, rpc);
8264
8318
  };
8265
8319
  const invoke$3 = async (method, ...params) => {
8266
8320
  const rpc = get$8(rpcId);
@@ -11675,12 +11729,12 @@ const handleBeforeInput = (editor, inputType, data) => {
11675
11729
  };
11676
11730
 
11677
11731
  const handleMessagePort = async (port, rpcId) => {
11678
- const rpc = await create$b({
11732
+ const rpc = await create$c({
11679
11733
  commandMap: {},
11680
11734
  messagePort: port
11681
11735
  });
11682
11736
  if (rpcId) {
11683
- set$h(rpcId, rpc);
11737
+ set$i(rpcId, rpc);
11684
11738
  }
11685
11739
  };
11686
11740
 
@@ -11953,7 +12007,7 @@ const sendMessagePortToSyntaxHighlightingWorker = async port => {
11953
12007
 
11954
12008
  const createSyntaxHighlightingWorkerRpc = async () => {
11955
12009
  try {
11956
- const rpc = await create$d({
12010
+ const rpc = await create$e({
11957
12011
  commandMap: {},
11958
12012
  send: sendMessagePortToSyntaxHighlightingWorker
11959
12013
  });
@@ -12325,14 +12379,11 @@ const compareNodes = (oldNode, newNode) => {
12325
12379
  }
12326
12380
  const patches = [];
12327
12381
  // Handle reference nodes - special handling for uid changes
12328
- if (oldNode.type === Reference) {
12329
- if (oldNode.uid !== newNode.uid) {
12330
- patches.push({
12331
- type: SetReferenceNodeUid,
12332
- uid: newNode.uid
12333
- });
12334
- }
12335
- return patches;
12382
+ if (oldNode.type === Reference && oldNode.uid !== newNode.uid) {
12383
+ patches.push({
12384
+ type: SetReferenceNodeUid,
12385
+ uid: newNode.uid
12386
+ });
12336
12387
  }
12337
12388
  // Handle text nodes
12338
12389
  if (oldNode.type === Text$1 && newNode.type === Text$1) {
@@ -12345,8 +12396,8 @@ const compareNodes = (oldNode, newNode) => {
12345
12396
  return patches;
12346
12397
  }
12347
12398
  // Compare attributes
12348
- const oldKeys = getKeys(oldNode);
12349
- const newKeys = getKeys(newNode);
12399
+ const oldKeys = getKeys(oldNode).filter(key => oldNode.type !== Reference || key !== 'uid');
12400
+ const newKeys = getKeys(newNode).filter(key => newNode.type !== Reference || key !== 'uid');
12350
12401
  // Check for attribute changes
12351
12402
  for (const key of newKeys) {
12352
12403
  if (oldNode[key] !== newNode[key]) {
@@ -12370,9 +12421,14 @@ const compareNodes = (oldNode, newNode) => {
12370
12421
  };
12371
12422
 
12372
12423
  const treeToArray = node => {
12373
- const result = [node.node];
12374
- for (const child of node.children) {
12375
- result.push(...treeToArray(child));
12424
+ const result = [];
12425
+ const stack = [node];
12426
+ while (stack.length > 0) {
12427
+ const current = stack.pop();
12428
+ result.push(current.node);
12429
+ for (let i = current.children.length - 1; i >= 0; i--) {
12430
+ stack.push(current.children[i]);
12431
+ }
12376
12432
  }
12377
12433
  return result;
12378
12434
  };
@@ -12484,17 +12540,14 @@ const diffTrees = (oldTree, newTree, patches, path) => {
12484
12540
  };
12485
12541
 
12486
12542
  const removeTrailingNavigationPatches = patches => {
12487
- // Find the last non-navigation patch
12488
- let lastNonNavigationIndex = -1;
12489
- for (let i = patches.length - 1; i >= 0; i--) {
12490
- const patch = patches[i];
12543
+ while (patches.length > 0) {
12544
+ const patch = patches.at(-1);
12491
12545
  if (patch.type !== NavigateChild && patch.type !== NavigateParent && patch.type !== NavigateSibling) {
12492
- lastNonNavigationIndex = i;
12493
12546
  break;
12494
12547
  }
12548
+ patches.pop();
12495
12549
  }
12496
- // Return patches up to and including the last non-navigation patch
12497
- return lastNonNavigationIndex === -1 ? [] : patches.slice(0, lastNonNavigationIndex + 1);
12550
+ return patches;
12498
12551
  };
12499
12552
 
12500
12553
  const diffTree = (oldNodes, newNodes) => {
@@ -13606,7 +13659,7 @@ for (const [key, value] of Object.entries(commandMap)) {
13606
13659
  }
13607
13660
 
13608
13661
  const initializeErrorWorker = async () => {
13609
- const rpc = await create$c({
13662
+ const rpc = await create$d({
13610
13663
  commandMap: {},
13611
13664
  send(port) {
13612
13665
  return sendMessagePortToErrorWorker(port, EditorWorker);
@@ -13617,7 +13670,7 @@ const initializeErrorWorker = async () => {
13617
13670
 
13618
13671
  const createExtensionHostRpc = async () => {
13619
13672
  const initialCommand = 'HandleMessagePort.handleMessagePort2';
13620
- const rpc = await create$c({
13673
+ const rpc = await create$d({
13621
13674
  commandMap: {},
13622
13675
  async send(port) {
13623
13676
  await sendMessagePortToExtensionHostWorker2(port, initialCommand, EditorWorker);
@@ -13632,7 +13685,7 @@ const initializeExtensionHost = async () => {
13632
13685
  };
13633
13686
 
13634
13687
  const createExtensionManagementWorkerRpc = async () => {
13635
- const rpc = await create$c({
13688
+ const rpc = await create$d({
13636
13689
  commandMap: {},
13637
13690
  async send(port) {
13638
13691
  await sendMessagePortToExtensionManagementWorker$1(port, EditorWorker);
@@ -13655,7 +13708,7 @@ const send$1 = port => {
13655
13708
  return sendMessagePortToOpenerWorker(port);
13656
13709
  };
13657
13710
  const initializeOpenerWorker = async () => {
13658
- const rpc = await create$c({
13711
+ const rpc = await create$d({
13659
13712
  commandMap: {},
13660
13713
  send: send$1
13661
13714
  });
@@ -13663,7 +13716,7 @@ const initializeOpenerWorker = async () => {
13663
13716
  };
13664
13717
 
13665
13718
  const initializeRendererWorker = async () => {
13666
- const rpc = await create$a({
13719
+ const rpc = await create$b({
13667
13720
  commandMap: commandMap
13668
13721
  });
13669
13722
  set$b(rpc);
@@ -13673,7 +13726,7 @@ const send = port => {
13673
13726
  return sendMessagePortToTextMeasurementWorker(port);
13674
13727
  };
13675
13728
  const initializeTextMeasurementWorker = async () => {
13676
- const rpc = await create$c({
13729
+ const rpc = await create$d({
13677
13730
  commandMap: {},
13678
13731
  send
13679
13732
  });
@@ -13683,6 +13736,11 @@ const initializeTextMeasurementWorker = async () => {
13683
13736
  const listen = async () => {
13684
13737
  registerCommands(commandMap);
13685
13738
  await Promise.all([initializeRendererWorker(), initializeErrorWorker(), initializeExtensionHost(), initializeExtensionManagementWorker(), initializeTextMeasurementWorker(), initializeOpenerWorker()]);
13739
+ const dialogRpc = await create$d({
13740
+ commandMap: {},
13741
+ send: sendMessagePortToDialogWorker
13742
+ });
13743
+ set$h(dialogRpc);
13686
13744
  };
13687
13745
 
13688
13746
  const CodeGeneratorInput = 'CodeGeneratorInput';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/editor-worker",
3
- "version": "19.25.0",
3
+ "version": "19.26.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git@github.com:lvce-editor/editor-worker.git"