UNPKG

@yext/chat-headless

Version:

A state manager library powered by Redux for Yext Chat integrations

1 lines 3.28 kB
{"version":3,"file":"ReduxStateManager.mjs","sources":["../../../src/ReduxStateManager.ts"],"sourcesContent":["import {\n configureStore,\n combineReducers,\n EnhancedStore,\n Unsubscribe,\n Action,\n} from \"@reduxjs/toolkit\";\nimport conversationReducer from \"./slices/conversation\";\nimport metaReducer from \"./slices/meta\";\nimport { State } from \"./models/state\";\nimport { StateListener } from \"./models/utils/StateListeners\";\n\n/**\n * A Redux-backed implementation of the {@link StateManager} interface. Redux is used to\n * manage the state, dispatch events, and register state listeners.\n *\n * @internal\n */\nexport class ReduxStateManager {\n private store: EnhancedStore;\n\n constructor() {\n const coreReducer = combineReducers({\n conversation: conversationReducer,\n meta: metaReducer,\n });\n this.store = configureStore({\n reducer: (state, action) => {\n return action.type === \"set-state\"\n ? action.payload\n : coreReducer(state, action);\n },\n });\n }\n\n /**\n * Returns the current state.\n */\n getState(): State {\n return this.store.getState();\n }\n\n /**\n * Returns the Redux store.\n */\n getStore(): EnhancedStore {\n return this.store;\n }\n\n /**\n * Dispatches an event. This can update the {@link State}.\n *\n * @param action - represents an intention to change the state.\n * This includes \"type\" field for the action type and \"payload\" field for the data to dispatch\n */\n dispatch<T extends Action>(action: T): void {\n this.store.dispatch(action);\n }\n\n /**\n * Adds a listener for a specific state value of type T.\n *\n * @param listener - The state listener to add\n * @returns The function for removing the added listener\n */\n addListener<T>(listener: StateListener<T>): Unsubscribe {\n let previousValue = listener.valueAccessor(this.getState());\n return this.store.subscribe(() => {\n const currentValue: T = listener.valueAccessor(this.getState());\n if (currentValue !== previousValue) {\n previousValue = currentValue;\n listener.callback(currentValue);\n }\n });\n }\n}\n"],"names":[],"mappings":";;;;AAYA;;;;;AAKG;MACU,iBAAiB,CAAA;AACpB,IAAA,KAAK,CAAgB;AAE7B,IAAA,WAAA,GAAA;QACE,MAAM,WAAW,GAAG,eAAe,CAAC;AAClC,YAAA,YAAY,EAAE,mBAAmB;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