Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions src/components/KeepAlive/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,16 @@ const createKeepAliveManager = () => {
// set global options: { deactivateDelay, keepInactiveCount, limit }
setOptions: (opts = {}) => {
if (typeof opts.deactivateDelay === 'number') deactivateDelay = opts.deactivateDelay
if (typeof opts.keepInactiveCount === 'number')
if (typeof opts.keepInactiveCount === 'number') {
keepInactiveCount = Math.max(0, Math.floor(opts.keepInactiveCount))
}
if (typeof opts.limit === 'number') limit = Math.max(0, Math.floor(opts.limit))
},
register: (id, opts) => {
// opts: { setShouldRender, persistOnUnmount }
instances.set(id, {
setShouldRender: opts.setShouldRender,
persistOnUnmount: !!opts.persistOnUnmount,
persistOnUnmount: !!opts.persistOnUnmount
})
},
unregister: (id) => {
Expand Down Expand Up @@ -193,7 +194,7 @@ const createKeepAliveManager = () => {
instances.delete(id)
activeMap.delete(id)
keys = keys.filter((k) => k !== id)
},
}
}
}

Expand Down Expand Up @@ -236,7 +237,7 @@ const KeepAlive = ({ id, active = false, children, persistOnUnmount = false, cac
const effectivePersist = ActivityComponent ? true : persistOnUnmount
keepAliveManager.register(id, {
setShouldRender,
persistOnUnmount: effectivePersist,
persistOnUnmount: effectivePersist
})
}
return () => {
Expand Down Expand Up @@ -314,7 +315,6 @@ const KeepAlive = ({ id, active = false, children, persistOnUnmount = false, cac
setContainerNode(containerRef.current)
if (process.env.NODE_ENV === 'development') {
try {

console.debug('[KeepAlive] setContainerNode', id, containerRef.current)
} catch (e) {}
}
Expand All @@ -330,14 +330,14 @@ const KeepAlive = ({ id, active = false, children, persistOnUnmount = false, cac
if (!active) return
scrollPos.current.set(e.target, {
left: e.target.scrollLeft,
top: e.target.scrollTop,
top: e.target.scrollTop
})
}

// Capture scroll events to record positions
target.addEventListener('scroll', onScroll, {
capture: true,
passive: true,
passive: true
})

return () => {
Expand Down Expand Up @@ -391,7 +391,7 @@ const KeepAlive = ({ id, active = false, children, persistOnUnmount = false, cac
// 在移动 DOM 之前,发送自定义事件通知子组件,然后同步移动到占位符下。
// 之前使用短延迟的异步移动会导致渲染滞后和大量定时器/帧回调,移到同步移动以提升响应性。
const event = new CustomEvent('keepalive-dom-move', {
detail: { from: container.parentNode, to: placeholder },
detail: { from: container.parentNode, to: placeholder }
})

let dispatchError = null
Expand All @@ -403,11 +403,10 @@ const KeepAlive = ({ id, active = false, children, persistOnUnmount = false, cac

if (process.env.NODE_ENV === 'development') {
try {

console.debug('[KeepAlive] appending container to placeholder', id, {
from: container.parentNode,
to: placeholder,
dispatchError,
dispatchError
})
} catch (e) {}
}
Expand All @@ -430,7 +429,7 @@ const KeepAlive = ({ id, active = false, children, persistOnUnmount = false, cac
id,
note: 'no-children-after-append',
childElementCount: container.childElementCount,
time: Date.now(),
time: Date.now()
})
try {
document.body.dataset.keepaliveIssue = 'no-children'
Expand All @@ -456,7 +455,7 @@ const KeepAlive = ({ id, active = false, children, persistOnUnmount = false, cac
childElementCount: container.childElementCount,
appended,
dispatchError,
time: Date.now(),
time: Date.now()
})
}
} catch (e) {}
Expand Down Expand Up @@ -494,7 +493,7 @@ const KeepAlive = ({ id, active = false, children, persistOnUnmount = false, cac
note: 'cleared-inline-display-none',
clearedCount: cleared.length,
clearedTagsSample: cleared.slice(0, 5),
time: Date.now(),
time: Date.now()
})
}
} catch (e) {}
Expand Down Expand Up @@ -525,7 +524,7 @@ const KeepAlive = ({ id, active = false, children, persistOnUnmount = false, cac
window.__keepalive_debug_details.push({
id,
note: 'force-visible-root',
time: Date.now(),
time: Date.now()
})
}
}
Expand Down Expand Up @@ -557,7 +556,7 @@ const KeepAlive = ({ id, active = false, children, persistOnUnmount = false, cac
shouldRender,
containerNode: !!containerNode,
childrenType: typeof children,
time: Date.now(),
time: Date.now()
})
} catch (e) {}
}, [shouldRender, active, containerNode, id, children])
Expand Down Expand Up @@ -615,7 +614,7 @@ KeepAlive.propTypes = {
active: PropTypes.bool,
children: PropTypes.node,
persistOnUnmount: PropTypes.bool,
cacheLimit: PropTypes.number,
cacheLimit: PropTypes.number
}

export default KeepAlive
Loading