diff --git a/libs/horizon/src/horizonx/adapters/reduxReact.ts b/libs/horizon/src/horizonx/adapters/reduxReact.ts index 8e205eb7..0954fbb3 100644 --- a/libs/horizon/src/horizonx/adapters/reduxReact.ts +++ b/libs/horizon/src/horizonx/adapters/reduxReact.ts @@ -33,12 +33,11 @@ export function createSelectorHook(context: Context): (selector?: (any) => any) return function(selector = state => state) { const [b, fr] = useState(false); - const listener = () => { - fr(!b); - }; - useEffect(() => { - return store.subscribe(listener); + const unsubscribe = store.subscribe(() => fr(!b)); + return () => { + unsubscribe(); + }; }); return selector(store.getState()); @@ -110,7 +109,7 @@ export function connect( useEffect(() => { const unsubscribe = store.subscribe(() => forceReload(!f)); - () => { + return () => { unsubscribe(); }; }); diff --git a/libs/horizon/src/horizonx/store/StoreHandler.ts b/libs/horizon/src/horizonx/store/StoreHandler.ts index ffc67aa8..ecbe22d9 100644 --- a/libs/horizon/src/horizonx/store/StoreHandler.ts +++ b/libs/horizon/src/horizonx/store/StoreHandler.ts @@ -15,7 +15,7 @@ function isPromise(obj: any): boolean { type StoreConfig, C extends UserComputedValues> = { state?: S; - options?: { suppressHooks?: boolean }; + // options?: { suppressHooks?: boolean }; actions?: A; id?: string; computed?: C; @@ -27,18 +27,18 @@ export type ReduxStoreHandler = { getState: () => any; subscribe: (listener: () => void) => () => void; replaceReducer: (reducer: (state: any, action: { type: string }) => any) => void; - _horizonXstore: StoreHandler; + // _horizonXstore: StoreHandler; }; type StoreHandler, C extends UserComputedValues> = { $subscribe: (listener: () => void) => void; $unsubscribe: (listener: () => void) => void; $s: S; - $config: StoreConfig; + // $config: StoreConfig; $queue: QueuedStoreActions; $a: StoreActions; $c: UserComputedValues; - reduxHandler?: ReduxStoreHandler; + // reduxHandler?: ReduxStoreHandler; } & { [K in keyof S]: S[K] } & { [K in keyof A]: Action } & { [K in keyof C]: ReturnType }; @@ -256,13 +256,15 @@ function hookStore() { function createStoreHook, C extends UserComputedValues>( storeHandler: StoreHandler ): () => StoreHandler { - return () => { + const storeHook = () => { if (!storeHandler.$config.options?.suppressHooks) { hookStore(); } return storeHandler; }; + + return storeHook; } export function useStore, C extends UserComputedValues>( diff --git a/scripts/__tests__/HorizonXText/StoreFunctionality/basicAccess.test.tsx b/scripts/__tests__/HorizonXText/StoreFunctionality/basicAccess.test.tsx index 0a2c8584..9d95c95f 100644 --- a/scripts/__tests__/HorizonXText/StoreFunctionality/basicAccess.test.tsx +++ b/scripts/__tests__/HorizonXText/StoreFunctionality/basicAccess.test.tsx @@ -107,4 +107,49 @@ describe('Basic store manipulation', () => { expect(document.getElementById(RESULT_ID)?.innerHTML).toBe('5'); }); + + it('should call computed from own actions', () => { + const useIncrementStore = createStore({ + id: 'incrementStore', + state: { + count: 2, + }, + actions: { + doublePlusOne: function(state) { + state.count = this.double + 1; + }, + }, + computed:{ + double: (state) => { + return state.count*2 + } + } + }); + + function App() { + const incrementStore = useIncrementStore(); + + return ( +
+ +

{incrementStore.count}

+
+ ); + } + + Horizon.render(, container); + + Horizon.act(() => { + triggerClickEvent(container, BUTTON_ID); + }); + + expect(document.getElementById(RESULT_ID)?.innerHTML).toBe('5'); + }) });