mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 15:23:44 +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) => {
|
this.getChildren().forEach((child) => {
|
||||||
// reset parent to prevent many _setChildrenIndices calls
|
// reset parent to prevent many _setChildrenIndices calls
|
||||||
child.parent = null;
|
child.parent = null;
|
||||||
child.index = 0;
|
|
||||||
child.remove();
|
child.remove();
|
||||||
});
|
});
|
||||||
this.children = [];
|
this.children = [];
|
||||||
@ -100,7 +99,6 @@ export abstract class Container<
|
|||||||
this.getChildren().forEach((child) => {
|
this.getChildren().forEach((child) => {
|
||||||
// reset parent to prevent many _setChildrenIndices calls
|
// reset parent to prevent many _setChildrenIndices calls
|
||||||
child.parent = null;
|
child.parent = null;
|
||||||
child.index = 0;
|
|
||||||
child.destroy();
|
child.destroy();
|
||||||
});
|
});
|
||||||
this.children = [];
|
this.children = [];
|
||||||
@ -139,7 +137,6 @@ export abstract class Container<
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
this._validateAdd(child);
|
this._validateAdd(child);
|
||||||
child.index = this.getChildren().length;
|
|
||||||
child.parent = this;
|
child.parent = this;
|
||||||
child._clearCaches();
|
child._clearCaches();
|
||||||
this.getChildren().push(child);
|
this.getChildren().push(child);
|
||||||
@ -336,12 +333,6 @@ export abstract class Container<
|
|||||||
node._clearSelfAndDescendantCache(attr);
|
node._clearSelfAndDescendantCache(attr);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
_setChildrenIndices() {
|
|
||||||
this.children?.forEach(function (child, n) {
|
|
||||||
child.index = n;
|
|
||||||
});
|
|
||||||
this._requestDraw();
|
|
||||||
}
|
|
||||||
drawScene(can?: SceneCanvas, top?: Node, bufferCanvas?: SceneCanvas) {
|
drawScene(can?: SceneCanvas, top?: Node, bufferCanvas?: SceneCanvas) {
|
||||||
var layer = this.getLayer()!,
|
var layer = this.getLayer()!,
|
||||||
canvas = can || (layer && layer.getCanvas()),
|
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 }>;
|
[index: string]: Array<{ name: string; handler: Function }>;
|
||||||
} = {};
|
} = {};
|
||||||
attrs: any = {};
|
attrs: any = {};
|
||||||
index = 0;
|
|
||||||
_allEventListeners: null | Array<Function> = null;
|
_allEventListeners: null | Array<Function> = null;
|
||||||
parent: Container | null = null;
|
parent: Container | null = null;
|
||||||
_cache: Map<string, any> = new Map<string, any>();
|
_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
|
// 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() {
|
hasChildren() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -854,7 +864,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
|
|||||||
|
|
||||||
if (parent && parent.children) {
|
if (parent && parent.children) {
|
||||||
parent.children.splice(this.index, 1);
|
parent.children.splice(this.index, 1);
|
||||||
parent._setChildrenIndices();
|
|
||||||
this.parent = null;
|
this.parent = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1361,7 +1370,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
|
|||||||
if (index < len - 1) {
|
if (index < len - 1) {
|
||||||
this.parent.children.splice(index, 1);
|
this.parent.children.splice(index, 1);
|
||||||
this.parent.children.push(this);
|
this.parent.children.push(this);
|
||||||
this.parent._setChildrenIndices();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -1382,7 +1390,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
|
|||||||
if (index < len - 1) {
|
if (index < len - 1) {
|
||||||
this.parent.children.splice(index, 1);
|
this.parent.children.splice(index, 1);
|
||||||
this.parent.children.splice(index + 1, 0, this);
|
this.parent.children.splice(index + 1, 0, this);
|
||||||
this.parent._setChildrenIndices();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -1402,7 +1409,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
|
|||||||
if (index > 0) {
|
if (index > 0) {
|
||||||
this.parent.children.splice(index, 1);
|
this.parent.children.splice(index, 1);
|
||||||
this.parent.children.splice(index - 1, 0, this);
|
this.parent.children.splice(index - 1, 0, this);
|
||||||
this.parent._setChildrenIndices();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -1422,7 +1428,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
|
|||||||
if (index > 0) {
|
if (index > 0) {
|
||||||
this.parent.children.splice(index, 1);
|
this.parent.children.splice(index, 1);
|
||||||
this.parent.children.unshift(this);
|
this.parent.children.unshift(this);
|
||||||
this.parent._setChildrenIndices();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -1444,7 +1449,6 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
|
|||||||
var index = this.index;
|
var index = this.index;
|
||||||
this.parent.children.splice(index, 1);
|
this.parent.children.splice(index, 1);
|
||||||
this.parent.children.splice(zIndex, 0, this);
|
this.parent.children.splice(zIndex, 0, this);
|
||||||
this.parent._setChildrenIndices();
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user