@blotch/angular-lynx 22.2.7 → 22.2.8

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.
@@ -262,6 +262,7 @@ const callOnBackground = (name, ...args) => {
262
262
  } catch (error) {
263
263
  return Promise.reject(error);
264
264
  }
265
+ listenInTestingEnvironment();
265
266
  return new Promise((resolve, reject) => {
266
267
  const id = nextCallId++;
267
268
  pending.set(id, {
@@ -316,60 +317,74 @@ const unsubscribeSessionStorageFromMainThread = (relayId) => {
316
317
  if (!sessionStorageListeners.delete(relayId)) return;
317
318
  callOnBackground("unsubscribeSessionStorage", relayId).catch(() => {});
318
319
  };
319
- const installRelay = () => {
320
- if (typeof lynx === "undefined") return;
321
- try {
322
- if (isMainThread()) {
323
- const context = lynx.getJSContext();
324
- context.addEventListener(RESULT_EVENT, (event) => {
325
- const { id, value, error } = JSON.parse(event.data);
326
- const call = pending.get(id);
327
- if (!call) return;
328
- pending.delete(id);
329
- if (error !== void 0) call.reject(new Error(error));
330
- else call.resolve(value);
331
- });
332
- context.addEventListener(SESSION_STORAGE_EVENT, (event) => {
333
- const { relayId, value } = JSON.parse(event.data);
334
- sessionStorageListeners.get(relayId)?.(value);
335
- });
336
- context.addEventListener(CALLBACK_EVENT, (event) => {
337
- const { id, args } = JSON.parse(event.data);
338
- relayedFunctions.get(id)?.(...args);
339
- });
340
- return;
320
+ const relayContexts = /* @__PURE__ */ new WeakSet();
321
+ const listenOnMainThread = (context) => {
322
+ if (relayContexts.has(context)) return;
323
+ relayContexts.add(context);
324
+ context.addEventListener(RESULT_EVENT, (event) => {
325
+ const { id, value, error } = JSON.parse(event.data);
326
+ const call = pending.get(id);
327
+ if (!call) return;
328
+ pending.delete(id);
329
+ if (error !== void 0) call.reject(new Error(error));
330
+ else call.resolve(value);
331
+ });
332
+ context.addEventListener(SESSION_STORAGE_EVENT, (event) => {
333
+ const { relayId, value } = JSON.parse(event.data);
334
+ sessionStorageListeners.get(relayId)?.(value);
335
+ });
336
+ context.addEventListener(CALLBACK_EVENT, (event) => {
337
+ const { id, args } = JSON.parse(event.data);
338
+ relayedFunctions.get(id)?.(...args);
339
+ });
340
+ };
341
+ const listenOnBackgroundThread = (context) => {
342
+ if (relayContexts.has(context)) return;
343
+ relayContexts.add(context);
344
+ context.addEventListener(CALL_EVENT, async (event) => {
345
+ const { id, name, args } = JSON.parse(event.data);
346
+ let reply;
347
+ try {
348
+ const handler = handlers[name];
349
+ if (!handler) throw new Error(`Unknown background call: ${name}`);
350
+ reply = {
351
+ id,
352
+ value: await handler(...args)
353
+ };
354
+ } catch (error) {
355
+ reply = {
356
+ id,
357
+ error: error instanceof Error ? error.message : String(error)
358
+ };
341
359
  }
342
- const context = lynx.getCoreContext();
343
- context.addEventListener(CALL_EVENT, async (event) => {
344
- const { id, name, args } = JSON.parse(event.data);
345
- let reply;
346
- try {
347
- const handler = handlers[name];
348
- if (!handler) throw new Error(`Unknown background call: ${name}`);
349
- reply = {
350
- id,
351
- value: await handler(...args)
352
- };
353
- } catch (error) {
354
- reply = {
355
- id,
356
- error: error instanceof Error ? error.message : String(error)
357
- };
358
- }
359
- let data;
360
- try {
361
- data = JSON.stringify(reply);
362
- } catch (error) {
363
- data = JSON.stringify({
364
- id,
365
- error: `The result of ${name} can't be sent to the main thread: ${error.message}`
366
- });
367
- }
368
- context.dispatchEvent({
369
- type: RESULT_EVENT,
370
- data
360
+ let data;
361
+ try {
362
+ data = JSON.stringify(reply);
363
+ } catch (error) {
364
+ data = JSON.stringify({
365
+ id,
366
+ error: `The result of ${name} can't be sent to the main thread: ${error.message}`
371
367
  });
368
+ }
369
+ context.dispatchEvent({
370
+ type: RESULT_EVENT,
371
+ data
372
372
  });
373
+ });
374
+ };
375
+ const listenInTestingEnvironment = () => {
376
+ const env = globalThis.lynxTestingEnv;
377
+ if (!env) return;
378
+ try {
379
+ listenOnMainThread(lynx.getJSContext());
380
+ listenOnBackgroundThread(env.backgroundThread.globalThis.lynx.getCoreContext());
381
+ } catch {}
382
+ };
383
+ const installRelay = () => {
384
+ if (typeof lynx === "undefined") return;
385
+ try {
386
+ if (isMainThread()) listenOnMainThread(lynx.getJSContext());
387
+ else listenOnBackgroundThread(lynx.getCoreContext());
373
388
  } catch {}
374
389
  };
375
390
  installRelay();