fix(dnd): isolate pending drop and registration lifecycles
Quality / quality (ubuntu-latest) (push) Failing after 0s
Quality / quality (windows-latest) (push) Canceled after 0s

This commit is contained in:
Clintchiz committed 2026-09-05 14:35:33 +05:30
1 parent bb28b8759a
commit 4ac31d750e
4 files changed
+176 -6

No files matched your search

+34 -5
View File
@@ -20,14 +20,19 @@ export function createDndManager(options: DndManagerOptions): DndManager {
>();
const manager = new DragDropManager();
let activeIds: Array<string | number> = [];
let generation = 0;
let destroyed = false;
manager.monitor.addEventListener("dragstart", (event) => {
if (destroyed) return;
generation++;
const source = event.operation.source;
if (!source) return;
activeIds = [source.id];
options.announce?.({ type: "pickedUp", ids: activeIds });
});
manager.monitor.addEventListener("dragover", (event) => {
if (destroyed) return;
const source = event.operation.source as
(typeof event.operation.source & { group?: string | number; index?: number }) | null;
if (!source) return;
@@ -38,6 +43,9 @@ export function createDndManager(options: DndManagerOptions): DndManager {
} else options.announce?.({ type: "movedOver", ids: activeIds, location: destination });
});
manager.monitor.addEventListener("dragend", async (event) => {
if (destroyed) return;
const endedGeneration = generation;
const ids = [...activeIds];
const source = event.operation.source as
| (typeof event.operation.source & {
group?: string | number;
@@ -47,12 +55,12 @@ export function createDndManager(options: DndManagerOptions): DndManager {
})
| null;
if (!source || event.operation.canceled) {
options.announce?.({ type: "cancelled", ids: activeIds });
options.announce?.({ type: "cancelled", ids });
activeIds = [];
return;
}
const intent: DndMoveIntent = {
ids: [...activeIds],
ids,
from: [
{
group: source.initialGroup ?? source.group ?? "default",
@@ -61,17 +69,32 @@ export function createDndManager(options: DndManagerOptions): DndManager {
],
to: { group: source.group ?? "default", index: source.index ?? 0 },
};
const accepted = await options.onMoveIntent(intent);
// A pending commit must not own the next gesture's selection or lifecycle.
activeIds = [];
let accepted: boolean | void;
try {
accepted =
options.accept && !options.accept(ids, intent.to)
? false
: await options.onMoveIntent(intent);
} catch (error) {
if (!destroyed && generation === endedGeneration) {
options.announce?.({ type: "cancelled", ids, location: intent.to });
options.onError?.(error, intent);
}
return;
}
if (destroyed || generation !== endedGeneration) return;
options.announce?.({
type: accepted === false ? "cancelled" : "dropped",
ids: activeIds,
ids,
location: intent.to,
});
activeIds = [];
});
return {
registerSortable(registration) {
if (destroyed) throw new Error("Cannot register a sortable after manager destruction.");
if (entries.has(registration.id))
throw new Error(`Duplicate sortable id ${String(registration.id)}.`);
const sortable = new Sortable(
@@ -95,11 +118,17 @@ export function createDndManager(options: DndManagerOptions): DndManager {
current: registration,
});
return () => {
// Cleanup can run after refresh or be called more than once.
if (entries.get(registration.id)?.sortable !== sortable) return;
sortable.destroy();
entries.delete(registration.id);
};
},
destroy() {
if (destroyed) return;
destroyed = true;
generation++;
activeIds = [];
for (const entry of entries.values()) entry.sortable.destroy();
entries.clear();
manager.destroy();