Skip to content

Commit 84ca52a

Browse files
fix(icon-broadcast): Do not sync between wc services, just with angul… (#1532)
* fix(icon-broadcast): Do not sync between wc services, just with angular-elements. --------- Co-authored-by: Radoslav Karaivanov <[email protected]>
1 parent db36317 commit 84ca52a

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/components/icon/icon-state.broadcast.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class IconsStateBroadcast {
1111
private channel!: BroadcastChannel | null;
1212
private collections: IconsCollection<SvgIcon>;
1313
private refsCollection: IconsCollection<IconMeta>;
14+
private static readonly origin = 'igniteui-webcomponents';
1415

1516
constructor(
1617
collections: IconsCollection<SvgIcon>,
@@ -31,14 +32,19 @@ export class IconsStateBroadcast {
3132
}
3233

3334
public handleEvent({ data }: MessageEvent<BroadcastIconsChangeMessage>) {
34-
if (data.actionType !== ActionType.SyncState) {
35+
// no need to sync with other wc icon services, just with angular elements
36+
if (
37+
data.actionType !== ActionType.SyncState ||
38+
data.origin === IconsStateBroadcast.origin
39+
) {
3540
return;
3641
}
3742

3843
this.send({
3944
actionType: ActionType.SyncState,
4045
collections: this.getUserSetCollection(this.collections).toMap(),
4146
references: this.getUserRefsCollection(this.refsCollection).toMap(),
47+
origin: IconsStateBroadcast.origin,
4248
});
4349
}
4450

src/components/icon/icon.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
aTimeout,
23
elementUpdated,
34
expect,
45
fixture,
@@ -9,13 +10,15 @@ import { stub } from 'sinon';
910

1011
import { defineComponents } from '../common/definitions/defineComponents.js';
1112
import { first, last } from '../common/util.js';
13+
import { IconsStateBroadcast } from './icon-state.broadcast.js';
1214
import IgcIconComponent from './icon.js';
1315
import {
1416
getIconRegistry,
1517
registerIcon,
1618
registerIconFromText,
1719
setIconRef,
1820
} from './icon.registry.js';
21+
import { createIconDefaultMap } from './registry/default-map.js';
1922
import {
2023
ActionType,
2124
type BroadcastIconsChangeMessage,
@@ -251,6 +254,31 @@ describe('Icon broadcast service', () => {
251254

252255
expect(events.length).to.equal(0);
253256
});
257+
258+
it('when multiple broadcast services are initialized they should not send sync events to each other.', async () => {
259+
const collections = createIconDefaultMap<string, SvgIcon>();
260+
const references = createIconDefaultMap<string, IconMeta>();
261+
// 2 new broadcasts
262+
const broadcast1 = new IconsStateBroadcast(collections, references);
263+
const broadcast2 = new IconsStateBroadcast(collections, references);
264+
// 1 global one, initialized when you get the icon registry first time.
265+
const iconReg = getIconRegistry();
266+
// total - 3 services now.
267+
268+
// a peer is requesting a state sync
269+
channel.postMessage({ actionType: ActionType.SyncState });
270+
await aTimeout(20);
271+
272+
// all icon broadcasts must respond with their state
273+
// 2 from broadcast service + 1 from global.
274+
expect(events.length).to.equal(3);
275+
276+
// dispose of mock services.
277+
// biome-ignore lint/complexity/useLiteralKeys: private access escape
278+
broadcast1['dispose']();
279+
// biome-ignore lint/complexity/useLiteralKeys: private access escape
280+
broadcast2['dispose']();
281+
});
254282
});
255283

256284
describe('Peer registry', () => {

src/components/icon/registry/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface BroadcastIconsChangeMessage {
3535
actionType: ActionType;
3636
collections?: Map<string, Map<string, SvgIcon>>;
3737
references?: Map<string, Map<string, IconMeta>>;
38+
origin?: string;
3839
}
3940

4041
// Exported public types

0 commit comments

Comments
 (0)