From 45ed09e8a87a57485000d244a68be85a1e3ca9d0 Mon Sep 17 00:00:00 2001
From: CJY <375564567@qq.com>
Date: Sun, 24 Nov 2019 18:59:33 +0800
Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E8=A7=A3=E9=99=A4=20KeepAliv?=
=?UTF-8?q?e=20children=20update=20=E6=97=B6=E4=BA=A6=E8=A7=A6=E5=8F=91=20?=
=?UTF-8?q?AliveScope=20=E6=9B=B4=E6=96=B0=E7=9A=84=E6=80=A7=E8=83=BD?=
=?UTF-8?q?=E6=8D=9F=E8=80=97=E9=97=AE=E9=A2=98=EF=BC=8C=E5=8F=AA=E6=9B=B4?=
=?UTF-8?q?=E6=96=B0=E5=85=B6=E5=AF=B9=E5=BA=94=20Keeper=EF=BC=9BKeepAlive?=
=?UTF-8?q?=20=E7=9E=AC=E6=97=B6=E6=9B=B4=E6=96=B0=E9=94=81=E6=A3=80?=
=?UTF-8?q?=E6=B5=8B=E6=9D=A1=E4=BB=B6=E6=94=BE=E5=AE=BD=EF=BC=8C=E5=87=8F?=
=?UTF-8?q?=E8=BD=BB=E5=AF=B9=E6=AD=A3=E5=B8=B8=E6=9B=B4=E6=96=B0=E8=A1=8C?=
=?UTF-8?q?=E4=B8=BA=E7=9A=84=E8=AF=AF=E5=88=A4=E6=83=85=E5=86=B5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
package.json | 2 +-
src/core/AliveScope.js | 31 +++++++++++++++++++++----------
src/core/KeepAlive.js | 4 ++--
src/core/Keeper.js | 16 ++++++++++++++--
4 files changed, 38 insertions(+), 15 deletions(-)
diff --git a/package.json b/package.json
index a0e6828..4d60947 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "react-activation",
- "version": "0.3.2",
+ "version": "0.3.3",
"description": " for React like in vue",
"main": "index.js",
"scripts": {
diff --git a/src/core/AliveScope.js b/src/core/AliveScope.js
index 9a76f83..1c71afc 100644
--- a/src/core/AliveScope.js
+++ b/src/core/AliveScope.js
@@ -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,
@@ -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 => {
@@ -122,7 +125,15 @@ export default class AliveScope extends Component {
{children}
{[...this.nodes.values()].map(({ children, ...props }) => (
-
+ {
+ this.keepers.set(props.id, keeper)
+ }}
+ >
{children}
))}
diff --git a/src/core/KeepAlive.js b/src/core/KeepAlive.js
index 2cdb298..6663f57 100644
--- a/src/core/KeepAlive.js
+++ b/src/core/KeepAlive.js
@@ -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()
diff --git a/src/core/Keeper.js b/src/core/Keeper.js
index 6f87b12..c2bda2f 100644
--- a/src/core/Keeper.js
+++ b/src/core/Keeper.js
@@ -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
@@ -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)
@@ -43,6 +53,7 @@ export default class Keeper extends Component {
// console.error(error) // do nothing
}
store.delete(id)
+ keepers.delete(id)
}
[LIFECYCLE_ACTIVATE]() {
@@ -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 (