Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add runtime index to global Modal and Style store #6248

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions packages/base/src/stores/I18nStore.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type I18nBundle from '@ui5/webcomponents-base/dist/i18nBundle.js';
import { getI18nBundle } from '@ui5/webcomponents-base/dist/i18nBundle.js';

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

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

function getListeners(): Array<() => void> {
globalThis[STORE_SYMBOL_LISTENERS] ??= [];
return globalThis[STORE_SYMBOL_LISTENERS];
STORE_LOCATION[STORE_SYMBOL_LISTENERS] ??= [];
return STORE_LOCATION[STORE_SYMBOL_LISTENERS];
}

function emitChange() {
Expand All @@ -18,15 +20,15 @@ function emitChange() {
}

function getSnapshot(): Record<string, I18nBundle> {
globalThis[STORE_SYMBOL] ??= initialStore;
return globalThis[STORE_SYMBOL];
STORE_LOCATION[STORE_SYMBOL] ??= initialStore;
return STORE_LOCATION[STORE_SYMBOL];
}

function subscribe(listener: () => void) {
const listeners = getListeners();
globalThis[STORE_SYMBOL_LISTENERS] = [...listeners, listener];
STORE_LOCATION[STORE_SYMBOL_LISTENERS] = [...listeners, listener];
return () => {
globalThis[STORE_SYMBOL_LISTENERS] = listeners.filter((l) => l !== listener);
STORE_LOCATION[STORE_SYMBOL_LISTENERS] = listeners.filter((l) => l !== listener);
};
}

Expand All @@ -40,8 +42,8 @@ export const I18nStore = {
const bundles = getSnapshot();
if (!bundles.hasOwnProperty(bundleName)) {
void getI18nBundle(bundleName).then((bundle) => {
globalThis[STORE_SYMBOL] = {
...globalThis[STORE_SYMBOL],
STORE_LOCATION[STORE_SYMBOL] = {
...STORE_LOCATION[STORE_SYMBOL],
[bundleName]: bundle
};
emitChange();
Expand All @@ -52,7 +54,7 @@ export const I18nStore = {
const bundles = getSnapshot();
const newBundles = await Promise.all(Object.keys(bundles).map((bundleName) => getI18nBundle(bundleName)));

globalThis[STORE_SYMBOL] = newBundles.reduce(
STORE_LOCATION[STORE_SYMBOL] = newBundles.reduce(
(acc, bundle) => ({
...acc,
[bundle.packageName]: bundle
Expand Down
28 changes: 19 additions & 9 deletions packages/base/src/stores/StyleStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const STORE_SYMBOL_LISTENERS = Symbol.for('@ui5/webcomponents-react/StyleStore/Listeners');
const STORE_SYMBOL = Symbol.for('@ui5/webcomponents-react/StyleStore');
import { getCurrentRuntimeIndex } from '@ui5/webcomponents-base/dist/Runtimes.js';

globalThis['@ui5/webcomponents-react'] ??= {};
const STORE_LOCATION = globalThis['@ui5/webcomponents-react'];

function getStyleStoreListenersSymbol() {
return Symbol.for(`@ui5/webcomponents-react/StyleStore-${getCurrentRuntimeIndex()}/Listeners`);
}

function getStyleStoreSymbol() {
return Symbol.for(`@ui5/webcomponents-react/StyleStore-${getCurrentRuntimeIndex()}`);
}

interface IStyleStore {
staticCssInjected: boolean;
Expand All @@ -12,8 +22,8 @@ const initialStore: IStyleStore = {
};

function getListeners(): Array<() => void> {
globalThis[STORE_SYMBOL_LISTENERS] ??= [];
return globalThis[STORE_SYMBOL_LISTENERS];
STORE_LOCATION[getStyleStoreListenersSymbol()] ??= [];
return STORE_LOCATION[getStyleStoreListenersSymbol()];
}

function emitChange() {
Expand All @@ -23,15 +33,15 @@ function emitChange() {
}

function getSnapshot(): IStyleStore {
globalThis[STORE_SYMBOL] ??= initialStore;
return globalThis[STORE_SYMBOL];
STORE_LOCATION[getStyleStoreSymbol()] ??= initialStore;
return STORE_LOCATION[getStyleStoreSymbol()];
}

function subscribe(listener: () => void) {
const listeners = getListeners();
globalThis[STORE_SYMBOL_LISTENERS] = [...listeners, listener];
STORE_LOCATION[getStyleStoreListenersSymbol()] = [...listeners, listener];
return () => {
globalThis[STORE_SYMBOL_LISTENERS] = listeners.filter((l) => l !== listener);
STORE_LOCATION[getStyleStoreListenersSymbol()] = listeners.filter((l) => l !== listener);
};
}

Expand All @@ -43,7 +53,7 @@ export const StyleStore = {
},
setStaticCssInjected: (staticCssInjected: boolean) => {
const curr = getSnapshot();
globalThis[STORE_SYMBOL] = {
STORE_LOCATION[getStyleStoreSymbol()] = {
...curr,
staticCssInjected
};
Expand Down
29 changes: 19 additions & 10 deletions packages/main/src/internal/ModalStore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { getCurrentRuntimeIndex } from '@ui5/webcomponents-base/dist/Runtimes.js';
import type { ComponentType, RefCallback, RefObject } from 'react';

const STORE_SYMBOL_LISTENERS = Symbol.for('@ui5/webcomponents-react/Modals/Listeners');
const STORE_SYMBOL = Symbol.for('@ui5/webcomponents-react/Modals');
globalThis['@ui5/webcomponents-react'] ??= {};
const STORE_LOCATION = globalThis['@ui5/webcomponents-react'];

function getStyleStoreListenersSymbol() {
return Symbol.for(`@ui5/webcomponents-react/Modals-${getCurrentRuntimeIndex()}/Listeners`);
}

function getStyleStoreSymbol() {
return Symbol.for(`@ui5/webcomponents-react/Modals-${getCurrentRuntimeIndex()}`);
}

type IModal = {
Component: ComponentType;
Expand All @@ -14,8 +23,8 @@ type IModal = {
const initialStore: IModal[] = [];

function getListeners(): Array<() => void> {
globalThis[STORE_SYMBOL_LISTENERS] ??= [];
return globalThis[STORE_SYMBOL_LISTENERS];
STORE_LOCATION[getStyleStoreListenersSymbol()] ??= [];
return STORE_LOCATION[getStyleStoreListenersSymbol()];
}

function emitChange() {
Expand All @@ -25,15 +34,15 @@ function emitChange() {
}

function getSnapshot(): IModal[] {
globalThis[STORE_SYMBOL] ??= initialStore;
return globalThis[STORE_SYMBOL];
STORE_LOCATION[getStyleStoreSymbol()] ??= initialStore;
return STORE_LOCATION[getStyleStoreSymbol()];
}

function subscribe(listener: () => void) {
const listeners = getListeners();
globalThis[STORE_SYMBOL_LISTENERS] = [...listeners, listener];
STORE_LOCATION[getStyleStoreListenersSymbol()] = [...listeners, listener];
return () => {
globalThis[STORE_SYMBOL_LISTENERS] = listeners.filter((l) => l !== listener);
STORE_LOCATION[getStyleStoreListenersSymbol()] = listeners.filter((l) => l !== listener);
};
}

Expand All @@ -44,11 +53,11 @@ export const ModalStore = {
return initialStore;
},
addModal(config: IModal) {
globalThis[STORE_SYMBOL] = [...getSnapshot(), config];
STORE_LOCATION[getStyleStoreSymbol()] = [...getSnapshot(), config];
emitChange();
},
removeModal(id: string) {
globalThis[STORE_SYMBOL] = getSnapshot().filter((modal) => modal.id !== id);
STORE_LOCATION[getStyleStoreSymbol()] = getSnapshot().filter((modal) => modal.id !== id);
emitChange();
}
};
Loading