Skip to content

Commit 3b0bb42

Browse files
committed
fix: fix add/remove hitbox item
1 parent 34d32d1 commit 3b0bb42

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

src/components/canvas/anchors/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { ESchedulerPriority } from "../../../lib";
12
import { ECameraScaleLevel } from "../../../services/camera/CameraService";
2-
import { frameDebouncer } from "../../../services/optimizations/frameDebouncer";
33
import { AnchorState, EAnchorType } from "../../../store/anchor/Anchor";
44
import { TBlockId } from "../../../store/block/Block";
55
import { selectBlockAnchor } from "../../../store/block/selectors";
6-
import { isMetaKeyEvent } from "../../../utils/functions";
6+
import { debounce, isMetaKeyEvent } from "../../../utils/functions";
77
import { TPoint } from "../../../utils/types/shapes";
88
import { GraphComponent } from "../GraphComponent";
99
import { GraphLayer, TGraphLayerContext } from "../layers/graphLayer/GraphLayer";
@@ -48,14 +48,14 @@ export class Anchor<T extends TAnchorProps = TAnchorProps> extends GraphComponen
4848

4949
private hitBoxHash: string;
5050

51-
private debouncedSetHitBox = frameDebouncer.add(
51+
private debouncedSetHitBox = debounce(
5252
() => {
5353
const { x, y } = this.props.getPosition(this.props);
5454
this.setHitBox(x - this.shift, y - this.shift, x + this.shift, y + this.shift);
5555
},
5656
{
57-
delay: 4,
58-
lightFrame: true,
57+
priority: ESchedulerPriority.HIGHEST,
58+
frameInterval: 4,
5959
}
6060
);
6161

@@ -90,6 +90,11 @@ export class Anchor<T extends TAnchorProps = TAnchorProps> extends GraphComponen
9090
return params ? this.context.camera.isRectVisible(...params) : true;
9191
}
9292

93+
protected unmount() {
94+
super.unmount();
95+
this.debouncedSetHitBox.cancel();
96+
}
97+
9398
public didIterate(): void {
9499
const { x: poxX, y: posY } = this.props.getPosition(this.props);
95100
const hash = `${poxX}/${posY}/${this.shift}`;

src/services/HitTest.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,9 @@ export class HitTest extends Emitter {
5959
() => {
6060
const items = [];
6161
for (const [item, bbox] of this.queue) {
62-
if (bbox === null) {
63-
// Remove operation
64-
this.tree.remove(item);
65-
} else {
66-
// Add/update operation
67-
this.tree.remove(item);
68-
// item.updateRect(bbox);
62+
this.tree.remove(item);
63+
if (bbox) {
64+
item.updateRect(bbox);
6965
items.push(item);
7066
}
7167
}
@@ -76,7 +72,7 @@ export class HitTest extends Emitter {
7672
},
7773
{
7874
priority: ESchedulerPriority.LOWEST,
79-
frameTimeout: 100,
75+
frameInterval: 1,
8076
}
8177
);
8278

@@ -98,24 +94,22 @@ export class HitTest extends Emitter {
9894
}
9995

10096
public add(item: HitBox) {
101-
this.queue.set(item, {
102-
minX: item.minX,
103-
minY: item.minY,
104-
maxX: item.maxX,
105-
maxY: item.maxY,
106-
x: item.x,
107-
y: item.y,
108-
});
97+
if (item.destroyed) {
98+
return;
99+
}
100+
this.queue.set(item, item);
109101
this.processQueue();
110102
}
111103

112104
public waitUsableRectUpdate(callback: (rect: TRect) => void) {
113105
if (this.isUnstable) {
114-
const fn = () => {
115-
this.waitUsableRectUpdate(callback);
116-
};
117-
this.once("update", fn);
118-
return () => this.off("update", fn);
106+
const removeListener = this.$usableRect.subscribe(() => {
107+
if (!this.isUnstable) {
108+
removeListener();
109+
callback(this.$usableRect.value);
110+
}
111+
});
112+
return removeListener;
119113
}
120114
callback(this.$usableRect.value);
121115
return noop;
@@ -173,8 +167,8 @@ export class HitTest extends Emitter {
173167
}
174168

175169
public destroy(): void {
170+
this.clear();
176171
super.destroy();
177-
this.processQueue.cancel();
178172
}
179173

180174
public testHitBox(item: HitBoxData): Component[] {
@@ -201,7 +195,7 @@ export class HitTest extends Emitter {
201195
}
202196

203197
export class HitBox implements IHitBox {
204-
private destroyed = false;
198+
public destroyed = false;
205199

206200
public maxX: number;
207201

@@ -217,6 +211,8 @@ export class HitBox implements IHitBox {
217211

218212
private rect: [number, number, number, number] = [0, 0, 0, 0];
219213

214+
protected unstable = true;
215+
220216
constructor(
221217
public item: { zIndex: number } & Component & IWithHitTest,
222218
protected hitTest: HitTest
@@ -230,12 +226,14 @@ export class HitBox implements IHitBox {
230226
this.x = rect.x;
231227
this.y = rect.y;
232228
this.rect = [this.minX, this.minY, this.maxX - this.minX, this.maxY - this.minY];
229+
this.unstable = false;
233230
}
234231

235232
public update = (minX: number, minY: number, maxX: number, maxY: number, force?: boolean): void => {
236233
if (this.destroyed) return;
237234
if (minX === this.minX && minY === this.minY && maxX === this.maxX && maxY === this.maxY && !force) return;
238-
this.updateRect({ minX, minY, maxX, maxY, x: this.x, y: this.y });
235+
this.unstable = true;
236+
this.rect = [minX, minY, maxX - minX, maxY - minY];
239237
this.hitTest.update(this, { minX, minY, maxX, maxY, x: this.x, y: this.y }, force);
240238
};
241239

0 commit comments

Comments
 (0)