@arsedizioni/ars-utils 21.2.355 → 21.2.357

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.
@@ -2309,7 +2309,7 @@ class BroadcastChannelManager {
2309
2309
  constructor(bus = 'ARS-CHANNEL') {
2310
2310
  this.subject = new Subject();
2311
2311
  this.subscriptions = [];
2312
- if ('BroadcastChannel' in window) {
2312
+ if (typeof window !== 'undefined' && 'BroadcastChannel' in window) {
2313
2313
  this.channel = new BroadcastChannel(bus);
2314
2314
  this.channel.onmessageerror = (e) => {
2315
2315
  console.error('[BroadcastChannelManager] Message receive error', e);
@@ -2341,15 +2341,26 @@ class BroadcastChannelManager {
2341
2341
  }
2342
2342
  /** @internal Implementation that handles all overloads. */
2343
2343
  sendMessage(messageIdOrBag, dataOrDelay, delay) {
2344
- const effectiveDelay = (typeof dataOrDelay === 'number' ? dataOrDelay : delay) ?? 0;
2345
- setTimeout(() => {
2346
- if (typeof messageIdOrBag === 'string') {
2347
- this.channel?.postMessage({ messageId: messageIdOrBag, data: dataOrDelay });
2348
- }
2349
- else {
2350
- this.channel?.postMessage(messageIdOrBag);
2351
- }
2352
- }, effectiveDelay);
2344
+ // Disambiguate overloads based on the first argument:
2345
+ // - string => (messageId, data?, delay?)
2346
+ // - object => (bag, delay?)
2347
+ let bag;
2348
+ let effectiveDelay;
2349
+ if (typeof messageIdOrBag === 'string') {
2350
+ bag = { messageId: messageIdOrBag, data: dataOrDelay };
2351
+ effectiveDelay = delay ?? 0;
2352
+ }
2353
+ else {
2354
+ bag = messageIdOrBag;
2355
+ effectiveDelay = (typeof dataOrDelay === 'number' ? dataOrDelay : 0);
2356
+ }
2357
+ const post = () => this.channel?.postMessage(bag);
2358
+ if (effectiveDelay > 0) {
2359
+ setTimeout(post, effectiveDelay);
2360
+ }
2361
+ else {
2362
+ post();
2363
+ }
2353
2364
  }
2354
2365
  /**
2355
2366
  * Registers a handler for messages with the given `messageId`.
@@ -2398,11 +2409,12 @@ class BroadcastService {
2398
2409
  * Useful when a caller needs direct channel access outside the service.
2399
2410
  * @returns A new `BroadcastChannelManager` connected to `'ARS-CHANNEL'`.
2400
2411
  */
2401
- static GetChannel() {
2412
+ static createChannel() {
2402
2413
  return new BroadcastChannelManager('ARS-CHANNEL');
2403
2414
  }
2404
2415
  ngOnDestroy() {
2405
2416
  this.channel?.dispose();
2417
+ this.subject.complete();
2406
2418
  }
2407
2419
  /**
2408
2420
  * Publishes a message to the in-process subject, optionally after a delay.
@@ -2412,9 +2424,12 @@ class BroadcastService {
2412
2424
  */
2413
2425
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
2414
2426
  sendMessage(id, data, delay = 0) {
2415
- setTimeout(() => {
2427
+ if (delay > 0) {
2428
+ setTimeout(() => this.subject.next({ id, data }), delay);
2429
+ }
2430
+ else {
2416
2431
  this.subject.next({ id, data });
2417
- }, delay);
2432
+ }
2418
2433
  }
2419
2434
  /**
2420
2435
  * Publishes a typed message over the native `BroadcastChannel` (cross-tab), optionally after a delay.
@@ -2433,6 +2448,13 @@ class BroadcastService {
2433
2448
  subscribeChannelMessage(id, action) {
2434
2449
  this.channel.subscribe({ messageId: id, action });
2435
2450
  }
2451
+ /**
2452
+ * Removes the channel subscription previously registered for the given `id`, if any.
2453
+ * @param id - The message type identifier whose subscription should be removed.
2454
+ */
2455
+ unsubscribeChannelMessage(id) {
2456
+ this.channel.unsubscribe(id);
2457
+ }
2436
2458
  /**
2437
2459
  * Returns an `Observable` that emits every in-process message published via `sendMessage`.
2438
2460
  * @returns An observable stream of `BroadcastMessageInfo` objects.