Skip to content

Commit 5e62e3e

Browse files
committed
Create unified shaped array
1 parent 027142c commit 5e62e3e

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

hooks/src/index.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ options._render = vnode => {
6060
if (previousComponent === currentComponent) {
6161
hooks._pendingEffects = [];
6262
currentComponent._renderCallbacks = [];
63-
hooks._list.forEach(hookItem => {
63+
hooks._list.some(hookItem => {
6464
if (hookItem._nextValue) {
6565
hookItem._value = hookItem._nextValue;
6666
}
@@ -83,7 +83,7 @@ options.diffed = vnode => {
8383
const c = vnode._component;
8484
if (c && c.__hooks) {
8585
if (c.__hooks._pendingEffects.length) afterPaint(afterPaintEffects.push(c));
86-
c.__hooks._list.forEach(hookItem => {
86+
c.__hooks._list.some(hookItem => {
8787
if (hookItem._pendingArgs) {
8888
hookItem._args = hookItem._pendingArgs;
8989
}
@@ -121,7 +121,7 @@ options.unmount = vnode => {
121121
const c = vnode._component;
122122
if (c && c.__hooks) {
123123
let hasErrored;
124-
c.__hooks._list.forEach(s => {
124+
c.__hooks._list.some(s => {
125125
try {
126126
invokeCleanup(s);
127127
} catch (e) {
@@ -133,6 +133,23 @@ options.unmount = vnode => {
133133
}
134134
};
135135

136+
/**
137+
*
138+
* @returns {import('./internal').HookState}
139+
*/
140+
function createHookState() {
141+
return {
142+
_value: undefined,
143+
_args: undefined,
144+
_cleanup: undefined,
145+
_pendingArgs: undefined,
146+
_component: undefined,
147+
_reducer: undefined,
148+
_context: undefined,
149+
_factory: undefined
150+
};
151+
}
152+
136153
/**
137154
* Get a hook's state from the currentComponent
138155
* @param {number} index The index of the hook to get
@@ -158,7 +175,7 @@ function getHookState(index, type) {
158175
});
159176

160177
if (index >= hooks._list.length) {
161-
hooks._list.push({});
178+
hooks._list.push(createHookState());
162179
}
163180

164181
return hooks._list[index];
@@ -248,7 +265,7 @@ export function useReducer(reducer, initialState, init) {
248265
let shouldUpdate =
249266
hookState._component.props !== p ||
250267
hooksList.every(x => !x._nextValue);
251-
hooksList.forEach(hookItem => {
268+
hooksList.some(hookItem => {
252269
if (hookItem._nextValue) {
253270
const currentValue = hookItem._value[0];
254271
hookItem._value = hookItem._nextValue;

hooks/src/internal.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export type HookState =
5656
interface BaseHookState {
5757
_value?: unknown;
5858
_nextValue?: unknown;
59-
_pendingValue?: unknown;
6059
_args?: unknown;
6160
_pendingArgs?: unknown;
6261
_component?: unknown;
@@ -75,7 +74,6 @@ export interface EffectHookState extends BaseHookState {
7574

7675
export interface MemoHookState<T = unknown> extends BaseHookState {
7776
_value?: T;
78-
_pendingValue?: T;
7977
_args?: unknown[];
8078
_pendingArgs?: unknown[];
8179
_factory?: () => T;

0 commit comments

Comments
 (0)