Skip to content

Commit b5d9b4f

Browse files
Emit warnings when attempting to remove nonexistent items.
Rather than fail with error (`Cannot read properties of undefined`), emit an appropriate console warning instead and then return. For #38
1 parent a8a97fe commit b5d9b4f

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- Emit more useful warnings when attempting to remove nonexistent items. Rather
8+
than fail with error (`Cannot read properties of undefined`), emit an
9+
appropriate console warning instead and then return.
10+
511
### Fixed
612

713
- Ensure layouts recomputed before other effects on drag start. Without this,

src/drag-drop-context.tsx

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,42 @@ const DragDropProvider: ParentComponent<DragDropContextProps> = (
184184
type,
185185
id,
186186
transformer
187-
) => setState(type, id, "transformers", transformer.id, transformer);
187+
) => {
188+
const displayType = type.substring(0, type.length - 1);
189+
190+
if (!untrack(() => state[type][id])) {
191+
console.warn(
192+
`Cannot add transformer to nonexistent ${displayType} with id: ${id}`
193+
);
194+
return;
195+
}
196+
197+
setState(type, id, "transformers", transformer.id, transformer);
198+
};
188199

189200
const removeTransformer: DragDropActions["removeTransformer"] = (
190201
type,
191202
id,
192203
transformerId
193-
) => setState(type, id, "transformers", transformerId, undefined!);
204+
) => {
205+
const displayType = type.substring(0, type.length - 1);
206+
207+
if (!untrack(() => state[type][id])) {
208+
console.warn(
209+
`Cannot remove transformer from nonexistent ${displayType} with id: ${id}`
210+
);
211+
return;
212+
}
213+
214+
if (!untrack(() => state[type][id]["transformers"][transformerId])) {
215+
console.warn(
216+
`Cannot remove from ${displayType} with id ${id}, nonexistent transformer with id: ${transformerId}`
217+
);
218+
return;
219+
}
220+
221+
setState(type, id, "transformers", transformerId, undefined!);
222+
};
194223

195224
const addDraggable: DragDropActions["addDraggable"] = ({
196225
id,
@@ -288,6 +317,11 @@ const DragDropProvider: ParentComponent<DragDropContextProps> = (
288317
};
289318

290319
const removeDraggable: DragDropActions["removeDraggable"] = (id) => {
320+
if (!untrack(() => state.draggables[id])) {
321+
console.warn(`Cannot remove nonexistent draggable with id: ${id}`);
322+
return;
323+
}
324+
291325
setState("draggables", id, "_pendingCleanup", true);
292326
queueMicrotask(() => cleanupDraggable(id));
293327
};
@@ -366,6 +400,11 @@ const DragDropProvider: ParentComponent<DragDropContextProps> = (
366400
};
367401

368402
const removeDroppable: DragDropActions["removeDroppable"] = (id) => {
403+
if (!untrack(() => state.droppables[id])) {
404+
console.warn(`Cannot remove nonexistent droppable with id: ${id}`);
405+
return;
406+
}
407+
369408
setState("droppables", id, "_pendingCleanup", true);
370409
queueMicrotask(() => cleanupDroppable(id));
371410
};
@@ -404,6 +443,11 @@ const DragDropProvider: ParentComponent<DragDropContextProps> = (
404443
};
405444

406445
const removeSensor: DragDropActions["removeSensor"] = (id) => {
446+
if (!untrack(() => state.sensors[id])) {
447+
console.warn(`Cannot remove nonexistent sensor with id: ${id}`);
448+
return;
449+
}
450+
407451
const cleanupActive = state.active.sensorId === id;
408452
batch(() => {
409453
if (cleanupActive) {

0 commit comments

Comments
 (0)