Skip to content

Commit

Permalink
fix(icon-service): Dispose of broadcast channel on pagehide (#1419)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkaraivanov authored Oct 4, 2024
1 parent 60f20fb commit 573f248
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/components/icon/icon-state.broadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
import { ActionType } from './registry/types.js';

export class IconsStateBroadcast {
private iconBroadcastChannel: BroadcastChannel;
private channel!: BroadcastChannel | null;
private collections: IconsCollection<SvgIcon>;
private refsCollection: IconsCollection<IconMeta>;

Expand All @@ -18,12 +18,16 @@ export class IconsStateBroadcast {
) {
this.collections = collections;
this.refsCollection = refsCollection;
this.iconBroadcastChannel = new BroadcastChannel('ignite-ui-icon-channel');
this.iconBroadcastChannel.addEventListener('message', this);
this.create();

globalThis.addEventListener('pageshow', () => this.create());
globalThis.addEventListener('pagehide', () => this.dispose());
}

public send(data: BroadcastIconsChangeMessage) {
this.iconBroadcastChannel.postMessage(data);
if (this.channel) {
this.channel.postMessage(data);
}
}

public handleEvent({ data }: MessageEvent<BroadcastIconsChangeMessage>) {
Expand All @@ -38,6 +42,22 @@ export class IconsStateBroadcast {
});
}

private create() {
if (!this.channel) {
this.channel = new BroadcastChannel('ignite-ui-icon-channel');
this.channel.addEventListener('message', this);
}
}

/* c8 ignore next 7 */
private dispose() {
if (this.channel) {
this.channel.removeEventListener('message', this);
this.channel.close();
this.channel = null;
}
}

private getUserRefsCollection(collections: IconsCollection<IconMeta>) {
const userSetIcons = createIconDefaultMap<string, IconMeta>();
for (const [collectionKey, collection] of collections.entries()) {
Expand Down

0 comments on commit 573f248

Please sign in to comment.