From 580164bd667fca6de517290978ea3c2d2bb8e5a7 Mon Sep 17 00:00:00 2001 From: zhourengui Date: Mon, 16 Sep 2024 19:08:26 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix=EF=BC=9Aelement=20remove=20leak?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CSSMotionList.tsx | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/CSSMotionList.tsx b/src/CSSMotionList.tsx index 2b2f03a..816578d 100644 --- a/src/CSSMotionList.tsx +++ b/src/CSSMotionList.tsx @@ -92,19 +92,10 @@ export function genCSSMotionList( const mixedKeyEntities = diffKeys(keyEntities, parsedKeyObjects); return { - keyEntities: mixedKeyEntities.filter(entity => { - const prevEntity = keyEntities.find(({ key }) => entity.key === key); - + keyEntities: mixedKeyEntities.filter( // Remove if already mark as removed - if ( - prevEntity && - prevEntity.status === STATUS_REMOVED && - entity.status === STATUS_REMOVE - ) { - return false; - } - return true; - }), + entity => entity.status !== STATUS_REMOVE, + ), }; } From eb9115ae6940efba35a9a5c046a0154627761609 Mon Sep 17 00:00:00 2001 From: zhourengui <729049182@qq.com> Date: Wed, 18 Sep 2024 23:49:08 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=BD=93=E5=85=83=E7=B4=A0=E4=B8=8D?= =?UTF-8?q?=E5=8F=AF=E8=A7=81=E6=97=B6=20=E5=9C=A8removekey=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=88=B0=E7=9A=84state=E4=BF=9D=E8=AF=81=E6=98=AF?= =?UTF-8?q?=E6=9C=80=E6=96=B0=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CSSMotionList.tsx | 62 +++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/src/CSSMotionList.tsx b/src/CSSMotionList.tsx index 816578d..c3d2b94 100644 --- a/src/CSSMotionList.tsx +++ b/src/CSSMotionList.tsx @@ -1,5 +1,6 @@ /* eslint react/prop-types: 0 */ import * as React from 'react'; + import type { CSSMotionProps } from './CSSMotion'; import OriginCSSMotion from './CSSMotion'; import type { KeyObject } from './util/diff'; @@ -92,30 +93,49 @@ export function genCSSMotionList( const mixedKeyEntities = diffKeys(keyEntities, parsedKeyObjects); return { - keyEntities: mixedKeyEntities.filter( + keyEntities: mixedKeyEntities.filter(entity => { + const prevEntity = keyEntities.find(({ key }) => entity.key === key); + // Remove if already mark as removed - entity => entity.status !== STATUS_REMOVE, - ), + if ( + prevEntity && + prevEntity.status === STATUS_REMOVED && + entity.status === STATUS_REMOVE + ) { + return false; + } + return true; + }), }; } // ZombieJ: Return the count of rest keys. It's safe to refactor if need more info. removeKey = (removeKey: React.Key) => { - const { keyEntities } = this.state; - const nextKeyEntities = keyEntities.map(entity => { - if (entity.key !== removeKey) return entity; - return { - ...entity, - status: STATUS_REMOVED, - }; - }); - - this.setState({ - keyEntities: nextKeyEntities, - }); - - return nextKeyEntities.filter(({ status }) => status !== STATUS_REMOVED) - .length; + this.setState( + prevState => { + const nextKeyEntities = prevState.keyEntities.map(entity => { + if (entity.key !== removeKey) return entity; + return { + ...entity, + status: STATUS_REMOVED, + }; + }); + + return { + keyEntities: nextKeyEntities, + }; + }, + () => { + const { keyEntities } = this.state; + const restKeysCount = keyEntities.filter( + ({ status }) => status !== STATUS_REMOVED, + ).length; + + if (restKeysCount === 0 && this.props.onAllRemoved) { + this.props.onAllRemoved(); + } + }, + ); }; render() { @@ -151,11 +171,7 @@ export function genCSSMotionList( onVisibleChanged?.(changedVisible, { key: eventProps.key }); if (!changedVisible) { - const restKeysCount = this.removeKey(eventProps.key); - - if (restKeysCount === 0 && onAllRemoved) { - onAllRemoved(); - } + this.removeKey(eventProps.key); } }} > From 377c63c43b442ead105d245620a472b22d82bc8a Mon Sep 17 00:00:00 2001 From: Little Boy <729049182@qq.com> Date: Wed, 18 Sep 2024 23:57:52 +0800 Subject: [PATCH 3/3] Clear code --- src/CSSMotionList.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/CSSMotionList.tsx b/src/CSSMotionList.tsx index c3d2b94..2a3d478 100644 --- a/src/CSSMotionList.tsx +++ b/src/CSSMotionList.tsx @@ -1,6 +1,5 @@ /* eslint react/prop-types: 0 */ import * as React from 'react'; - import type { CSSMotionProps } from './CSSMotion'; import OriginCSSMotion from './CSSMotion'; import type { KeyObject } from './util/diff';