Skip to content

Commit

Permalink
尝试解除 KeepAlive children update 时亦触发 AliveScope 更新的性能损耗问题,只更新其对应 Keepe…
Browse files Browse the repository at this point in the history
…r;KeepAlive 瞬时更新锁检测条件放宽,减轻对正常更新行为的误判情况
  • Loading branch information
CJY0208 committed Nov 24, 2019
1 parent 4301049 commit 45ed09e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-activation",
"version": "0.3.2",
"version": "0.3.3",
"description": "<KeepAlive /> for React like <keep-alive /> in vue",
"main": "index.js",
"scripts": {
Expand Down
31 changes: 21 additions & 10 deletions src/core/AliveScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ import Keeper from './Keeper'
export default class AliveScope extends Component {
store = new Map()
nodes = new Map()
keepers = new Map()

// FIXME: 每次 update 均触发 forceUpdate,可能存在性能问题,待验证
update = (id, params) =>
new Promise(resolve => {
const node = this.nodes.get(id) || null
const isNew = !node
const keeper = this.keepers.get(id)
const isNew = !keeper
const now = Date.now()

if (isNew) {
this.helpers = { ...this.helpers }
}

const node = this.nodes.get(id) || null
this.nodes.set(id, {
id,
createTime: now,
Expand All @@ -28,7 +24,14 @@ export default class AliveScope extends Component {
...params
})

this.forceUpdate(resolve)
if (isNew) {
this.helpers = { ...this.helpers }

this.forceUpdate(resolve)
} else {
const { children, bridgeProps } = params
keeper.setState({ children, bridgeProps }, resolve)
}
})
keep = (id, params) =>
new Promise(resolve => {
Expand Down Expand Up @@ -122,7 +125,15 @@ export default class AliveScope extends Component {
{children}
<div style={{ display: 'none' }}>
{[...this.nodes.values()].map(({ children, ...props }) => (
<Keeper key={props.id} {...props} store={this.store}>
<Keeper
key={props.id}
{...props}
store={this.store}
keepers={this.keepers}
ref={keeper => {
this.keepers.set(props.id, keeper)
}}
>
{children}
</Keeper>
))}
Expand Down
4 changes: 2 additions & 2 deletions src/core/KeepAlive.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class KeepAlive extends Component {
}, 100)
releaseUpdateTimes = debounce(() => {
this.updateTimes = 0
}, 32)
}, 16)
needForceStopUpdate = () => {
const needForceStopUpdate = this.updateTimes > 16
const needForceStopUpdate = this.updateTimes > 64

if (needForceStopUpdate) {
this.errorTips()
Expand Down
16 changes: 14 additions & 2 deletions src/core/Keeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import { LIFECYCLE_ACTIVATE, LIFECYCLE_UNACTIVATE } from './lifecycles'
export default class Keeper extends Component {
listeners = new Map()
wrapper = null

constructor(props, ...rest) {
super(props, ...rest)

this.state = {
children: props.children,
bridgeProps: props.bridgeProps
}
}

componentDidMount() {
const { store, id } = this.props
const listeners = this.listeners
Expand All @@ -32,7 +42,7 @@ export default class Keeper extends Component {
}

componentWillUnmount() {
const { store, id } = this.props
const { store, keepers, id } = this.props
// 卸载前尝试归位 DOM 节点
try {
const cache = store.get(id)
Expand All @@ -43,6 +53,7 @@ export default class Keeper extends Component {
// console.error(error) // do nothing
}
store.delete(id)
keepers.delete(id)
}

[LIFECYCLE_ACTIVATE]() {
Expand Down Expand Up @@ -105,7 +116,8 @@ export default class Keeper extends Component {
}

render() {
const { id, children, bridgeProps, ...props } = this.props
const { id, ...props } = this.props
const { children, bridgeProps } = this.state

return (
<div
Expand Down

0 comments on commit 45ed09e

Please sign in to comment.