Match-id-4b75eabcccc40c58b54c47cc42540baa5dfe2a8c

This commit is contained in:
* 2022-07-30 15:14:14 +08:00 committed by *
commit 1225839303
3 changed files with 57 additions and 11 deletions

View File

@ -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();
};
});

View File

@ -15,7 +15,7 @@ function isPromise(obj: any): boolean {
type StoreConfig<S extends object, A extends UserActions<S>, C extends UserComputedValues<S>> = {
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<any, any, any>;
// _horizonXstore: StoreHandler<any, any, any>;
};
type StoreHandler<S extends object, A extends UserActions<S>, C extends UserComputedValues<S>> = {
$subscribe: (listener: () => void) => void;
$unsubscribe: (listener: () => void) => void;
$s: S;
$config: StoreConfig<S, A, C>;
// $config: StoreConfig<S, A, C>;
$queue: QueuedStoreActions<S, A>;
$a: StoreActions<S, A>;
$c: UserComputedValues<S>;
reduxHandler?: ReduxStoreHandler;
// reduxHandler?: ReduxStoreHandler;
} & { [K in keyof S]: S[K] } &
{ [K in keyof A]: Action<A[K], S> } &
{ [K in keyof C]: ReturnType<C[K]> };
@ -256,13 +256,15 @@ function hookStore() {
function createStoreHook<S extends object, A extends UserActions<S>, C extends UserComputedValues<S>>(
storeHandler: StoreHandler<S, A, C>
): () => StoreHandler<S, A, C> {
return () => {
const storeHook = () => {
if (!storeHandler.$config.options?.suppressHooks) {
hookStore();
}
return storeHandler;
};
return storeHook;
}
export function useStore<S extends object, A extends UserActions<S>, C extends UserComputedValues<S>>(

View File

@ -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 (
<div>
<button
id={BUTTON_ID}
onClick={() => {
incrementStore.doublePlusOne();
}}
>
+
</button>
<p id={RESULT_ID}>{incrementStore.count}</p>
</div>
);
}
Horizon.render(<App />, container);
Horizon.act(() => {
triggerClickEvent(container, BUTTON_ID);
});
expect(document.getElementById(RESULT_ID)?.innerHTML).toBe('5');
})
});