Skip to content

Commit ccef29a

Browse files
author
Brian Vaughn
committed
Refactored hook to reflect changes in RFC
1 parent 3a2a112 commit ccef29a

File tree

12 files changed

+545
-265
lines changed

12 files changed

+545
-265
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ function getPrimitiveStackCache(): Map<string, Array<any>> {
7272
Dispatcher.useCallback(() => {});
7373
Dispatcher.useMemo(() => null);
7474
Dispatcher.useMutableSource(
75-
{},
7675
{
77-
getVersion: () => null,
76+
_source: {},
77+
_getVersion: () => 1,
78+
_workInProgressVersionPrimary: null,
79+
_workInProgressVersionSecondary: null,
80+
},
81+
{
7882
getSnapshot: () => null,
7983
subscribe: () => () => {},
8084
},
@@ -236,13 +240,14 @@ function useMemo<T>(
236240
return value;
237241
}
238242

239-
function useMutableSource<S>(
240-
source: MutableSource,
241-
config: MutableSourceHookConfig<S>,
242-
): S {
243+
export function useMutableSource<Source, Snapshot>(
244+
source: MutableSource<Source>,
245+
config: MutableSourceHookConfig<Source, Snapshot>,
246+
): Snapshot {
243247
const hook = nextHook();
244248
const getSnapshot = config.getSnapshot;
245-
const value = hook !== null ? hook.memoizedState.snapshot : getSnapshot();
249+
const value =
250+
hook !== null ? hook.memoizedState.snapshot : getSnapshot(source._source);
246251
hookLog.push({primitive: 'MutableSource', stackError: new Error(), value});
247252
return value;
248253
}

packages/react-dom/src/server/ReactPartialRendererHooks.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,13 @@ function useResponder(responder, props): ReactEventResponderListener<any, any> {
463463
};
464464
}
465465

466-
function useMutableSource<S>(
467-
source: MutableSource,
468-
config: MutableSourceHookConfig<S>,
469-
): S {
466+
function useMutableSource<Source, Snapshot>(
467+
source: MutableSource<Source>,
468+
config: MutableSourceHookConfig<Source, Snapshot>,
469+
): Snapshot {
470470
resolveCurrentlyRenderingComponent();
471471
const getSnapshot = config.getSnapshot;
472-
return getSnapshot();
472+
return getSnapshot(source._source);
473473
}
474474

475475
function useDeferredValue<T>(value: T, config: TimeoutConfig | null | void): T {

packages/react-reconciler/src/ReactFiberBeginWork.js

+33
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ function forceUnmountCurrentAndReconcile(
277277
}
278278

279279
function updateForwardRef(
280+
root: FiberRoot,
280281
current: Fiber | null,
281282
workInProgress: Fiber,
282283
Component: any,
@@ -316,6 +317,7 @@ function updateForwardRef(
316317
nextChildren = renderWithHooks(
317318
current,
318319
workInProgress,
320+
root,
319321
render,
320322
nextProps,
321323
ref,
@@ -330,6 +332,7 @@ function updateForwardRef(
330332
nextChildren = renderWithHooks(
331333
current,
332334
workInProgress,
335+
root,
333336
render,
334337
nextProps,
335338
ref,
@@ -342,6 +345,7 @@ function updateForwardRef(
342345
nextChildren = renderWithHooks(
343346
current,
344347
workInProgress,
348+
root,
345349
render,
346350
nextProps,
347351
ref,
@@ -370,6 +374,7 @@ function updateForwardRef(
370374
}
371375

372376
function updateMemoComponent(
377+
root: FiberRoot,
373378
current: Fiber | null,
374379
workInProgress: Fiber,
375380
Component: any,
@@ -398,6 +403,7 @@ function updateMemoComponent(
398403
validateFunctionComponentInDev(workInProgress, type);
399404
}
400405
return updateSimpleMemoComponent(
406+
root,
401407
current,
402408
workInProgress,
403409
resolvedType,
@@ -478,6 +484,7 @@ function updateMemoComponent(
478484
}
479485

480486
function updateSimpleMemoComponent(
487+
root: FiberRoot,
481488
current: Fiber | null,
482489
workInProgress: Fiber,
483490
Component: any,
@@ -532,6 +539,7 @@ function updateSimpleMemoComponent(
532539
}
533540
}
534541
return updateFunctionComponent(
542+
root,
535543
current,
536544
workInProgress,
537545
Component,
@@ -601,6 +609,7 @@ function markRef(current: Fiber | null, workInProgress: Fiber) {
601609
}
602610

603611
function updateFunctionComponent(
612+
root,
604613
current,
605614
workInProgress,
606615
Component,
@@ -638,6 +647,7 @@ function updateFunctionComponent(
638647
nextChildren = renderWithHooks(
639648
current,
640649
workInProgress,
650+
root,
641651
Component,
642652
nextProps,
643653
context,
@@ -652,6 +662,7 @@ function updateFunctionComponent(
652662
nextChildren = renderWithHooks(
653663
current,
654664
workInProgress,
665+
root,
655666
Component,
656667
nextProps,
657668
context,
@@ -664,6 +675,7 @@ function updateFunctionComponent(
664675
nextChildren = renderWithHooks(
665676
current,
666677
workInProgress,
678+
root,
667679
Component,
668680
nextProps,
669681
context,
@@ -692,6 +704,7 @@ function updateFunctionComponent(
692704
}
693705

694706
function updateChunk(
707+
root: FiberRoot,
695708
current: Fiber | null,
696709
workInProgress: Fiber,
697710
chunk: any,
@@ -714,6 +727,7 @@ function updateChunk(
714727
nextChildren = renderWithHooks(
715728
current,
716729
workInProgress,
730+
root,
717731
render,
718732
nextProps,
719733
data,
@@ -728,6 +742,7 @@ function updateChunk(
728742
nextChildren = renderWithHooks(
729743
current,
730744
workInProgress,
745+
root,
731746
render,
732747
nextProps,
733748
data,
@@ -740,6 +755,7 @@ function updateChunk(
740755
nextChildren = renderWithHooks(
741756
current,
742757
workInProgress,
758+
root,
743759
render,
744760
nextProps,
745761
data,
@@ -1110,6 +1126,7 @@ function updateHostText(current, workInProgress) {
11101126
}
11111127

11121128
function mountLazyComponent(
1129+
root,
11131130
_current,
11141131
workInProgress,
11151132
elementType,
@@ -1147,6 +1164,7 @@ function mountLazyComponent(
11471164
);
11481165
}
11491166
child = updateFunctionComponent(
1167+
root,
11501168
null,
11511169
workInProgress,
11521170
Component,
@@ -1177,6 +1195,7 @@ function mountLazyComponent(
11771195
);
11781196
}
11791197
child = updateForwardRef(
1198+
root,
11801199
null,
11811200
workInProgress,
11821201
Component,
@@ -1201,6 +1220,7 @@ function mountLazyComponent(
12011220
}
12021221
}
12031222
child = updateMemoComponent(
1223+
root,
12041224
null,
12051225
workInProgress,
12061226
Component,
@@ -1214,6 +1234,7 @@ function mountLazyComponent(
12141234
if (enableChunksAPI) {
12151235
// TODO: Resolve for Hot Reloading.
12161236
child = updateChunk(
1237+
root,
12171238
null,
12181239
workInProgress,
12191240
Component,
@@ -1306,6 +1327,7 @@ function mountIncompleteClassComponent(
13061327
}
13071328

13081329
function mountIndeterminateComponent(
1330+
root,
13091331
_current,
13101332
workInProgress,
13111333
Component,
@@ -1362,6 +1384,7 @@ function mountIndeterminateComponent(
13621384
value = renderWithHooks(
13631385
null,
13641386
workInProgress,
1387+
root,
13651388
Component,
13661389
props,
13671390
context,
@@ -1371,6 +1394,7 @@ function mountIndeterminateComponent(
13711394
value = renderWithHooks(
13721395
null,
13731396
workInProgress,
1397+
root,
13741398
Component,
13751399
props,
13761400
context,
@@ -1467,6 +1491,7 @@ function mountIndeterminateComponent(
14671491
value = renderWithHooks(
14681492
null,
14691493
workInProgress,
1494+
root,
14701495
Component,
14711496
props,
14721497
context,
@@ -2886,6 +2911,7 @@ function remountFiber(
28862911
}
28872912

28882913
function beginWork(
2914+
root: FiberRoot,
28892915
current: Fiber | null,
28902916
workInProgress: Fiber,
28912917
renderExpirationTime: ExpirationTime,
@@ -3109,6 +3135,7 @@ function beginWork(
31093135
switch (workInProgress.tag) {
31103136
case IndeterminateComponent: {
31113137
return mountIndeterminateComponent(
3138+
root,
31123139
current,
31133140
workInProgress,
31143141
workInProgress.type,
@@ -3118,6 +3145,7 @@ function beginWork(
31183145
case LazyComponent: {
31193146
const elementType = workInProgress.elementType;
31203147
return mountLazyComponent(
3148+
root,
31213149
current,
31223150
workInProgress,
31233151
elementType,
@@ -3133,6 +3161,7 @@ function beginWork(
31333161
? unresolvedProps
31343162
: resolveDefaultProps(Component, unresolvedProps);
31353163
return updateFunctionComponent(
3164+
root,
31363165
current,
31373166
workInProgress,
31383167
Component,
@@ -3181,6 +3210,7 @@ function beginWork(
31813210
? unresolvedProps
31823211
: resolveDefaultProps(type, unresolvedProps);
31833212
return updateForwardRef(
3213+
root,
31843214
current,
31853215
workInProgress,
31863216
type,
@@ -3227,6 +3257,7 @@ function beginWork(
32273257
}
32283258
resolvedProps = resolveDefaultProps(type.type, resolvedProps);
32293259
return updateMemoComponent(
3260+
root,
32303261
current,
32313262
workInProgress,
32323263
type,
@@ -3237,6 +3268,7 @@ function beginWork(
32373268
}
32383269
case SimpleMemoComponent: {
32393270
return updateSimpleMemoComponent(
3271+
root,
32403272
current,
32413273
workInProgress,
32423274
workInProgress.type,
@@ -3292,6 +3324,7 @@ function beginWork(
32923324
const chunk = workInProgress.type;
32933325
const props = workInProgress.pendingProps;
32943326
return updateChunk(
3327+
root,
32953328
current,
32963329
workInProgress,
32973330
chunk,

packages/react-reconciler/src/ReactFiberCommitWork.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,9 @@ function commitHookEffectListMount(tag: number, finishedWork: Fiber) {
356356
if ((effect.tag & tag) === tag) {
357357
// Mount
358358
const create = effect.create;
359-
effect.destroy = create();
359+
if (typeof create === 'function') {
360+
effect.destroy = create();
361+
}
360362

361363
if (__DEV__) {
362364
const destroy = effect.destroy;

0 commit comments

Comments
 (0)