Skip to content

Commit

Permalink
fix: 需要在每次更新 coord 前清理已销毁的组件
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue committed Oct 8, 2023
1 parent 506bc33 commit a2139b9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
13 changes: 6 additions & 7 deletions packages/f2/src/chart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ class Chart<

const { scale: scaleOptions, coord: coordOption } = props;

const style = this.getStyle(props);
coord.updateLayout(style);
this.resetCoordLayout();

// 初始化 scale
scale.create(scaleOptions);
Expand Down Expand Up @@ -136,8 +135,6 @@ class Chart<

willUpdate(): void {
this.coord.create(this.props.coord);
// 清除已经卸载的component对应的position缓存
this.removeComponentsPositionCache();
}

on(eventName: string, listener: (...args: any[]) => void) {
Expand Down Expand Up @@ -190,6 +187,8 @@ class Chart<

// 先重置,然后整体重新算一次
this.resetCoordLayout();
// 再整体计算前,需要去掉已经销毁的组件
this.removeComponentsPositionCache();
componentsPosition.forEach((componentPosition) => {
const { layout } = componentPosition;
this.updateCoordLayout(layout);
Expand All @@ -204,11 +203,11 @@ class Chart<

removeComponentsPositionCache() {
if (!this.componentsPosition?.length) return;
for(let i = this.componentsPosition.length; i > -1; i--) {

for (let i = this.componentsPosition.length; i > -1; i--) {
const item = this.componentsPosition[i];
if (item && item.component && item.component.destroyed) {
this.componentsPosition.splice(i, 1)
this.componentsPosition.splice(i, 1);
}
}
}
Expand Down
16 changes: 4 additions & 12 deletions packages/f2/test/chart/changeDataWithUndefined.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ describe('Chart', () => {
it('Chart 切换data为undefined', async () => {
const { props } = (
<Canvas context={context} pixelRatio={1}>
<Chart<TRecord>
data={data}
>
<Chart<TRecord> data={data}>
<Axis field="genre" />
<Axis<TRecord> field="sold" />
<Interval<TRecord> x="genre" y="sold" color="genre" />
Expand All @@ -37,9 +35,7 @@ describe('Chart', () => {
canvas.update(
(
<Canvas context={context} pixelRatio={1} animate={false}>
<Chart
data={undefined}
>
<Chart data={undefined}>
<Axis field="genre" />
<Axis field="sold" />
<Interval x="genre" y="sold" color="genre" />
Expand All @@ -53,9 +49,7 @@ describe('Chart', () => {
canvas.update(
(
<Canvas context={context} pixelRatio={1} animate={false}>
<Chart
data={data}
>
<Chart data={data}>
<Axis field="genre" />
<Axis field="sold" />
<Interval x="genre" y="sold" color="genre" />
Expand All @@ -69,9 +63,7 @@ describe('Chart', () => {
canvas.update(
(
<Canvas context={context} pixelRatio={1} animate={false}>
<Chart
data={data.slice(0, 5)}
>
<Chart data={data.slice(0, 5)}>
<Axis field="genre" />
<Axis field="sold" />
<Interval x="genre" y="sold" color="genre" />
Expand Down

0 comments on commit a2139b9

Please sign in to comment.