Skip to content

Commit 8184b4e

Browse files
fix: add runtime index to global Modal and Style store (SAP#6248)
1 parent 41cc7da commit 8184b4e

File tree

3 files changed

+49
-28
lines changed

3 files changed

+49
-28
lines changed

packages/base/src/stores/I18nStore.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import type I18nBundle from '@ui5/webcomponents-base/dist/i18nBundle.js';
22
import { getI18nBundle } from '@ui5/webcomponents-base/dist/i18nBundle.js';
33

4+
globalThis['@ui5/webcomponents-react'] ??= {};
5+
const STORE_LOCATION = globalThis['@ui5/webcomponents-react'];
46
const STORE_SYMBOL_LISTENERS = Symbol.for('@ui5/webcomponents-react/I18nStore/Listeners');
57
const STORE_SYMBOL = Symbol.for('@ui5/webcomponents-react/I18nStore');
68

79
const initialStore: Record<string, I18nBundle> = {};
810

911
function getListeners(): Array<() => void> {
10-
globalThis[STORE_SYMBOL_LISTENERS] ??= [];
11-
return globalThis[STORE_SYMBOL_LISTENERS];
12+
STORE_LOCATION[STORE_SYMBOL_LISTENERS] ??= [];
13+
return STORE_LOCATION[STORE_SYMBOL_LISTENERS];
1214
}
1315

1416
function emitChange() {
@@ -18,15 +20,15 @@ function emitChange() {
1820
}
1921

2022
function getSnapshot(): Record<string, I18nBundle> {
21-
globalThis[STORE_SYMBOL] ??= initialStore;
22-
return globalThis[STORE_SYMBOL];
23+
STORE_LOCATION[STORE_SYMBOL] ??= initialStore;
24+
return STORE_LOCATION[STORE_SYMBOL];
2325
}
2426

2527
function subscribe(listener: () => void) {
2628
const listeners = getListeners();
27-
globalThis[STORE_SYMBOL_LISTENERS] = [...listeners, listener];
29+
STORE_LOCATION[STORE_SYMBOL_LISTENERS] = [...listeners, listener];
2830
return () => {
29-
globalThis[STORE_SYMBOL_LISTENERS] = listeners.filter((l) => l !== listener);
31+
STORE_LOCATION[STORE_SYMBOL_LISTENERS] = listeners.filter((l) => l !== listener);
3032
};
3133
}
3234

@@ -40,8 +42,8 @@ export const I18nStore = {
4042
const bundles = getSnapshot();
4143
if (!bundles.hasOwnProperty(bundleName)) {
4244
void getI18nBundle(bundleName).then((bundle) => {
43-
globalThis[STORE_SYMBOL] = {
44-
...globalThis[STORE_SYMBOL],
45+
STORE_LOCATION[STORE_SYMBOL] = {
46+
...STORE_LOCATION[STORE_SYMBOL],
4547
[bundleName]: bundle
4648
};
4749
emitChange();
@@ -52,7 +54,7 @@ export const I18nStore = {
5254
const bundles = getSnapshot();
5355
const newBundles = await Promise.all(Object.keys(bundles).map((bundleName) => getI18nBundle(bundleName)));
5456

55-
globalThis[STORE_SYMBOL] = newBundles.reduce(
57+
STORE_LOCATION[STORE_SYMBOL] = newBundles.reduce(
5658
(acc, bundle) => ({
5759
...acc,
5860
[bundle.packageName]: bundle

packages/base/src/stores/StyleStore.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
const STORE_SYMBOL_LISTENERS = Symbol.for('@ui5/webcomponents-react/StyleStore/Listeners');
2-
const STORE_SYMBOL = Symbol.for('@ui5/webcomponents-react/StyleStore');
1+
import { getCurrentRuntimeIndex } from '@ui5/webcomponents-base/dist/Runtimes.js';
2+
3+
globalThis['@ui5/webcomponents-react'] ??= {};
4+
const STORE_LOCATION = globalThis['@ui5/webcomponents-react'];
5+
6+
function getStyleStoreListenersSymbol() {
7+
return Symbol.for(`@ui5/webcomponents-react/StyleStore-${getCurrentRuntimeIndex()}/Listeners`);
8+
}
9+
10+
function getStyleStoreSymbol() {
11+
return Symbol.for(`@ui5/webcomponents-react/StyleStore-${getCurrentRuntimeIndex()}`);
12+
}
313

414
interface IStyleStore {
515
staticCssInjected: boolean;
@@ -12,8 +22,8 @@ const initialStore: IStyleStore = {
1222
};
1323

1424
function getListeners(): Array<() => void> {
15-
globalThis[STORE_SYMBOL_LISTENERS] ??= [];
16-
return globalThis[STORE_SYMBOL_LISTENERS];
25+
STORE_LOCATION[getStyleStoreListenersSymbol()] ??= [];
26+
return STORE_LOCATION[getStyleStoreListenersSymbol()];
1727
}
1828

1929
function emitChange() {
@@ -23,15 +33,15 @@ function emitChange() {
2333
}
2434

2535
function getSnapshot(): IStyleStore {
26-
globalThis[STORE_SYMBOL] ??= initialStore;
27-
return globalThis[STORE_SYMBOL];
36+
STORE_LOCATION[getStyleStoreSymbol()] ??= initialStore;
37+
return STORE_LOCATION[getStyleStoreSymbol()];
2838
}
2939

3040
function subscribe(listener: () => void) {
3141
const listeners = getListeners();
32-
globalThis[STORE_SYMBOL_LISTENERS] = [...listeners, listener];
42+
STORE_LOCATION[getStyleStoreListenersSymbol()] = [...listeners, listener];
3343
return () => {
34-
globalThis[STORE_SYMBOL_LISTENERS] = listeners.filter((l) => l !== listener);
44+
STORE_LOCATION[getStyleStoreListenersSymbol()] = listeners.filter((l) => l !== listener);
3545
};
3646
}
3747

@@ -43,7 +53,7 @@ export const StyleStore = {
4353
},
4454
setStaticCssInjected: (staticCssInjected: boolean) => {
4555
const curr = getSnapshot();
46-
globalThis[STORE_SYMBOL] = {
56+
STORE_LOCATION[getStyleStoreSymbol()] = {
4757
...curr,
4858
staticCssInjected
4959
};
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
import { getCurrentRuntimeIndex } from '@ui5/webcomponents-base/dist/Runtimes.js';
12
import type { ComponentType, RefCallback, RefObject } from 'react';
23

3-
const STORE_SYMBOL_LISTENERS = Symbol.for('@ui5/webcomponents-react/Modals/Listeners');
4-
const STORE_SYMBOL = Symbol.for('@ui5/webcomponents-react/Modals');
4+
globalThis['@ui5/webcomponents-react'] ??= {};
5+
const STORE_LOCATION = globalThis['@ui5/webcomponents-react'];
6+
7+
function getStyleStoreListenersSymbol() {
8+
return Symbol.for(`@ui5/webcomponents-react/Modals-${getCurrentRuntimeIndex()}/Listeners`);
9+
}
10+
11+
function getStyleStoreSymbol() {
12+
return Symbol.for(`@ui5/webcomponents-react/Modals-${getCurrentRuntimeIndex()}`);
13+
}
514

615
type IModal = {
716
Component: ComponentType;
@@ -14,8 +23,8 @@ type IModal = {
1423
const initialStore: IModal[] = [];
1524

1625
function getListeners(): Array<() => void> {
17-
globalThis[STORE_SYMBOL_LISTENERS] ??= [];
18-
return globalThis[STORE_SYMBOL_LISTENERS];
26+
STORE_LOCATION[getStyleStoreListenersSymbol()] ??= [];
27+
return STORE_LOCATION[getStyleStoreListenersSymbol()];
1928
}
2029

2130
function emitChange() {
@@ -25,15 +34,15 @@ function emitChange() {
2534
}
2635

2736
function getSnapshot(): IModal[] {
28-
globalThis[STORE_SYMBOL] ??= initialStore;
29-
return globalThis[STORE_SYMBOL];
37+
STORE_LOCATION[getStyleStoreSymbol()] ??= initialStore;
38+
return STORE_LOCATION[getStyleStoreSymbol()];
3039
}
3140

3241
function subscribe(listener: () => void) {
3342
const listeners = getListeners();
34-
globalThis[STORE_SYMBOL_LISTENERS] = [...listeners, listener];
43+
STORE_LOCATION[getStyleStoreListenersSymbol()] = [...listeners, listener];
3544
return () => {
36-
globalThis[STORE_SYMBOL_LISTENERS] = listeners.filter((l) => l !== listener);
45+
STORE_LOCATION[getStyleStoreListenersSymbol()] = listeners.filter((l) => l !== listener);
3746
};
3847
}
3948

@@ -44,11 +53,11 @@ export const ModalStore = {
4453
return initialStore;
4554
},
4655
addModal(config: IModal) {
47-
globalThis[STORE_SYMBOL] = [...getSnapshot(), config];
56+
STORE_LOCATION[getStyleStoreSymbol()] = [...getSnapshot(), config];
4857
emitChange();
4958
},
5059
removeModal(id: string) {
51-
globalThis[STORE_SYMBOL] = getSnapshot().filter((modal) => modal.id !== id);
60+
STORE_LOCATION[getStyleStoreSymbol()] = getSnapshot().filter((modal) => modal.id !== id);
5261
emitChange();
5362
}
5463
};

0 commit comments

Comments
 (0)