Skip to content

Instantly share code, notes, and snippets.

@gaearon
Last active December 3, 2019 01:39
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gaearon/864f451ff2a2a5821fe2c4db344ebcdf to your computer and use it in GitHub Desktop.
Save gaearon/864f451ff2a2a5821fe2c4db344ebcdf to your computer and use it in GitHub Desktop.
// This is from post-processed code by minifier so names are weird
var NoEffect$1 = 0,
UnmountSnapshot = 2,
UnmountMutation = 4,
MountMutation = 8,
UnmountLayout = 16,
MountLayout = 32,
MountPassive = 64,
UnmountPassive = 128,
ReactCurrentDispatcher$1 = ReactSharedInternals.ReactCurrentDispatcher,
renderExpirationTime = 0,
currentlyRenderingFiber$1 = null,
currentHook = null,
nextCurrentHook = null,
firstWorkInProgressHook = null,
workInProgressHook = null,
nextWorkInProgressHook = null,
remainingExpirationTime = 0,
componentUpdateQueue = null,
sideEffectTag = 0,
didScheduleRenderPhaseUpdate = !1,
renderPhaseUpdates = null,
numberOfReRenders = 0;
function throwInvalidHookError() {
reactProdInvariant("307");
}
function areHookInputsEqual(nextDeps, prevDeps) {
if (null === prevDeps) return !1;
for (var i = 0; i < prevDeps.length && i < nextDeps.length; i++)
if (!is(nextDeps[i], prevDeps[i])) return !1;
return !0;
}
function renderWithHooks(
current,
workInProgress,
Component,
props,
refOrContext,
nextRenderExpirationTime
) {
renderExpirationTime = nextRenderExpirationTime;
currentlyRenderingFiber$1 = workInProgress;
nextCurrentHook = null !== current ? current.memoizedState : null;
ReactCurrentDispatcher$1.current =
null === nextCurrentHook ? HooksDispatcherOnMount : HooksDispatcherOnUpdate;
workInProgress = Component(props, refOrContext);
if (didScheduleRenderPhaseUpdate) {
do
(didScheduleRenderPhaseUpdate = !1),
(numberOfReRenders += 1),
(nextCurrentHook = null !== current ? current.memoizedState : null),
(nextWorkInProgressHook = firstWorkInProgressHook),
(componentUpdateQueue = workInProgressHook = currentHook = null),
(ReactCurrentDispatcher$1.current = HooksDispatcherOnUpdate),
(workInProgress = Component(props, refOrContext));
while (didScheduleRenderPhaseUpdate);
renderPhaseUpdates = null;
numberOfReRenders = 0;
}
ReactCurrentDispatcher$1.current = ContextOnlyDispatcher;
current = currentlyRenderingFiber$1;
current.memoizedState = firstWorkInProgressHook;
current.expirationTime = remainingExpirationTime;
current.updateQueue = componentUpdateQueue;
current.effectTag |= sideEffectTag;
current = null !== currentHook && null !== currentHook.next;
renderExpirationTime = 0;
nextWorkInProgressHook = workInProgressHook = firstWorkInProgressHook = nextCurrentHook = currentHook = currentlyRenderingFiber$1 = null;
remainingExpirationTime = 0;
componentUpdateQueue = null;
sideEffectTag = 0;
current ? reactProdInvariant("300") : void 0;
return workInProgress;
}
function resetHooks() {
ReactCurrentDispatcher$1.current = ContextOnlyDispatcher;
renderExpirationTime = 0;
nextWorkInProgressHook = workInProgressHook = firstWorkInProgressHook = nextCurrentHook = currentHook = currentlyRenderingFiber$1 = null;
remainingExpirationTime = 0;
componentUpdateQueue = null;
sideEffectTag = 0;
didScheduleRenderPhaseUpdate = !1;
renderPhaseUpdates = null;
numberOfReRenders = 0;
}
function mountWorkInProgressHook() {
var hook = {
memoizedState: null,
baseState: null,
queue: null,
baseUpdate: null,
next: null
};
null === workInProgressHook
? (firstWorkInProgressHook = workInProgressHook = hook)
: (workInProgressHook = workInProgressHook.next = hook);
return workInProgressHook;
}
function updateWorkInProgressHook() {
if (null !== nextWorkInProgressHook)
(workInProgressHook = nextWorkInProgressHook),
(nextWorkInProgressHook = workInProgressHook.next),
(currentHook = nextCurrentHook),
(nextCurrentHook = null !== currentHook ? currentHook.next : null);
else {
null === nextCurrentHook ? reactProdInvariant("310") : void 0;
currentHook = nextCurrentHook;
var newHook = {
memoizedState: currentHook.memoizedState,
baseState: currentHook.baseState,
queue: currentHook.queue,
baseUpdate: currentHook.baseUpdate,
next: null
};
workInProgressHook =
null === workInProgressHook
? (firstWorkInProgressHook = newHook)
: (workInProgressHook.next = newHook);
nextCurrentHook = currentHook.next;
}
return workInProgressHook;
}
function basicStateReducer(state, action) {
return "function" === typeof action ? action(state) : action;
}
function updateReducer(reducer) {
var hook = updateWorkInProgressHook(),
queue = hook.queue;
null === queue ? reactProdInvariant("311") : void 0;
if (0 < numberOfReRenders) {
var _dispatch = queue.dispatch;
if (null !== renderPhaseUpdates) {
var firstRenderPhaseUpdate = renderPhaseUpdates.get(queue);
if (void 0 !== firstRenderPhaseUpdate) {
renderPhaseUpdates.delete(queue);
var newState = hook.memoizedState;
do
(newState = reducer(newState, firstRenderPhaseUpdate.action)),
(firstRenderPhaseUpdate = firstRenderPhaseUpdate.next);
while (null !== firstRenderPhaseUpdate);
is(newState, hook.memoizedState) || (didReceiveUpdate = !0);
hook.memoizedState = newState;
hook.baseUpdate === queue.last && (hook.baseState = newState);
queue.eagerReducer = reducer;
queue.eagerState = newState;
return [newState, _dispatch];
}
}
return [hook.memoizedState, _dispatch];
}
_dispatch = queue.last;
var baseUpdate = hook.baseUpdate;
newState = hook.baseState;
null !== baseUpdate
? (null !== _dispatch && (_dispatch.next = null),
(_dispatch = baseUpdate.next))
: (_dispatch = null !== _dispatch ? _dispatch.next : null);
if (null !== _dispatch) {
var newBaseUpdate = (firstRenderPhaseUpdate = null),
_update = _dispatch,
didSkip = !1;
do {
var updateExpirationTime = _update.expirationTime;
updateExpirationTime < renderExpirationTime
? (didSkip ||
((didSkip = !0),
(newBaseUpdate = baseUpdate),
(firstRenderPhaseUpdate = newState)),
updateExpirationTime > remainingExpirationTime &&
(remainingExpirationTime = updateExpirationTime))
: (newState =
_update.eagerReducer === reducer
? _update.eagerState
: reducer(newState, _update.action));
baseUpdate = _update;
_update = _update.next;
} while (null !== _update && _update !== _dispatch);
didSkip ||
((newBaseUpdate = baseUpdate), (firstRenderPhaseUpdate = newState));
is(newState, hook.memoizedState) || (didReceiveUpdate = !0);
hook.memoizedState = newState;
hook.baseUpdate = newBaseUpdate;
hook.baseState = firstRenderPhaseUpdate;
queue.eagerReducer = reducer;
queue.eagerState = newState;
}
return [hook.memoizedState, queue.dispatch];
}
function pushEffect(tag, create, destroy, deps) {
tag = { tag: tag, create: create, destroy: destroy, deps: deps, next: null };
null === componentUpdateQueue
? ((componentUpdateQueue = { lastEffect: null }),
(componentUpdateQueue.lastEffect = tag.next = tag))
: ((create = componentUpdateQueue.lastEffect),
null === create
? (componentUpdateQueue.lastEffect = tag.next = tag)
: ((destroy = create.next),
(create.next = tag),
(tag.next = destroy),
(componentUpdateQueue.lastEffect = tag)));
return tag;
}
function mountEffectImpl(fiberEffectTag, hookEffectTag, create, deps) {
var hook = mountWorkInProgressHook();
sideEffectTag |= fiberEffectTag;
hook.memoizedState = pushEffect(
hookEffectTag,
create,
void 0,
void 0 === deps ? null : deps
);
}
function updateEffectImpl(fiberEffectTag, hookEffectTag, create, deps) {
var hook = updateWorkInProgressHook();
deps = void 0 === deps ? null : deps;
var destroy = void 0;
if (null !== currentHook) {
var prevEffect = currentHook.memoizedState;
destroy = prevEffect.destroy;
if (null !== deps && areHookInputsEqual(deps, prevEffect.deps)) {
pushEffect(NoEffect$1, create, destroy, deps);
return;
}
}
sideEffectTag |= fiberEffectTag;
hook.memoizedState = pushEffect(hookEffectTag, create, destroy, deps);
}
function imperativeHandleEffect(create, ref) {
if ("function" === typeof ref)
return (
(create = create()),
ref(create),
function() {
ref(null);
}
);
if (null !== ref && void 0 !== ref)
return (
(create = create()),
(ref.current = create),
function() {
ref.current = null;
}
);
}
function mountDebugValue() {}
function dispatchAction(fiber, queue, action) {
25 > numberOfReRenders ? void 0 : reactProdInvariant("301");
var alternate = fiber.alternate;
if (
fiber === currentlyRenderingFiber$1 ||
(null !== alternate && alternate === currentlyRenderingFiber$1)
)
if (
((didScheduleRenderPhaseUpdate = !0),
(fiber = {
expirationTime: renderExpirationTime,
action: action,
eagerReducer: null,
eagerState: null,
next: null
}),
null === renderPhaseUpdates && (renderPhaseUpdates = new Map()),
(action = renderPhaseUpdates.get(queue)),
void 0 === action)
)
renderPhaseUpdates.set(queue, fiber);
else {
for (queue = action; null !== queue.next; ) queue = queue.next;
queue.next = fiber;
}
else {
flushPassiveEffects();
var currentTime = requestCurrentTime();
currentTime = computeExpirationForFiber(currentTime, fiber);
var _update2 = {
expirationTime: currentTime,
action: action,
eagerReducer: null,
eagerState: null,
next: null
},
_last = queue.last;
if (null === _last) _update2.next = _update2;
else {
var first = _last.next;
null !== first && (_update2.next = first);
_last.next = _update2;
}
queue.last = _update2;
if (
0 === fiber.expirationTime &&
(null === alternate || 0 === alternate.expirationTime) &&
((alternate = queue.eagerReducer), null !== alternate)
)
try {
var currentState = queue.eagerState,
_eagerState = alternate(currentState, action);
_update2.eagerReducer = alternate;
_update2.eagerState = _eagerState;
if (is(_eagerState, currentState)) return;
} catch (error) {
} finally {
}
scheduleWork(fiber, currentTime);
}
}
var ContextOnlyDispatcher = {
readContext: readContext,
useCallback: throwInvalidHookError,
useContext: throwInvalidHookError,
useEffect: throwInvalidHookError,
useImperativeHandle: throwInvalidHookError,
useLayoutEffect: throwInvalidHookError,
useMemo: throwInvalidHookError,
useReducer: throwInvalidHookError,
useRef: throwInvalidHookError,
useState: throwInvalidHookError,
useDebugValue: throwInvalidHookError
},
HooksDispatcherOnMount = {
readContext: readContext,
useCallback: function(callback, deps) {
mountWorkInProgressHook().memoizedState = [
callback,
void 0 === deps ? null : deps
];
return callback;
},
useContext: readContext,
useEffect: function(create, deps) {
return mountEffectImpl(516, UnmountPassive | MountPassive, create, deps);
},
useImperativeHandle: function(ref, create, deps) {
deps = null !== deps && void 0 !== deps ? deps.concat([ref]) : null;
return mountEffectImpl(
4,
UnmountMutation | MountLayout,
imperativeHandleEffect.bind(null, create, ref),
deps
);
},
useLayoutEffect: function(create, deps) {
return mountEffectImpl(4, UnmountMutation | MountLayout, create, deps);
},
useMemo: function(nextCreate, deps) {
var hook = mountWorkInProgressHook();
deps = void 0 === deps ? null : deps;
nextCreate = nextCreate();
hook.memoizedState = [nextCreate, deps];
return nextCreate;
},
useReducer: function(reducer, initialArg, init) {
var hook = mountWorkInProgressHook();
initialArg = void 0 !== init ? init(initialArg) : initialArg;
hook.memoizedState = hook.baseState = initialArg;
reducer = hook.queue = {
last: null,
dispatch: null,
eagerReducer: reducer,
eagerState: initialArg
};
reducer = reducer.dispatch = dispatchAction.bind(
null,
currentlyRenderingFiber$1,
reducer
);
return [hook.memoizedState, reducer];
},
useRef: function(initialValue) {
var hook = mountWorkInProgressHook();
initialValue = { current: initialValue };
return (hook.memoizedState = initialValue);
},
useState: function(initialState) {
var hook = mountWorkInProgressHook();
"function" === typeof initialState && (initialState = initialState());
hook.memoizedState = hook.baseState = initialState;
initialState = hook.queue = {
last: null,
dispatch: null,
eagerReducer: basicStateReducer,
eagerState: initialState
};
initialState = initialState.dispatch = dispatchAction.bind(
null,
currentlyRenderingFiber$1,
initialState
);
return [hook.memoizedState, initialState];
},
useDebugValue: mountDebugValue
},
HooksDispatcherOnUpdate = {
readContext: readContext,
useCallback: function(callback, deps) {
var hook = updateWorkInProgressHook();
deps = void 0 === deps ? null : deps;
var prevState = hook.memoizedState;
if (
null !== prevState &&
null !== deps &&
areHookInputsEqual(deps, prevState[1])
)
return prevState[0];
hook.memoizedState = [callback, deps];
return callback;
},
useContext: readContext,
useEffect: function(create, deps) {
return updateEffectImpl(516, UnmountPassive | MountPassive, create, deps);
},
useImperativeHandle: function(ref, create, deps) {
deps = null !== deps && void 0 !== deps ? deps.concat([ref]) : null;
return updateEffectImpl(
4,
UnmountMutation | MountLayout,
imperativeHandleEffect.bind(null, create, ref),
deps
);
},
useLayoutEffect: function(create, deps) {
return updateEffectImpl(4, UnmountMutation | MountLayout, create, deps);
},
useMemo: function(nextCreate, deps) {
var hook = updateWorkInProgressHook();
deps = void 0 === deps ? null : deps;
var prevState = hook.memoizedState;
if (
null !== prevState &&
null !== deps &&
areHookInputsEqual(deps, prevState[1])
)
return prevState[0];
nextCreate = nextCreate();
hook.memoizedState = [nextCreate, deps];
return nextCreate;
},
useReducer: updateReducer,
useRef: function() {
return updateWorkInProgressHook().memoizedState;
},
useState: function(initialState) {
return updateReducer(basicStateReducer, initialState);
},
useDebugValue: mountDebugValue
},
}
@williamhub
Copy link

Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment