@lvce-editor/ipc 3.1.0 → 3.2.0

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/dist/index.d.ts CHANGED
@@ -7,6 +7,8 @@ export const IpcChildWithElectronMessagePort: IpcChild
7
7
  export const IpcChildWithElectronUtilityProcess: IpcChild
8
8
  export const IpcChildWithNodeForkedProcess: IpcChild
9
9
  export const IpcChildWithWebSocket: IpcChild
10
+ export const IpcChildWithModuleWorkerAndMessagePort: IpcChild
11
+ export const IpcChildWithModuleWorker: IpcChild
10
12
 
11
13
  interface IpcParent {
12
14
  readonly create: any
package/dist/index.js CHANGED
@@ -151,7 +151,7 @@ const isMessagePortMain = value => {
151
151
  };
152
152
 
153
153
  // @ts-ignore
154
- const listen$3 = ({
154
+ const listen$5 = ({
155
155
  messagePort
156
156
  }) => {
157
157
  if (!isMessagePortMain(messagePort)) {
@@ -176,7 +176,7 @@ const getActualData$1 = event => {
176
176
  };
177
177
 
178
178
  // @ts-ignore
179
- const wrap$6 = messagePort => {
179
+ const wrap$8 = messagePort => {
180
180
  return {
181
181
  messagePort,
182
182
  // @ts-ignore
@@ -213,8 +213,8 @@ const wrap$6 = messagePort => {
213
213
 
214
214
  const IpcChildWithElectronMessagePort = {
215
215
  __proto__: null,
216
- listen: listen$3,
217
- wrap: wrap$6
216
+ listen: listen$5,
217
+ wrap: wrap$8
218
218
  };
219
219
 
220
220
  // @ts-ignore
@@ -232,7 +232,7 @@ const getUtilityProcessPortData = event => {
232
232
  };
233
233
  };
234
234
 
235
- const listen$2 = () => {
235
+ const listen$4 = () => {
236
236
  // @ts-ignore
237
237
  const {
238
238
  parentPort
@@ -244,12 +244,12 @@ const listen$2 = () => {
244
244
  };
245
245
 
246
246
  // @ts-ignore
247
- const signal$1 = parentPort => {
247
+ const signal$2 = parentPort => {
248
248
  parentPort.postMessage('ready');
249
249
  };
250
250
 
251
251
  // @ts-ignore
252
- const wrap$5 = parentPort => {
252
+ const wrap$7 = parentPort => {
253
253
  return {
254
254
  parentPort,
255
255
  // @ts-ignore
@@ -287,8 +287,146 @@ const wrap$5 = parentPort => {
287
287
 
288
288
  const IpcChildWithElectronUtilityProcess = {
289
289
  __proto__: null,
290
- listen: listen$2,
290
+ listen: listen$4,
291
+ signal: signal$2,
292
+ wrap: wrap$7
293
+ };
294
+
295
+ const getData = event => {
296
+ return event.data;
297
+ };
298
+
299
+ const listen$3 = () => {
300
+ // @ts-ignore
301
+ if (typeof WorkerGlobalScope === 'undefined') {
302
+ throw new TypeError('module is not in web worker scope');
303
+ }
304
+ // @ts-ignore
305
+ globalThis.postMessage('ready');
306
+ return globalThis;
307
+ };
308
+ const signal$1 = global => {
309
+ global.postMessage('ready');
310
+ };
311
+ const wrap$6 = global => {
312
+ return {
313
+ global,
314
+ /**
315
+ * @type {any}
316
+ */
317
+ listener: undefined,
318
+ send(message) {
319
+ this.global.postMessage(message);
320
+ },
321
+ sendAndTransfer(message, transferables) {
322
+ this.global.postMessage(message, transferables);
323
+ },
324
+ get onmessage() {
325
+ return this.listener;
326
+ },
327
+ set onmessage(listener) {
328
+ const wrappedListener = event => {
329
+ const data = getData(event);
330
+ // @ts-expect-error
331
+ listener({
332
+ data,
333
+ target: this
334
+ });
335
+ };
336
+ this.listener = listener;
337
+ this.global.onmessage = wrappedListener;
338
+ }
339
+ };
340
+ };
341
+
342
+ const IpcChildWithModuleWorker = {
343
+ __proto__: null,
344
+ listen: listen$3,
291
345
  signal: signal$1,
346
+ wrap: wrap$6
347
+ };
348
+
349
+ const withResolvers = () => {
350
+ let _resolve;
351
+ const promise = new Promise(resolve => {
352
+ _resolve = resolve;
353
+ });
354
+ return {
355
+ resolve: _resolve,
356
+ promise
357
+ };
358
+ };
359
+
360
+ const waitForFirstMessage = async port => {
361
+ const {
362
+ resolve,
363
+ promise
364
+ } = withResolvers();
365
+ const cleanup = value => {
366
+ port.onmessage = null;
367
+ resolve(value);
368
+ };
369
+ const handleMessage = event => {
370
+ cleanup(event);
371
+ };
372
+ port.onmessage = handleMessage;
373
+ const event = await promise;
374
+ // @ts-expect-error
375
+ return event.data;
376
+ };
377
+
378
+ const listen$2 = async () => {
379
+ const parentIpcRaw = listen$3();
380
+ const parentIpc = wrap$6(parentIpcRaw);
381
+ const firstMessage = await waitForFirstMessage(parentIpc);
382
+ if (firstMessage.method !== 'initialize') {
383
+ throw new IpcError('unexpected first message');
384
+ }
385
+ const type = firstMessage.params[0];
386
+ if (type === 'message-port') {
387
+ const port = firstMessage.params[1];
388
+ return port;
389
+ }
390
+ return globalThis;
391
+ };
392
+ const wrap$5 = port => {
393
+ return {
394
+ port,
395
+ /**
396
+ * @type {any}
397
+ */
398
+ wrappedListener: undefined,
399
+ send(message) {
400
+ this.port.postMessage(message);
401
+ },
402
+ sendAndTransfer(message, transferables) {
403
+ this.port.postMessage(message, transferables);
404
+ },
405
+ get onmessage() {
406
+ return this.wrappedListener;
407
+ },
408
+ set onmessage(listener) {
409
+ if (listener) {
410
+ // @ts-expect-error
411
+ this.wrappedListener = event => {
412
+ const data = getData(event);
413
+ // @ts-expect-error
414
+ listener({
415
+ data,
416
+ target: this
417
+ });
418
+ };
419
+ } else {
420
+ this.wrappedListener = undefined;
421
+ }
422
+ this.port.onmessage = this.wrappedListener;
423
+ }
424
+ };
425
+ };
426
+
427
+ const IpcChildWithModuleWorkerAndMessagePort = {
428
+ __proto__: null,
429
+ listen: listen$2,
292
430
  wrap: wrap$5
293
431
  };
294
432
 
@@ -356,17 +494,6 @@ const IpcChildWithNodeForkedProcess = {
356
494
  const Open = 1;
357
495
  const Close = 2;
358
496
 
359
- const withResolvers = () => {
360
- let _resolve;
361
- const promise = new Promise(resolve => {
362
- _resolve = resolve;
363
- });
364
- return {
365
- resolve: _resolve,
366
- promise
367
- };
368
- };
369
-
370
497
  // @ts-ignore
371
498
  const getFirstEvent = (eventEmitter, eventMap) => {
372
499
  const {
@@ -910,4 +1037,4 @@ const IpcParentWithNodeWorker = {
910
1037
  wrap
911
1038
  };
912
1039
 
913
- export { IpcChildWithElectronMessagePort, IpcChildWithElectronUtilityProcess, IpcChildWithNodeForkedProcess, IpcChildWithWebSocket, IpcParentWithElectronUtilityProcess, IpcParentWithNodeForkedProcess, IpcParentWithNodeWorker };
1040
+ export { IpcChildWithElectronMessagePort, IpcChildWithElectronUtilityProcess, IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort, IpcChildWithNodeForkedProcess, IpcChildWithWebSocket, IpcParentWithElectronUtilityProcess, IpcParentWithNodeForkedProcess, IpcParentWithNodeWorker };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/ipc",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "Inter Process Communication for Lvce Editor",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",