UNPKG

@ui5/webcomponents-react

Version:

React Wrapper for UI5 Web Components and additional components

45 lines 1.47 kB
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/Modals-${getCurrentRuntimeIndex()}/Listeners`); } function getStyleStoreSymbol() { return Symbol.for(`@ui5/webcomponents-react/Modals-${getCurrentRuntimeIndex()}`); } const initialStore = []; function getListeners() { STORE_LOCATION[getStyleStoreListenersSymbol()] ??= []; return STORE_LOCATION[getStyleStoreListenersSymbol()]; } function emitChange() { for (const listener of getListeners()) { listener(); } } function getSnapshot() { STORE_LOCATION[getStyleStoreSymbol()] ??= initialStore; return STORE_LOCATION[getStyleStoreSymbol()]; } function subscribe(listener) { const listeners = getListeners(); STORE_LOCATION[getStyleStoreListenersSymbol()] = [...listeners, listener]; return () => { STORE_LOCATION[getStyleStoreListenersSymbol()] = listeners.filter(l => l !== listener); }; } export const ModalStore = { subscribe, getSnapshot, getServerSnapshot: () => { return initialStore; }, addModal(config) { STORE_LOCATION[getStyleStoreSymbol()] = [...getSnapshot(), config]; emitChange(); }, removeModal(id) { STORE_LOCATION[getStyleStoreSymbol()] = getSnapshot().filter(modal => modal.id !== id); emitChange(); } };