Skip to content

Commit

Permalink
bump: version 0.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
shenjunru committed Jun 14, 2022
1 parent b1bab50 commit de5142e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 27 deletions.
5 changes: 5 additions & 0 deletions lib/cjs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ exports.FiberFlag = Object.freeze({
NoFlags: 0b00000000000000000000000000,
Placement: 0b00000000000000000000000010,
Update: 0b00000000000000000000000100,
Ref: v18 ? 0b00000000000000001000000000
: 0b00000000000000000010000000,
Passive: v18 ? 0b00000000000000100000000000
: 0b00000000000000001000000000,
LifecycleEffectMask: v18 ? 0b00000000000111111000000000
Expand Down Expand Up @@ -298,6 +300,9 @@ const isNoEffectHook = (effect) => {
};
const applyFiberEffect = (fiber) => {
let flags = exports.FiberFlag.NoFlags;
if (null != fiber.ref) {
flags |= exports.FiberFlag.Ref;
}
const isFunctionComponent = traverseEffectHooks(fiber, (effect) => {
if (isNoEffectHook(effect)) {
return;
Expand Down
27 changes: 17 additions & 10 deletions lib/esm/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export const FiberFlag = Object.freeze({
NoFlags: 0b00000000000000000000000000,
Placement: 0b00000000000000000000000010,
Update: 0b00000000000000000000000100,
Ref: v18 ? 0b00000000000000001000000000
: 0b00000000000000000010000000,
Passive: v18 ? 0b00000000000000100000000000
: 0b00000000000000001000000000,
LifecycleEffectMask: v18 ? 0b00000000000111111000000000
Expand All @@ -97,12 +99,13 @@ export const getElementFiber = (element, prefix = DomPropPrefix) => {
return internalKey ? element[internalKey] : undefined;
};
export const getRootFiber = (container) => {
var _a;
const fiber = v18 && container && getElementFiber(container, '__reactContainer$');
const root = fiber ? fiber.stateNode : container?._reactRootContainer?._internalRoot;
return root?.current;
const root = fiber ? fiber.stateNode : (_a = container === null || container === void 0 ? void 0 : container._reactRootContainer) === null || _a === void 0 ? void 0 : _a._internalRoot;
return root === null || root === void 0 ? void 0 : root.current;
};
const getHostNode = (fiber) => {
switch (fiber?.tag) {
switch (fiber === null || fiber === void 0 ? void 0 : fiber.tag) {
case FiberTag.HostComponent:
case FiberTag.HostText:
return fiber.stateNode;
Expand Down Expand Up @@ -178,7 +181,7 @@ export const findFiber = (fiber, predicate, flags = FiberVisit.Child) => {
}
return null;
};
export const findParentFiber = (fiber, Component) => findFiber(fiber?.return, (node) => {
export const findParentFiber = (fiber, Component) => findFiber(fiber === null || fiber === void 0 ? void 0 : fiber.return, (node) => {
return node.elementType === Component;
}, FiberVisit.Return);
export const traverseFiber = (fiber, visit, flags = FiberVisit.Child) => {
Expand Down Expand Up @@ -257,10 +260,10 @@ const traverseEffectHooks = (fiber, visit) => {
return false;
}
const updateQueue = fiber.updateQueue;
const lastEffect = updateQueue?.lastEffect;
let nextEffect = updateQueue?.lastEffect;
const lastEffect = updateQueue === null || updateQueue === void 0 ? void 0 : updateQueue.lastEffect;
let nextEffect = updateQueue === null || updateQueue === void 0 ? void 0 : updateQueue.lastEffect;
while (nextEffect) {
if (null != nextEffect?.tag) {
if (null != (nextEffect === null || nextEffect === void 0 ? void 0 : nextEffect.tag)) {
let match = HookEffectTag.NoFlags;
match || (match = nextEffect.tag & HookEffectTag.Layout);
match || (match = nextEffect.tag & HookEffectTag.Passive);
Expand All @@ -287,6 +290,9 @@ const isNoEffectHook = (effect) => {
};
const applyFiberEffect = (fiber) => {
let flags = FiberFlag.NoFlags;
if (null != fiber.ref) {
flags |= FiberFlag.Ref;
}
const isFunctionComponent = traverseEffectHooks(fiber, (effect) => {
if (isNoEffectHook(effect)) {
return;
Expand Down Expand Up @@ -314,12 +320,13 @@ const applyFiberEffect = (fiber) => {
const instance = fiber.stateNode;
const noUpdate = FiberFlag.NoFlags === (fiber.flags & FiberFlag.Update);
const needRender = noUpdate && (true === instance.constructor[EffectProp]);
if (needRender || isFunction(instance?.componentDidMount)) {
if (needRender || isFunction(instance === null || instance === void 0 ? void 0 : instance.componentDidMount)) {
flags |= FiberFlag.Update;
const propKey = 'componentDidUpdate';
const propVal = instance[propKey];
const ownProp = hasOwnProperty.call(instance, propKey);
instance[propKey] = function simulateComponentDidMount() {
var _a;
if (ownProp) {
instance[propKey] = propVal;
}
Expand All @@ -329,7 +336,7 @@ const applyFiberEffect = (fiber) => {
if (needRender) {
this.render();
}
this.componentDidMount?.();
(_a = this.componentDidMount) === null || _a === void 0 ? void 0 : _a.call(this);
};
break;
}
Expand Down Expand Up @@ -440,7 +447,7 @@ export const protectFiber = (fiber) => {
const needDetach = isNodeFiber(node) && true === stack[0];
if (needDetach) {
const hostNode = getHostNode(node);
const parentNode = hostNode?.parentNode;
const parentNode = hostNode === null || hostNode === void 0 ? void 0 : hostNode.parentNode;
protectFiberProp(node, 'stateNode', restore);
if (hostNode && parentNode) {
const dummy = document.createElement('dummy');
Expand Down
34 changes: 18 additions & 16 deletions lib/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const KeepAliveProvider = (props) => {
delete context[MapperPropKey];
}
}, [context]);
return (React.createElement(KeepAliveContext.Provider, { ...props }));
return (React.createElement(KeepAliveContext.Provider, Object.assign({}, props)));
};
class KeepAliveCursor extends React.Component {
render() {
Expand All @@ -35,9 +35,9 @@ const KeepAliveEffect = (props) => {
}
const rootFiber = getRootFiber(context);
const cursorFiber = findFiber(rootFiber, (fiber) => fiber.stateNode === cursor.current);
const effectFiber = cursorFiber?.sibling;
const renderFiber = effectFiber?.sibling;
const finishFiber = renderFiber?.sibling;
const effectFiber = cursorFiber === null || cursorFiber === void 0 ? void 0 : cursorFiber.sibling;
const renderFiber = effectFiber === null || effectFiber === void 0 ? void 0 : effectFiber.sibling;
const finishFiber = renderFiber === null || renderFiber === void 0 ? void 0 : renderFiber.sibling;
if (rootFiber && effectFiber && renderFiber && finishFiber) {
appendFiberEffect(rootFiber, effectFiber, renderFiber, finishFiber);
}
Expand All @@ -56,14 +56,14 @@ const KeepAliveManage = (props) => {
const context = React.useContext(KeepAliveContext);
const cursor = React.useRef(null);
const caches = React.useMemo(() => {
const value = context?.[CachesPropKey] || new Map();
const value = (context === null || context === void 0 ? void 0 : context[CachesPropKey]) || new Map();
if (context) {
context[CachesPropKey] = value;
}
return value;
}, []);
const mapper = React.useMemo(() => {
const value = context?.[MapperPropKey] || new Map();
const value = (context === null || context === void 0 ? void 0 : context[MapperPropKey]) || new Map();
if (context) {
context[MapperPropKey] = value;
}
Expand All @@ -86,12 +86,13 @@ const KeepAliveManage = (props) => {
const ignore = React.useRef(true === props.ignore);
ignore.current = true === props.ignore;
useIsomorphicLayoutEffect(() => () => {
var _a;
if (!context || !readKey || ignore.current || bypass.current) {
return;
}
const rootFiber = getRootFiber(context);
const cursorFiber = findFiber(rootFiber, (fiber) => fiber.stateNode === cursor.current);
const renderFiber = cursorFiber?.sibling?.sibling;
const renderFiber = (_a = cursorFiber === null || cursorFiber === void 0 ? void 0 : cursorFiber.sibling) === null || _a === void 0 ? void 0 : _a.sibling;
if (!renderFiber) {
return;
}
Expand All @@ -103,6 +104,7 @@ const KeepAliveManage = (props) => {
caches.set(readKey, [renderFiber, restore]);
}, []);
React.useEffect(() => {
var _a;
if (step !== 1) {
return;
}
Expand All @@ -111,7 +113,7 @@ const KeepAliveManage = (props) => {
}
const rootFiber = getRootFiber(context);
const cursorFiber = findFiber(rootFiber, (fiber) => fiber.stateNode === cursor.current);
const renderFiber = cursorFiber?.sibling?.sibling;
const renderFiber = (_a = cursorFiber === null || cursorFiber === void 0 ? void 0 : cursorFiber.sibling) === null || _a === void 0 ? void 0 : _a.sibling;
if (!renderFiber || !renderFiber) {
return setState([1, cache]);
}
Expand Down Expand Up @@ -142,22 +144,22 @@ export function keepAlive(Component, getProps) {
return (props) => {
const value = getProps(props);
const alive = typeof value === 'string' ? { name: value } : value;
return (React.createElement(KeepAlive, { ...alive },
React.createElement(Component, { ...props })));
return (React.createElement(KeepAlive, Object.assign({}, alive),
React.createElement(Component, Object.assign({}, props))));
};
}
export const useIgnoreKeepAlive = () => {
const context = React.useContext(KeepAliveContext);
return React.useCallback((name) => {
const caches = context?.[CachesPropKey];
const mapper = context?.[MapperPropKey];
const readKey = mapper?.get(name);
readKey && caches?.delete(readKey);
readKey && mapper?.forEach((value, key) => {
const caches = context === null || context === void 0 ? void 0 : context[CachesPropKey];
const mapper = context === null || context === void 0 ? void 0 : context[MapperPropKey];
const readKey = mapper === null || mapper === void 0 ? void 0 : mapper.get(name);
readKey && (caches === null || caches === void 0 ? void 0 : caches.delete(readKey));
readKey && (mapper === null || mapper === void 0 ? void 0 : mapper.forEach((value, key) => {
if (value === readKey) {
mapper.delete(key);
}
});
}));
}, [context]);
};
export { markClassComponentHasSideEffectRender, markEffectHookIsOnetime, };
Expand Down
1 change: 1 addition & 0 deletions lib/types/helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export declare const FiberFlag: Readonly<{
NoFlags: number;
Placement: number;
Update: number;
Ref: number;
Passive: number;
LifecycleEffectMask: number;
PassiveMask: number;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-fiber-keep-alive",
"version": "0.6.1",
"version": "0.6.2",
"description": "Keep-Alive for React DOM",
"main": "lib/cjs/index.js",
"module": "lib/esm/index.js",
Expand Down

0 comments on commit de5142e

Please sign in to comment.