Skip to content

Commit e428829

Browse files
authored
refactor(elements): Close icon service broadcast channel on pagehide (#14874)
1 parent a34e44c commit e428829

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

projects/igniteui-angular-elements/src/lib/icon.broadcast.service.ts

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,68 @@ import { IconMeta } from '../../../igniteui-angular/src/lib/icon/public_api';
77
export interface SvgIcon {
88
svg: string;
99
title?: string;
10-
}
10+
}
1111

12-
export type Collection<T, U> = Map<T, U>;
12+
export type Collection<T, U> = Map<T, U>;
1313

14-
export enum ActionType {
14+
export enum ActionType {
1515
SyncState = 0,
1616
RegisterIcon = 1,
1717
UpdateIconReference = 2,
18-
}
18+
}
1919

20-
export interface BroadcastIconsChangeMessage {
20+
export interface BroadcastIconsChangeMessage {
2121
actionType: ActionType;
2222
collections?: Collection<string, Map<string, SvgIcon>>;
2323
references?: Collection<string, Map<string, IconMeta>>;
24-
}
24+
}
2525

2626
/** @hidden @internal **/
2727
@Injectable()
2828
export class IgxIconBroadcastService {
29-
private iconBroadcastChannel: BroadcastChannel;
29+
private iconBroadcastChannel: BroadcastChannel | null;
30+
3031
constructor(
3132
protected _iconService: IgxIconService,
3233
@Optional() private _platformUtil: PlatformUtil
3334
) {
3435
if (this._platformUtil?.isBrowser) {
35-
// open broadcast channel for sync with wc icon service.
36-
this.iconBroadcastChannel = new BroadcastChannel("ignite-ui-icon-channel");
37-
this.iconBroadcastChannel.onmessage = (event) => {
38-
const message = event.data as BroadcastIconsChangeMessage;
39-
if (message.actionType === ActionType.SyncState ||
40-
message.actionType === ActionType.RegisterIcon) {
41-
this.updateIconsFromCollection(message.collections);
42-
}
43-
44-
if (message.actionType === ActionType.SyncState ||
45-
message.actionType === ActionType.UpdateIconReference) {
46-
this.updateRefsFromCollection(message.references);
47-
}
48-
};
49-
// send message to sync state
36+
this.create();
37+
38+
globalThis.addEventListener('pageshow', () => this.create())
39+
globalThis.addEventListener('pagehide', () => this.dispose())
40+
}
41+
}
42+
43+
public handleEvent({ data }: MessageEvent<BroadcastIconsChangeMessage>) {
44+
const { actionType, collections, references } = data;
45+
46+
if (actionType === ActionType.SyncState || ActionType.RegisterIcon) {
47+
this.updateIconsFromCollection(collections);
48+
}
49+
50+
if (actionType === ActionType.SyncState || ActionType.UpdateIconReference) {
51+
this.updateRefsFromCollection(references);
52+
}
53+
}
54+
55+
private create() {
56+
if (!this.iconBroadcastChannel) {
57+
this.iconBroadcastChannel = new BroadcastChannel('ignite-ui-icon-channel');
58+
this.iconBroadcastChannel.addEventListener('message', this);
59+
60+
// sync state
5061
this.iconBroadcastChannel.postMessage({
5162
actionType: ActionType.SyncState
52-
});
63+
})
64+
}
65+
}
66+
67+
private dispose() {
68+
if (this.iconBroadcastChannel) {
69+
this.iconBroadcastChannel.removeEventListener('message', this);
70+
this.iconBroadcastChannel.close();
71+
this.iconBroadcastChannel = null;
5372
}
5473
}
5574

0 commit comments

Comments
 (0)