Skip to content

Commit

Permalink
Refactored hook to reflect changes in RFC
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed Feb 13, 2020
1 parent 3a2a112 commit ccef29a
Show file tree
Hide file tree
Showing 12 changed files with 545 additions and 265 deletions.
19 changes: 12 additions & 7 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ function getPrimitiveStackCache(): Map<string, Array<any>> {
Dispatcher.useCallback(() => {});
Dispatcher.useMemo(() => null);
Dispatcher.useMutableSource(
{},
{
getVersion: () => null,
_source: {},
_getVersion: () => 1,
_workInProgressVersionPrimary: null,
_workInProgressVersionSecondary: null,
},
{
getSnapshot: () => null,
subscribe: () => () => {},
},
Expand Down Expand Up @@ -236,13 +240,14 @@ function useMemo<T>(
return value;
}

function useMutableSource<S>(
source: MutableSource,
config: MutableSourceHookConfig<S>,
): S {
export function useMutableSource<Source, Snapshot>(
source: MutableSource<Source>,
config: MutableSourceHookConfig<Source, Snapshot>,
): Snapshot {
const hook = nextHook();
const getSnapshot = config.getSnapshot;
const value = hook !== null ? hook.memoizedState.snapshot : getSnapshot();
const value =
hook !== null ? hook.memoizedState.snapshot : getSnapshot(source._source);
hookLog.push({primitive: 'MutableSource', stackError: new Error(), value});
return value;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/react-dom/src/server/ReactPartialRendererHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,13 +463,13 @@ function useResponder(responder, props): ReactEventResponderListener<any, any> {
};
}

function useMutableSource<S>(
source: MutableSource,
config: MutableSourceHookConfig<S>,
): S {
function useMutableSource<Source, Snapshot>(
source: MutableSource<Source>,
config: MutableSourceHookConfig<Source, Snapshot>,
): Snapshot {
resolveCurrentlyRenderingComponent();
const getSnapshot = config.getSnapshot;
return getSnapshot();
return getSnapshot(source._source);
}

function useDeferredValue<T>(value: T, config: TimeoutConfig | null | void): T {
Expand Down
33 changes: 33 additions & 0 deletions packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ function forceUnmountCurrentAndReconcile(
}

function updateForwardRef(
root: FiberRoot,
current: Fiber | null,
workInProgress: Fiber,
Component: any,
Expand Down Expand Up @@ -316,6 +317,7 @@ function updateForwardRef(
nextChildren = renderWithHooks(
current,
workInProgress,
root,
render,
nextProps,
ref,
Expand All @@ -330,6 +332,7 @@ function updateForwardRef(
nextChildren = renderWithHooks(
current,
workInProgress,
root,
render,
nextProps,
ref,
Expand All @@ -342,6 +345,7 @@ function updateForwardRef(
nextChildren = renderWithHooks(
current,
workInProgress,
root,
render,
nextProps,
ref,
Expand Down Expand Up @@ -370,6 +374,7 @@ function updateForwardRef(
}

function updateMemoComponent(
root: FiberRoot,
current: Fiber | null,
workInProgress: Fiber,
Component: any,
Expand Down Expand Up @@ -398,6 +403,7 @@ function updateMemoComponent(
validateFunctionComponentInDev(workInProgress, type);
}
return updateSimpleMemoComponent(
root,
current,
workInProgress,
resolvedType,
Expand Down Expand Up @@ -478,6 +484,7 @@ function updateMemoComponent(
}

function updateSimpleMemoComponent(
root: FiberRoot,
current: Fiber | null,
workInProgress: Fiber,
Component: any,
Expand Down Expand Up @@ -532,6 +539,7 @@ function updateSimpleMemoComponent(
}
}
return updateFunctionComponent(
root,
current,
workInProgress,
Component,
Expand Down Expand Up @@ -601,6 +609,7 @@ function markRef(current: Fiber | null, workInProgress: Fiber) {
}

function updateFunctionComponent(
root,
current,
workInProgress,
Component,
Expand Down Expand Up @@ -638,6 +647,7 @@ function updateFunctionComponent(
nextChildren = renderWithHooks(
current,
workInProgress,
root,
Component,
nextProps,
context,
Expand All @@ -652,6 +662,7 @@ function updateFunctionComponent(
nextChildren = renderWithHooks(
current,
workInProgress,
root,
Component,
nextProps,
context,
Expand All @@ -664,6 +675,7 @@ function updateFunctionComponent(
nextChildren = renderWithHooks(
current,
workInProgress,
root,
Component,
nextProps,
context,
Expand Down Expand Up @@ -692,6 +704,7 @@ function updateFunctionComponent(
}

function updateChunk(
root: FiberRoot,
current: Fiber | null,
workInProgress: Fiber,
chunk: any,
Expand All @@ -714,6 +727,7 @@ function updateChunk(
nextChildren = renderWithHooks(
current,
workInProgress,
root,
render,
nextProps,
data,
Expand All @@ -728,6 +742,7 @@ function updateChunk(
nextChildren = renderWithHooks(
current,
workInProgress,
root,
render,
nextProps,
data,
Expand All @@ -740,6 +755,7 @@ function updateChunk(
nextChildren = renderWithHooks(
current,
workInProgress,
root,
render,
nextProps,
data,
Expand Down Expand Up @@ -1110,6 +1126,7 @@ function updateHostText(current, workInProgress) {
}

function mountLazyComponent(
root,
_current,
workInProgress,
elementType,
Expand Down Expand Up @@ -1147,6 +1164,7 @@ function mountLazyComponent(
);
}
child = updateFunctionComponent(
root,
null,
workInProgress,
Component,
Expand Down Expand Up @@ -1177,6 +1195,7 @@ function mountLazyComponent(
);
}
child = updateForwardRef(
root,
null,
workInProgress,
Component,
Expand All @@ -1201,6 +1220,7 @@ function mountLazyComponent(
}
}
child = updateMemoComponent(
root,
null,
workInProgress,
Component,
Expand All @@ -1214,6 +1234,7 @@ function mountLazyComponent(
if (enableChunksAPI) {
// TODO: Resolve for Hot Reloading.
child = updateChunk(
root,
null,
workInProgress,
Component,
Expand Down Expand Up @@ -1306,6 +1327,7 @@ function mountIncompleteClassComponent(
}

function mountIndeterminateComponent(
root,
_current,
workInProgress,
Component,
Expand Down Expand Up @@ -1362,6 +1384,7 @@ function mountIndeterminateComponent(
value = renderWithHooks(
null,
workInProgress,
root,
Component,
props,
context,
Expand All @@ -1371,6 +1394,7 @@ function mountIndeterminateComponent(
value = renderWithHooks(
null,
workInProgress,
root,
Component,
props,
context,
Expand Down Expand Up @@ -1467,6 +1491,7 @@ function mountIndeterminateComponent(
value = renderWithHooks(
null,
workInProgress,
root,
Component,
props,
context,
Expand Down Expand Up @@ -2886,6 +2911,7 @@ function remountFiber(
}

function beginWork(
root: FiberRoot,
current: Fiber | null,
workInProgress: Fiber,
renderExpirationTime: ExpirationTime,
Expand Down Expand Up @@ -3109,6 +3135,7 @@ function beginWork(
switch (workInProgress.tag) {
case IndeterminateComponent: {
return mountIndeterminateComponent(
root,
current,
workInProgress,
workInProgress.type,
Expand All @@ -3118,6 +3145,7 @@ function beginWork(
case LazyComponent: {
const elementType = workInProgress.elementType;
return mountLazyComponent(
root,
current,
workInProgress,
elementType,
Expand All @@ -3133,6 +3161,7 @@ function beginWork(
? unresolvedProps
: resolveDefaultProps(Component, unresolvedProps);
return updateFunctionComponent(
root,
current,
workInProgress,
Component,
Expand Down Expand Up @@ -3181,6 +3210,7 @@ function beginWork(
? unresolvedProps
: resolveDefaultProps(type, unresolvedProps);
return updateForwardRef(
root,
current,
workInProgress,
type,
Expand Down Expand Up @@ -3227,6 +3257,7 @@ function beginWork(
}
resolvedProps = resolveDefaultProps(type.type, resolvedProps);
return updateMemoComponent(
root,
current,
workInProgress,
type,
Expand All @@ -3237,6 +3268,7 @@ function beginWork(
}
case SimpleMemoComponent: {
return updateSimpleMemoComponent(
root,
current,
workInProgress,
workInProgress.type,
Expand Down Expand Up @@ -3292,6 +3324,7 @@ function beginWork(
const chunk = workInProgress.type;
const props = workInProgress.pendingProps;
return updateChunk(
root,
current,
workInProgress,
chunk,
Expand Down
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@ function commitHookEffectListMount(tag: number, finishedWork: Fiber) {
if ((effect.tag & tag) === tag) {
// Mount
const create = effect.create;
effect.destroy = create();
if (typeof create === 'function') {
effect.destroy = create();
}

if (__DEV__) {
const destroy = effect.destroy;
Expand Down
Loading

0 comments on commit ccef29a

Please sign in to comment.