Skip to content

Commit 61d7910

Browse files
committed
feat: optimiz package size
1 parent f2b2a87 commit 61d7910

9 files changed

Lines changed: 132 additions & 410 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import type { IAnimate, IGraphic, IGroup } from '@visactor/vrender-core';
2+
3+
export type ExitReleaseCallbackState = {
4+
finalized: boolean;
5+
removeFromParent: boolean;
6+
onComplete: (() => void)[];
7+
};
8+
9+
export type AnimateExitReleaseState = ExitReleaseCallbackState & {
10+
pendingAnimates: Set<IAnimate>;
11+
};
12+
13+
export function collectTrackedAnimates(
14+
graphic: IGraphic,
15+
animates: IAnimate[] = [],
16+
visited: Set<IAnimate> = new Set()
17+
): IAnimate[] {
18+
const trackedAnimates = (graphic as any).getTrackedAnimates?.() ?? (graphic as any).animates;
19+
20+
trackedAnimates?.forEach((animate: IAnimate) => {
21+
if (animate && !visited.has(animate)) {
22+
visited.add(animate);
23+
animates.push(animate);
24+
}
25+
});
26+
27+
(graphic as IGroup).forEachChildren?.((child: IGraphic) => {
28+
collectTrackedAnimates(child, animates, visited);
29+
});
30+
31+
return animates;
32+
}
33+
34+
export function appendExitReleaseCallback(state: ExitReleaseCallbackState | undefined, callback?: () => void) {
35+
if (callback) {
36+
state?.onComplete.push(callback);
37+
}
38+
}
39+
40+
export function runExitReleaseCallbacks(callbacks: (() => void)[]) {
41+
callbacks.forEach(callback => {
42+
callback();
43+
});
44+
}
45+
46+
export function bindExitReleaseAnimates<T extends AnimateExitReleaseState>(
47+
exitAnimates: IAnimate[],
48+
getState: () => T | undefined,
49+
finalize: () => void
50+
) {
51+
const finish = (animate: IAnimate) => {
52+
const state = getState();
53+
if (!state || state.finalized || !state.pendingAnimates.has(animate)) {
54+
return;
55+
}
56+
57+
state.pendingAnimates.delete(animate);
58+
if (!state.pendingAnimates.size) {
59+
finalize();
60+
}
61+
};
62+
63+
exitAnimates.forEach(animate => {
64+
animate.onEnd(() => finish(animate));
65+
animate.onRemove(() => finish(animate));
66+
});
67+
}

packages/vrender-components/src/axis/base.ts

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* @description 坐标轴组件基类
55
*/
66
import type {
7-
IAnimate,
87
IGroup,
98
INode,
109
ITextGraphicAttribute,
@@ -41,15 +40,17 @@ import { getElMap, getVerticalCoord } from './util';
4140
import { dispatchClickState, dispatchHoverState, dispatchUnHoverState } from '../util/interaction';
4241
import { AnimateComponent } from '../animation/animate-component';
4342
import { commitUpdateAnimationTarget } from '../animation/static-truth';
43+
import {
44+
appendExitReleaseCallback,
45+
bindExitReleaseAnimates,
46+
collectTrackedAnimates,
47+
runExitReleaseCallbacks,
48+
type AnimateExitReleaseState
49+
} from '../animation/exit-release';
4450
import { DefaultAxisAnimation } from './animate/config';
4551
import type { IBaseScale } from '@visactor/vscale';
4652

47-
type AxisExitReleaseState = {
48-
pendingAnimates: Set<IAnimate>;
49-
finalized: boolean;
50-
removeFromParent: boolean;
51-
onComplete: (() => void)[];
52-
};
53+
type AxisExitReleaseState = AnimateExitReleaseState;
5354

5455
export abstract class AxisBase<T extends AxisBaseAttributes> extends AnimateComponent<Required<T>> {
5556
name = 'axis';
@@ -148,27 +149,6 @@ export abstract class AxisBase<T extends AxisBaseAttributes> extends AnimateComp
148149
return offscreenGroup.AABBBounds;
149150
}
150151

151-
private _collectTrackedAnimates(graphic: IGraphic, animates: IAnimate[], visited: Set<IAnimate>) {
152-
const trackedAnimates = (graphic as any).getTrackedAnimates?.() ?? (graphic as any).animates;
153-
154-
trackedAnimates?.forEach((animate: IAnimate) => {
155-
if (animate && !visited.has(animate)) {
156-
visited.add(animate);
157-
animates.push(animate);
158-
}
159-
});
160-
161-
(graphic as IGroup).forEachChildren?.((child: IGraphic) => {
162-
this._collectTrackedAnimates(child, animates, visited);
163-
});
164-
}
165-
166-
private _appendExitReleaseCallback(callback?: () => void) {
167-
if (callback) {
168-
this._exitReleaseState?.onComplete.push(callback);
169-
}
170-
}
171-
172152
private _finalizeExitRelease() {
173153
const state = this._exitReleaseState;
174154
if (state?.finalized) {
@@ -194,15 +174,13 @@ export abstract class AxisBase<T extends AxisBaseAttributes> extends AnimateComp
194174
(parent ?? this.parent)?.removeChild(this);
195175
}
196176

197-
callbacks.forEach(callback => {
198-
callback();
199-
});
177+
runExitReleaseCallbacks(callbacks);
200178
}
201179

202180
private _runExitAnimationBeforeRelease(options: ComponentExitReleaseOptions = {}) {
203181
if (this._exitReleaseState && !this._exitReleaseState.finalized) {
204182
this._exitReleaseState.removeFromParent = this._exitReleaseState.removeFromParent || !!options.removeFromParent;
205-
this._appendExitReleaseCallback(options.onComplete);
183+
appendExitReleaseCallback(this._exitReleaseState, options.onComplete);
206184
return true;
207185
}
208186

@@ -231,8 +209,7 @@ export abstract class AxisBase<T extends AxisBaseAttributes> extends AnimateComp
231209
return false;
232210
}
233211

234-
const existingAnimates: IAnimate[] = [];
235-
this._collectTrackedAnimates(this as unknown as IGraphic, existingAnimates, new Set());
212+
const existingAnimates = collectTrackedAnimates(this as unknown as IGraphic);
236213

237214
const {
238215
delay = 0,
@@ -255,8 +232,7 @@ export abstract class AxisBase<T extends AxisBaseAttributes> extends AnimateComp
255232
target.animate().wait(delay).to(endAttrs, duration, easing);
256233
});
257234

258-
const animates: IAnimate[] = [];
259-
this._collectTrackedAnimates(this as unknown as IGraphic, animates, new Set());
235+
const animates = collectTrackedAnimates(this as unknown as IGraphic);
260236
const exitAnimates = animates.filter(animate => !existingAnimates.includes(animate));
261237

262238
if (!exitAnimates.length) {
@@ -275,22 +251,11 @@ export abstract class AxisBase<T extends AxisBaseAttributes> extends AnimateComp
275251
onComplete: options.onComplete ? [options.onComplete] : []
276252
};
277253

278-
const finish = (animate: IAnimate) => {
279-
const state = this._exitReleaseState;
280-
if (!state || state.finalized || !state.pendingAnimates.has(animate)) {
281-
return;
282-
}
283-
284-
state.pendingAnimates.delete(animate);
285-
if (!state.pendingAnimates.size) {
286-
this._finalizeExitRelease();
287-
}
288-
};
289-
290-
exitAnimates.forEach(animate => {
291-
animate.onEnd(() => finish(animate));
292-
animate.onRemove(() => finish(animate));
293-
});
254+
bindExitReleaseAnimates(
255+
exitAnimates,
256+
() => this._exitReleaseState,
257+
() => this._finalizeExitRelease()
258+
);
294259

295260
return true;
296261
}

packages/vrender-components/src/label/base.ts

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type {
55
IGroup,
66
Text,
77
IGraphic,
8-
IAnimate,
98
IText,
109
FederatedPointerEvent,
1110
IColor,
@@ -60,6 +59,13 @@ import { loadLabelComponent } from './register';
6059
import { shiftY } from './overlap/shiftY';
6160
import { AnimateComponent } from '../animation/animate-component';
6261
import { commitUpdateAnimationTarget } from '../animation/static-truth';
62+
import {
63+
appendExitReleaseCallback,
64+
bindExitReleaseAnimates,
65+
collectTrackedAnimates,
66+
runExitReleaseCallbacks,
67+
type AnimateExitReleaseState
68+
} from '../animation/exit-release';
6369

6470
loadLabelComponent();
6571

@@ -76,12 +82,7 @@ function cloneAttributeSnapshot<T>(value: T): T {
7682
return snapshot as T;
7783
}
7884

79-
type LabelExitReleaseState = {
80-
pendingAnimates: Set<IAnimate>;
81-
finalized: boolean;
82-
removeFromParent: boolean;
83-
onComplete: (() => void)[];
84-
};
85+
type LabelExitReleaseState = AnimateExitReleaseState;
8586

8687
export class LabelBase<T extends BaseLabelAttrs> extends AnimateComponent<T> {
8788
name = 'label';
@@ -196,27 +197,6 @@ export class LabelBase<T extends BaseLabelAttrs> extends AnimateComponent<T> {
196197
}
197198
}
198199

199-
private _collectTrackedAnimates(graphic: IGraphic, animates: IAnimate[], visited: Set<IAnimate>) {
200-
const trackedAnimates = (graphic as any).getTrackedAnimates?.() ?? (graphic as any).animates;
201-
202-
trackedAnimates?.forEach((animate: IAnimate) => {
203-
if (animate && !visited.has(animate)) {
204-
visited.add(animate);
205-
animates.push(animate);
206-
}
207-
});
208-
209-
(graphic as IGroup).forEachChildren?.((child: IGraphic) => {
210-
this._collectTrackedAnimates(child, animates, visited);
211-
});
212-
}
213-
214-
private _appendExitReleaseCallback(callback?: () => void) {
215-
if (callback) {
216-
this._exitReleaseState?.onComplete.push(callback);
217-
}
218-
}
219-
220200
private _finalizeExitRelease() {
221201
const state = this._exitReleaseState;
222202
if (state?.finalized) {
@@ -242,15 +222,13 @@ export class LabelBase<T extends BaseLabelAttrs> extends AnimateComponent<T> {
242222
(parent ?? this.parent)?.removeChild(this);
243223
}
244224

245-
callbacks.forEach(callback => {
246-
callback();
247-
});
225+
runExitReleaseCallbacks(callbacks);
248226
}
249227

250228
private _runExitAnimationBeforeRelease(options: ComponentExitReleaseOptions = {}) {
251229
if (this._exitReleaseState && !this._exitReleaseState.finalized) {
252230
this._exitReleaseState.removeFromParent = this._exitReleaseState.removeFromParent || !!options.removeFromParent;
253-
this._appendExitReleaseCallback(options.onComplete);
231+
appendExitReleaseCallback(this._exitReleaseState, options.onComplete);
254232
return true;
255233
}
256234

@@ -278,8 +256,7 @@ export class LabelBase<T extends BaseLabelAttrs> extends AnimateComponent<T> {
278256
return false;
279257
}
280258

281-
const existingAnimates: IAnimate[] = [];
282-
this._collectTrackedAnimates(this as unknown as IGraphic, existingAnimates, new Set());
259+
const existingAnimates = collectTrackedAnimates(this as unknown as IGraphic);
283260

284261
exitTargets.forEach(target => {
285262
target.applyAnimationState(
@@ -297,8 +274,7 @@ export class LabelBase<T extends BaseLabelAttrs> extends AnimateComponent<T> {
297274
);
298275
});
299276

300-
const animates: IAnimate[] = [];
301-
this._collectTrackedAnimates(this as unknown as IGraphic, animates, new Set());
277+
const animates = collectTrackedAnimates(this as unknown as IGraphic);
302278
const exitAnimates = animates.filter(animate => !existingAnimates.includes(animate));
303279

304280
if (!exitAnimates.length) {
@@ -316,22 +292,11 @@ export class LabelBase<T extends BaseLabelAttrs> extends AnimateComponent<T> {
316292
onComplete: options.onComplete ? [options.onComplete] : []
317293
};
318294

319-
const finish = (animate: IAnimate) => {
320-
const state = this._exitReleaseState;
321-
if (!state || state.finalized || !state.pendingAnimates.has(animate)) {
322-
return;
323-
}
324-
325-
state.pendingAnimates.delete(animate);
326-
if (!state.pendingAnimates.size) {
327-
this._finalizeExitRelease();
328-
}
329-
};
330-
331-
exitAnimates.forEach(animate => {
332-
animate.onEnd(() => finish(animate));
333-
animate.onRemove(() => finish(animate));
334-
});
295+
bindExitReleaseAnimates(
296+
exitAnimates,
297+
() => this._exitReleaseState,
298+
() => this._finalizeExitRelease()
299+
);
335300

336301
return true;
337302
}

packages/vrender-components/src/label/dataLabel.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import type { DataLabelAttrs } from './type';
77
import { LabelBase as PointLabel, type LabelBase } from './base';
88
import type { ComponentOptions } from '../interface';
99
import { getLabelComponent } from './data-label-register';
10+
import {
11+
appendExitReleaseCallback,
12+
runExitReleaseCallbacks,
13+
type ExitReleaseCallbackState
14+
} from '../animation/exit-release';
1015

11-
type DataLabelExitReleaseState = {
16+
type DataLabelExitReleaseState = ExitReleaseCallbackState & {
1217
pendingCount: number;
13-
finalized: boolean;
14-
removeFromParent: boolean;
15-
onComplete: (() => void)[];
1618
};
1719

1820
export class DataLabel extends AbstractComponent<DataLabelAttrs> {
@@ -93,12 +95,6 @@ export class DataLabel extends AbstractComponent<DataLabelAttrs> {
9395
this._componentMap = currentComponentMap;
9496
}
9597

96-
private _appendExitReleaseCallback(callback?: () => void) {
97-
if (callback) {
98-
this._exitReleaseState?.onComplete.push(callback);
99-
}
100-
}
101-
10298
private _finalizeExitRelease() {
10399
const state = this._exitReleaseState;
104100
if (state?.finalized) {
@@ -121,9 +117,7 @@ export class DataLabel extends AbstractComponent<DataLabelAttrs> {
121117
(parent ?? this.parent)?.removeChild(this);
122118
}
123119

124-
callbacks.forEach(callback => {
125-
callback();
126-
});
120+
runExitReleaseCallbacks(callbacks);
127121
}
128122

129123
releaseWithExitAnimation(options: ComponentExitReleaseOptions = {}): boolean {
@@ -133,7 +127,7 @@ export class DataLabel extends AbstractComponent<DataLabelAttrs> {
133127

134128
if (this._exitReleaseState && !this._exitReleaseState.finalized) {
135129
this._exitReleaseState.removeFromParent = this._exitReleaseState.removeFromParent || !!options.removeFromParent;
136-
this._appendExitReleaseCallback(options.onComplete);
130+
appendExitReleaseCallback(this._exitReleaseState, options.onComplete);
137131
return true;
138132
}
139133

0 commit comments

Comments
 (0)