From a5933d8607e63ed08818ffab43e858863cb35d47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claud=C3=A9ric=20Demers?= Date: Thu, 12 Sep 2024 13:05:08 -0400 Subject: [PATCH] Move equality check from observer to notifier --- .changeset/notifier-observer.md | 5 +++++ packages/abstract/src/core/collision/notifier.ts | 13 +++++++++++++ packages/abstract/src/core/collision/observer.ts | 8 -------- 3 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 .changeset/notifier-observer.md diff --git a/.changeset/notifier-observer.md b/.changeset/notifier-observer.md new file mode 100644 index 00000000..9514bfd4 --- /dev/null +++ b/.changeset/notifier-observer.md @@ -0,0 +1,5 @@ +--- +'@dnd-kit/abstract': patch +--- + +Move responsibility from `CollisionObserver` to `CollisionNotifier` to check if the previous collisions are equal to the next collisions. diff --git a/packages/abstract/src/core/collision/notifier.ts b/packages/abstract/src/core/collision/notifier.ts index 94d791b6..11e59975 100644 --- a/packages/abstract/src/core/collision/notifier.ts +++ b/packages/abstract/src/core/collision/notifier.ts @@ -4,10 +4,17 @@ import {DragDropManager} from '../manager/index.ts'; import {CorePlugin} from '../plugins/index.ts'; import {defaultPreventable} from '../manager/events.ts'; +import type {Collision} from './types.ts'; + export class CollisionNotifier extends CorePlugin { constructor(manager: DragDropManager) { super(manager); + const isEqual = (a: Collision[], b: Collision[]) => + a.map(({id}) => id).join('') === b.map(({id}) => id).join(''); + + let previousCollisions: Collision[] = []; + this.destroy = effect(() => { const {collisionObserver, monitor} = manager; const {collisions} = collisionObserver; @@ -26,6 +33,12 @@ export class CollisionNotifier extends CorePlugin { return; } + if (isEqual(collisions, previousCollisions)) { + return; + } else { + previousCollisions = collisions; + } + const [firstCollision] = collisions; untracked(() => { diff --git a/packages/abstract/src/core/collision/observer.ts b/packages/abstract/src/core/collision/observer.ts index d0f08af6..8a651ec4 100644 --- a/packages/abstract/src/core/collision/observer.ts +++ b/packages/abstract/src/core/collision/observer.ts @@ -20,19 +20,11 @@ export class CollisionObserver< this.computeCollisions = this.computeCollisions.bind(this); this.#collisions = signal(DEFAULT_VALUE); - const isEqual = (a: Collision[], b: Collision[]) => - a.map(({id}) => id).join('') === b.map(({id}) => id).join(''); - let previousCoordinates: Coordinates = {x: 0, y: 0}; this.destroy = effects( () => { const collisions = this.computeCollisions(); - const previousCollisions = this.#collisions.peek(); - - if (isEqual(collisions, previousCollisions)) { - return; - } const coordinates = untracked( () => this.manager.dragOperation.position.current