Match-id-2d85e7b702d4272d993947bdb2536d54112a3111
This commit is contained in:
parent
ac98a11381
commit
e275c108dc
|
@ -1,20 +1,18 @@
|
||||||
import type { Hook, Reducer, Trigger, Update } from './HookType';
|
import type { Hook, Reducer, Trigger, Update } from './HookType';
|
||||||
import {
|
import { createHook, getCurrentHook, throwNotInFuncError } from './BaseHook';
|
||||||
createHook,
|
import { launchUpdateFromVNode } from '../TreeBuilder';
|
||||||
getCurrentHook,
|
|
||||||
throwNotInFuncError
|
|
||||||
} from './BaseHook';
|
|
||||||
import {
|
|
||||||
launchUpdateFromVNode
|
|
||||||
} from '../TreeBuilder';
|
|
||||||
import { isSame } from '../utils/compare';
|
import { isSame } from '../utils/compare';
|
||||||
import { setStateChange } from '../render/FunctionComponent';
|
import { setStateChange } from '../render/FunctionComponent';
|
||||||
import { getHookStage, HookStage } from './HookStage';
|
import { getHookStage, HookStage } from './HookStage';
|
||||||
import type { VNode } from '../Types';
|
import type { VNode } from '../Types';
|
||||||
import {getProcessingVNode} from '../GlobalVar';
|
import { getProcessingVNode } from '../GlobalVar';
|
||||||
|
|
||||||
export function useReducerImpl<S, P, A>(reducer: (S, A) =>
|
export function useReducerImpl<S, P, A>(
|
||||||
S, initArg: P, init?: (P) => S, isUseState?: boolean): [S, Trigger<A>] | void {
|
reducer: (S, A) => S,
|
||||||
|
initArg: P,
|
||||||
|
init?: (P) => S,
|
||||||
|
isUseState?: boolean
|
||||||
|
): [S, Trigger<A>] | void {
|
||||||
const stage = getHookStage();
|
const stage = getHookStage();
|
||||||
if (stage === null) {
|
if (stage === null) {
|
||||||
throwNotInFuncError();
|
throwNotInFuncError();
|
||||||
|
@ -53,16 +51,19 @@ function insertUpdate<S, A>(action: A, hook: Hook<S, A>): Update<S, A> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// setState, setReducer触发函数
|
// setState, setReducer触发函数
|
||||||
export function TriggerAction<S, A, T>(vNode: VNode, hook: Hook<S, A>, action: A) {
|
export function TriggerAction<S, A>(vNode: VNode, hook: Hook<S, A>, isUseState: boolean, action: A): void {
|
||||||
const newUpdate = insertUpdate(action, hook);
|
const newUpdate = insertUpdate(action, hook);
|
||||||
|
|
||||||
// 判断是否需要刷新
|
// 判断是否需要刷新
|
||||||
if (!vNode.shouldUpdate) {
|
if (!vNode.shouldUpdate && isUseState) {
|
||||||
const reducerObj = hook.state as Reducer<S, A>;
|
const { stateValue, reducer } = hook.state as Reducer<S, A>;
|
||||||
const { stateValue, reducer } = reducerObj;
|
|
||||||
|
|
||||||
|
if (reducer === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
// 在进入render阶段前reducer没有变化,可以复用state值,提升性能
|
// 在进入render阶段前reducer没有变化,可以复用state值,提升性能
|
||||||
newUpdate.state = reducer(stateValue, action);
|
newUpdate.state = reducer(stateValue, action);
|
||||||
|
|
||||||
// 标记为已经计算过,不需要重新计算了
|
// 标记为已经计算过,不需要重新计算了
|
||||||
newUpdate.didCalculated = true;
|
newUpdate.didCalculated = true;
|
||||||
|
|
||||||
|
@ -87,16 +88,17 @@ export function useReducerForInit<S, A>(reducer, initArg, init, isUseState?: boo
|
||||||
}
|
}
|
||||||
|
|
||||||
const hook = createHook();
|
const hook = createHook();
|
||||||
|
const trigger = TriggerAction.bind(null, getProcessingVNode(), hook, isUseState);
|
||||||
// 为hook.state赋值{状态值, 触发函数, reducer, updates更新数组, 是否是useState}
|
// 为hook.state赋值{状态值, 触发函数, reducer, updates更新数组, 是否是useState}
|
||||||
hook.state = {
|
hook.state = {
|
||||||
stateValue: stateValue,
|
stateValue: stateValue,
|
||||||
trigger: TriggerAction.bind(null, getProcessingVNode(), hook),
|
trigger,
|
||||||
reducer,
|
reducer,
|
||||||
updates: null,
|
updates: null,
|
||||||
isUseState
|
isUseState,
|
||||||
} as Reducer<S, A>;
|
} as Reducer<S, A>;
|
||||||
|
|
||||||
return [hook.state.stateValue, hook.state.trigger];
|
return [hook.state.stateValue, trigger];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新hook.state
|
// 更新hook.state
|
||||||
|
@ -136,5 +138,3 @@ function calculateNewState<S, A>(currentHookUpdates: Array<Update<S, A>>, curren
|
||||||
|
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,25 +13,25 @@ describe('useReducer Hook Test', () => {
|
||||||
return {
|
return {
|
||||||
...intlCar,
|
...intlCar,
|
||||||
logo: 'ford',
|
logo: 'ford',
|
||||||
price: 76
|
price: 76,
|
||||||
};
|
};
|
||||||
case 'bmw':
|
case 'bmw':
|
||||||
return {
|
return {
|
||||||
...intlCar,
|
...intlCar,
|
||||||
logo: 'bmw',
|
logo: 'bmw',
|
||||||
price: 100
|
price: 100,
|
||||||
};
|
};
|
||||||
case 'benz':
|
case 'benz':
|
||||||
return {
|
return {
|
||||||
...intlCar,
|
...intlCar,
|
||||||
logo: 'benz',
|
logo: 'benz',
|
||||||
price: 80
|
price: 80,
|
||||||
};
|
};
|
||||||
default:
|
default:
|
||||||
return {
|
return {
|
||||||
...intlCar,
|
...intlCar,
|
||||||
logo: 'audi',
|
logo: 'audi',
|
||||||
price: 88
|
price: 88,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -56,4 +56,34 @@ describe('useReducer Hook Test', () => {
|
||||||
expect(container.querySelector('p').innerHTML).toBe('audi');
|
expect(container.querySelector('p').innerHTML).toBe('audi');
|
||||||
expect(container.querySelector('#senP').innerHTML).toBe('88');
|
expect(container.querySelector('#senP').innerHTML).toBe('88');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('dispatch只触发一次', () => {
|
||||||
|
let nextId = 1;
|
||||||
|
const reducer = () => {
|
||||||
|
return { data: nextId++ };
|
||||||
|
};
|
||||||
|
const btnRef = Horizon.createRef();
|
||||||
|
const Main = () => {
|
||||||
|
const [{ data }, dispatch] = useReducer(reducer, { data: 0 });
|
||||||
|
const dispatchLogging = () => {
|
||||||
|
console.log('dispatch is called once');
|
||||||
|
dispatch();
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<button ref={btnRef} onClick={() => dispatchLogging()}>
|
||||||
|
increment
|
||||||
|
</button>
|
||||||
|
<div>{data}</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
Horizon.render(<Main />, container);
|
||||||
|
Horizon.act(() => {
|
||||||
|
btnRef.current.click();
|
||||||
|
});
|
||||||
|
expect(nextId).toBe(2);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue