Skip to content

Commit 8df267d

Browse files
Alexzjtyunchen.wywang1212
authored
perf: batch update attributes (#2001)
* perf: batch update attributes * perf: batch update attributes * 0.0.1-0 * chore: add changeset * chore: revert change --------- Co-authored-by: yunchen.wy <yunchen.wy@antgroup.com> Co-authored-by: wang1212 <mrwang1212@126.com>
1 parent 00d08e9 commit 8df267d

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

.changeset/modern-queens-knock.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@antv/g-lite': minor
3+
---
4+
5+
feat: add experimental api `setAttributes`

packages/g-lite/src/css/interfaces.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ export interface PropertyParseOptions {
131131
forceUpdateGeometry: boolean;
132132
usedAttributes: string[];
133133
memoize: boolean;
134+
/**
135+
* @experimental
136+
*/
137+
skipDispatchAttrModifiedEvent?: boolean;
134138
}
135139

136140
export interface StyleValueRegistry {

packages/g-lite/src/display-objects/DisplayObject.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,43 @@ export class DisplayObject<
275275
}
276276
}
277277

278+
/**
279+
* batch update attributes without attributeChangedCallback, for performance
280+
* use with caution
281+
* @param attributes
282+
* @param parseOptions
283+
* @experimental
284+
*/
285+
setAttributes(
286+
attributes: Partial<StyleProps>,
287+
parseOptions: Partial<PropertyParseOptions> = {},
288+
) {
289+
const { skipDispatchAttrModifiedEvent = false } = parseOptions;
290+
let oldAttributes;
291+
let oldParsedValues;
292+
if (!skipDispatchAttrModifiedEvent) {
293+
oldAttributes = { ...this.attributes };
294+
oldParsedValues = { ...this.parsedStyle };
295+
}
296+
runtime.styleValueRegistry.processProperties(
297+
this as unknown as DisplayObject,
298+
attributes,
299+
parseOptions,
300+
);
301+
// redraw at next frame
302+
this.dirty();
303+
if (!skipDispatchAttrModifiedEvent) {
304+
for (const key in attributes) {
305+
this.dispatchAttrModifiedEvent(
306+
key,
307+
oldAttributes[key],
308+
attributes[key],
309+
oldParsedValues[key as string],
310+
);
311+
}
312+
}
313+
}
314+
278315
/**
279316
* called when attributes get changed or initialized
280317
*/
@@ -297,6 +334,17 @@ export class DisplayObject<
297334
// redraw at next frame
298335
this.dirty();
299336

337+
// return;
338+
339+
this.dispatchAttrModifiedEvent(name, oldValue, value, oldParsedValue);
340+
}
341+
342+
private dispatchAttrModifiedEvent<Key extends keyof StyleProps>(
343+
name: Key,
344+
oldValue: StyleProps[Key],
345+
value: StyleProps[Key],
346+
oldParsedValue: ParsedStyleProps[keyof ParsedStyleProps],
347+
) {
300348
const newParsedValue = this.parsedStyle[name as string];
301349
if (this.isConnected) {
302350
attrModifiedEvent.relatedNode = this as IElement;

0 commit comments

Comments
 (0)