mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 02:27:53 +08:00
perf: Make index of Node readonly to prevent potential remove child with complexity o(n^2)
This commit is contained in:
parent
6d34326084
commit
b9bb77e027
@ -83,7 +83,6 @@ export abstract class Container<
|
||||
this.getChildren().forEach((child) => {
|
||||
// reset parent to prevent many _setChildrenIndices calls
|
||||
child.parent = null;
|
||||
child.index = 0;
|
||||
child.remove();
|
||||
});
|
||||
this.children = [];
|
||||
@ -100,7 +99,6 @@ export abstract class Container<
|
||||
this.getChildren().forEach((child) => {
|
||||
// reset parent to prevent many _setChildrenIndices calls
|
||||
child.parent = null;
|
||||
child.index = 0;
|
||||
child.destroy();
|
||||
});
|
||||
this.children = [];
|
||||
@ -139,7 +137,6 @@ export abstract class Container<
|
||||
return this;
|
||||
}
|
||||
this._validateAdd(child);
|
||||
child.index = this.getChildren().length;
|
||||
child.parent = this;
|
||||
child._clearCaches();
|
||||
this.getChildren().push(child);
|
||||
@ -336,12 +333,6 @@ export abstract class Container<
|
||||
node._clearSelfAndDescendantCache(attr);
|
||||
});
|
||||
}
|
||||
_setChildrenIndices() {
|
||||
this.children?.forEach(function (child, n) {
|
||||
child.index = n;
|
||||
});
|
||||
this._requestDraw();
|
||||
}
|
||||
drawScene(can?: SceneCanvas, top?: Node, bufferCanvas?: SceneCanvas) {
|
||||
var layer = this.getLayer()!,
|
||||
canvas = can || (layer && layer.getCanvas()),
|
||||
|
18
src/Node.ts
18
src/Node.ts
@ -145,7 +145,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
|
||||
[index: string]: Array<{ name: string; handler: Function }>;
|
||||
} = {};
|
||||
attrs: any = {};
|
||||
index = 0;
|
||||
_allEventListeners: null | Array<Function> = null;
|
||||
parent: Container | null = null;
|
||||
_cache: Map<string, any> = new Map<string, any>();
|
||||
@ -172,6 +171,17 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
|
||||
// all change event listeners are attached to the prototype
|
||||
}
|
||||
|
||||
get index() {
|
||||
if (this.parent) {
|
||||
return this.parent.children.indexOf(this);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
set index(_val: number) {
|
||||
|
||||
}
|
||||
|
||||
hasChildren() {
|
||||
return false;
|
||||
}
|
||||
@ -854,7 +864,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
|
||||
|
||||
if (parent && parent.children) {
|
||||
parent.children.splice(this.index, 1);
|
||||
parent._setChildrenIndices();
|
||||
this.parent = null;
|
||||
}
|
||||
}
|
||||
@ -1361,7 +1370,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
|
||||
if (index < len - 1) {
|
||||
this.parent.children.splice(index, 1);
|
||||
this.parent.children.push(this);
|
||||
this.parent._setChildrenIndices();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -1382,7 +1390,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
|
||||
if (index < len - 1) {
|
||||
this.parent.children.splice(index, 1);
|
||||
this.parent.children.splice(index + 1, 0, this);
|
||||
this.parent._setChildrenIndices();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -1402,7 +1409,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
|
||||
if (index > 0) {
|
||||
this.parent.children.splice(index, 1);
|
||||
this.parent.children.splice(index - 1, 0, this);
|
||||
this.parent._setChildrenIndices();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -1422,7 +1428,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
|
||||
if (index > 0) {
|
||||
this.parent.children.splice(index, 1);
|
||||
this.parent.children.unshift(this);
|
||||
this.parent._setChildrenIndices();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -1444,7 +1449,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
|
||||
var index = this.index;
|
||||
this.parent.children.splice(index, 1);
|
||||
this.parent.children.splice(zIndex, 0, this);
|
||||
this.parent._setChildrenIndices();
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user