@pyreon/dnd 0.18.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5386,7 +5386,7 @@ var drawChart = (function (exports) {
5386
5386
  </script>
5387
5387
  <script>
5388
5388
  /*<!--*/
5389
- const data = {"version":2,"tree":{"name":"root","children":[{"name":"index.js","children":[{"name":"src","children":[{"uid":"0889448f-1","name":"use-drag-monitor.ts"},{"uid":"0889448f-3","name":"use-draggable.ts"},{"uid":"0889448f-5","name":"use-droppable.ts"},{"uid":"0889448f-7","name":"use-file-drop.ts"},{"uid":"0889448f-9","name":"use-sortable.ts"},{"uid":"0889448f-11","name":"index.ts"}]}]}],"isRoot":true},"nodeParts":{"0889448f-1":{"renderedLength":1307,"gzipLength":625,"brotliLength":0,"metaUid":"0889448f-0"},"0889448f-3":{"renderedLength":1312,"gzipLength":600,"brotliLength":0,"metaUid":"0889448f-2"},"0889448f-5":{"renderedLength":1361,"gzipLength":597,"brotliLength":0,"metaUid":"0889448f-4"},"0889448f-7":{"renderedLength":2300,"gzipLength":939,"brotliLength":0,"metaUid":"0889448f-6"},"0889448f-9":{"renderedLength":5920,"gzipLength":2016,"brotliLength":0,"metaUid":"0889448f-8"},"0889448f-11":{"renderedLength":0,"gzipLength":0,"brotliLength":0,"metaUid":"0889448f-10"}},"nodeMetas":{"0889448f-0":{"id":"/src/use-drag-monitor.ts","moduleParts":{"index.js":"0889448f-1"},"imported":[{"uid":"0889448f-12"},{"uid":"0889448f-13"}],"importedBy":[{"uid":"0889448f-10"}]},"0889448f-2":{"id":"/src/use-draggable.ts","moduleParts":{"index.js":"0889448f-3"},"imported":[{"uid":"0889448f-12"},{"uid":"0889448f-13"}],"importedBy":[{"uid":"0889448f-10"}]},"0889448f-4":{"id":"/src/use-droppable.ts","moduleParts":{"index.js":"0889448f-5"},"imported":[{"uid":"0889448f-12"},{"uid":"0889448f-13"}],"importedBy":[{"uid":"0889448f-10"}]},"0889448f-6":{"id":"/src/use-file-drop.ts","moduleParts":{"index.js":"0889448f-7"},"imported":[{"uid":"0889448f-14"},{"uid":"0889448f-15"},{"uid":"0889448f-13"}],"importedBy":[{"uid":"0889448f-10"}]},"0889448f-8":{"id":"/src/use-sortable.ts","moduleParts":{"index.js":"0889448f-9"},"imported":[{"uid":"0889448f-16"},{"uid":"0889448f-12"},{"uid":"0889448f-17"},{"uid":"0889448f-18"},{"uid":"0889448f-13"}],"importedBy":[{"uid":"0889448f-10"}]},"0889448f-10":{"id":"/src/index.ts","moduleParts":{"index.js":"0889448f-11"},"imported":[{"uid":"0889448f-0"},{"uid":"0889448f-2"},{"uid":"0889448f-4"},{"uid":"0889448f-6"},{"uid":"0889448f-8"}],"importedBy":[],"isEntry":true},"0889448f-12":{"id":"@atlaskit/pragmatic-drag-and-drop/element/adapter","moduleParts":{},"imported":[],"importedBy":[{"uid":"0889448f-0"},{"uid":"0889448f-2"},{"uid":"0889448f-4"},{"uid":"0889448f-8"}]},"0889448f-13":{"id":"@pyreon/reactivity","moduleParts":{},"imported":[],"importedBy":[{"uid":"0889448f-0"},{"uid":"0889448f-2"},{"uid":"0889448f-4"},{"uid":"0889448f-6"},{"uid":"0889448f-8"}]},"0889448f-14":{"id":"@atlaskit/pragmatic-drag-and-drop/external/adapter","moduleParts":{},"imported":[],"importedBy":[{"uid":"0889448f-6"}]},"0889448f-15":{"id":"@atlaskit/pragmatic-drag-and-drop/external/file","moduleParts":{},"imported":[],"importedBy":[{"uid":"0889448f-6"}]},"0889448f-16":{"id":"@atlaskit/pragmatic-drag-and-drop/combine","moduleParts":{},"imported":[],"importedBy":[{"uid":"0889448f-8"}]},"0889448f-17":{"id":"@atlaskit/pragmatic-drag-and-drop-auto-scroll/element","moduleParts":{},"imported":[],"importedBy":[{"uid":"0889448f-8"}]},"0889448f-18":{"id":"@atlaskit/pragmatic-drag-and-drop-hitbox/closest-edge","moduleParts":{},"imported":[],"importedBy":[{"uid":"0889448f-8"}]}},"env":{"rollup":"4.23.0"},"options":{"gzip":true,"brotli":false,"sourcemap":false}};
5389
+ const data = {"version":2,"tree":{"name":"root","children":[{"name":"index.js","children":[{"name":"src","children":[{"uid":"35c84808-1","name":"use-drag-monitor.ts"},{"uid":"35c84808-3","name":"use-draggable.ts"},{"uid":"35c84808-5","name":"use-droppable.ts"},{"uid":"35c84808-7","name":"use-file-drop.ts"},{"uid":"35c84808-9","name":"use-sortable.ts"},{"uid":"35c84808-11","name":"index.ts"}]}]}],"isRoot":true},"nodeParts":{"35c84808-1":{"renderedLength":1307,"gzipLength":625,"brotliLength":0,"metaUid":"35c84808-0"},"35c84808-3":{"renderedLength":1312,"gzipLength":600,"brotliLength":0,"metaUid":"35c84808-2"},"35c84808-5":{"renderedLength":1361,"gzipLength":597,"brotliLength":0,"metaUid":"35c84808-4"},"35c84808-7":{"renderedLength":2300,"gzipLength":939,"brotliLength":0,"metaUid":"35c84808-6"},"35c84808-9":{"renderedLength":6160,"gzipLength":2095,"brotliLength":0,"metaUid":"35c84808-8"},"35c84808-11":{"renderedLength":0,"gzipLength":0,"brotliLength":0,"metaUid":"35c84808-10"}},"nodeMetas":{"35c84808-0":{"id":"/src/use-drag-monitor.ts","moduleParts":{"index.js":"35c84808-1"},"imported":[{"uid":"35c84808-12"},{"uid":"35c84808-13"}],"importedBy":[{"uid":"35c84808-10"}]},"35c84808-2":{"id":"/src/use-draggable.ts","moduleParts":{"index.js":"35c84808-3"},"imported":[{"uid":"35c84808-12"},{"uid":"35c84808-13"}],"importedBy":[{"uid":"35c84808-10"}]},"35c84808-4":{"id":"/src/use-droppable.ts","moduleParts":{"index.js":"35c84808-5"},"imported":[{"uid":"35c84808-12"},{"uid":"35c84808-13"}],"importedBy":[{"uid":"35c84808-10"}]},"35c84808-6":{"id":"/src/use-file-drop.ts","moduleParts":{"index.js":"35c84808-7"},"imported":[{"uid":"35c84808-14"},{"uid":"35c84808-15"},{"uid":"35c84808-13"}],"importedBy":[{"uid":"35c84808-10"}]},"35c84808-8":{"id":"/src/use-sortable.ts","moduleParts":{"index.js":"35c84808-9"},"imported":[{"uid":"35c84808-16"},{"uid":"35c84808-12"},{"uid":"35c84808-17"},{"uid":"35c84808-18"},{"uid":"35c84808-13"}],"importedBy":[{"uid":"35c84808-10"}]},"35c84808-10":{"id":"/src/index.ts","moduleParts":{"index.js":"35c84808-11"},"imported":[{"uid":"35c84808-0"},{"uid":"35c84808-2"},{"uid":"35c84808-4"},{"uid":"35c84808-6"},{"uid":"35c84808-8"}],"importedBy":[],"isEntry":true},"35c84808-12":{"id":"@atlaskit/pragmatic-drag-and-drop/element/adapter","moduleParts":{},"imported":[],"importedBy":[{"uid":"35c84808-0"},{"uid":"35c84808-2"},{"uid":"35c84808-4"},{"uid":"35c84808-8"}]},"35c84808-13":{"id":"@pyreon/reactivity","moduleParts":{},"imported":[],"importedBy":[{"uid":"35c84808-0"},{"uid":"35c84808-2"},{"uid":"35c84808-4"},{"uid":"35c84808-6"},{"uid":"35c84808-8"}]},"35c84808-14":{"id":"@atlaskit/pragmatic-drag-and-drop/external/adapter","moduleParts":{},"imported":[],"importedBy":[{"uid":"35c84808-6"}]},"35c84808-15":{"id":"@atlaskit/pragmatic-drag-and-drop/external/file","moduleParts":{},"imported":[],"importedBy":[{"uid":"35c84808-6"}]},"35c84808-16":{"id":"@atlaskit/pragmatic-drag-and-drop/combine","moduleParts":{},"imported":[],"importedBy":[{"uid":"35c84808-8"}]},"35c84808-17":{"id":"@atlaskit/pragmatic-drag-and-drop-auto-scroll/element","moduleParts":{},"imported":[],"importedBy":[{"uid":"35c84808-8"}]},"35c84808-18":{"id":"@atlaskit/pragmatic-drag-and-drop-hitbox/closest-edge","moduleParts":{},"imported":[],"importedBy":[{"uid":"35c84808-8"}]}},"env":{"rollup":"4.23.0"},"options":{"gzip":true,"brotli":false,"sourcemap":false}};
5390
5390
 
5391
5391
  const run = () => {
5392
5392
  const width = window.innerWidth;
package/lib/index.js CHANGED
@@ -311,6 +311,7 @@ function useSortable(options) {
311
311
  const overEdge = signal(null);
312
312
  const axis = options.axis ?? "vertical";
313
313
  const cleanups = [];
314
+ const itemCleanups = /* @__PURE__ */ new Map();
314
315
  /** Perform the reorder based on current active/over/edge state. */
315
316
  function performReorder() {
316
317
  const dragId = activeId.peek();
@@ -379,6 +380,11 @@ function useSortable(options) {
379
380
  }
380
381
  function itemRef(key) {
381
382
  return (el) => {
383
+ const prev = itemCleanups.get(key);
384
+ if (prev) {
385
+ prev();
386
+ itemCleanups.delete(key);
387
+ }
382
388
  if (!el) return;
383
389
  el.dataset.pyreonSortKey = String(key);
384
390
  if (!el.hasAttribute("tabindex")) el.setAttribute("tabindex", "0");
@@ -424,12 +430,14 @@ function useSortable(options) {
424
430
  }
425
431
  }
426
432
  }));
427
- cleanups.push(cleanup);
433
+ itemCleanups.set(key, cleanup);
428
434
  };
429
435
  }
430
436
  onCleanup(() => {
431
437
  for (const cleanup of cleanups) cleanup();
432
438
  cleanups.length = 0;
439
+ for (const cleanup of itemCleanups.values()) cleanup();
440
+ itemCleanups.clear();
433
441
  activeId.set(null);
434
442
  overId.set(null);
435
443
  overEdge.set(null);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pyreon/dnd",
3
- "version": "0.18.0",
3
+ "version": "0.19.0",
4
4
  "description": "Signal-driven drag and drop for Pyreon — wraps @atlaskit/pragmatic-drag-and-drop",
5
5
  "bugs": {
6
6
  "url": "https://github.com/pyreon/pyreon/issues"
@@ -42,12 +42,12 @@
42
42
  "@atlaskit/pragmatic-drag-and-drop": "^1.7.10",
43
43
  "@atlaskit/pragmatic-drag-and-drop-auto-scroll": "^2.0.0",
44
44
  "@atlaskit/pragmatic-drag-and-drop-hitbox": "^1.0.0",
45
- "@pyreon/core": "^0.18.0",
46
- "@pyreon/reactivity": "^0.18.0"
45
+ "@pyreon/core": "^0.19.0",
46
+ "@pyreon/reactivity": "^0.19.0"
47
47
  },
48
48
  "devDependencies": {
49
- "@pyreon/test-utils": "^0.13.5",
50
- "@pyreon/typescript": "^0.18.0",
49
+ "@pyreon/test-utils": "^0.13.6",
50
+ "@pyreon/typescript": "^0.19.0",
51
51
  "@vitest/browser-playwright": "^4.1.4"
52
52
  }
53
53
  }
@@ -162,9 +162,9 @@ describe('useSortable', () => {
162
162
  // Regression: Pyreon's runtime invokes ref callbacks with `el | null`
163
163
  // — `null` on unmount. Previously the exported types were
164
164
  // `(el: HTMLElement) => void` (non-null), forcing every consumer to
165
- // wrap with adapter callbacks. The hook now accepts `null` and
166
- // no-ops — pdnd's per-element cleanups are registered via
167
- // `onCleanup`, so the unmount path is already covered.
165
+ // wrap with adapter callbacks. The hook accepts `null`; on `null`
166
+ // the per-key pdnd registration is disposed eagerly (F3 fix) rather
167
+ // than deferred to component `onCleanup`.
168
168
  const { useSortable } = await import('../use-sortable')
169
169
  const items = signal([{ id: '1' }, { id: '2' }])
170
170
 
@@ -201,6 +201,10 @@ describe('useSortable', () => {
201
201
  expect(el.getAttribute('tabindex')).toBe('-1')
202
202
  })
203
203
 
204
+ // F3 (per-item registration leak) is regression-tested in the isolated
205
+ // `use-sortable-leak.test.ts` (pdnd mocked there — the real lib uses
206
+ // document-level listeners with no robust per-element DOM observable).
207
+
204
208
  it('supports horizontal axis', async () => {
205
209
  const { useSortable } = await import('../use-sortable')
206
210
  const items = signal([{ id: '1' }, { id: '2' }])
@@ -0,0 +1,110 @@
1
+ import { effect, signal } from '@pyreon/reactivity'
2
+ import { describe, expect, it, vi } from 'vitest'
3
+
4
+ // F3 — useSortable per-item registration leak.
5
+ //
6
+ // pdnd is mocked here so the test isolates ONE thing: that useSortable
7
+ // disposes each item's `combine(...)` cleanup on unmount (ref called with
8
+ // `null`) and on re-register of the same key — instead of pushing every
9
+ // registration onto a shared array drained only when the WHOLE sortable
10
+ // unmounts. The real pdnd uses document-level listeners + a registry, so
11
+ // there is no robust per-element DOM observable; mocking `combine` to
12
+ // return a tracked spy is the deterministic, version-agnostic proxy.
13
+ //
14
+ // Scoped to its own file: vi.mock is module-global, and the rest of the
15
+ // dnd suite needs the real pdnd.
16
+
17
+ const combineCleanups: Array<ReturnType<typeof vi.fn>> = []
18
+
19
+ vi.mock('@atlaskit/pragmatic-drag-and-drop/combine', () => ({
20
+ combine: (..._fns: Array<() => void>) => {
21
+ const cleanup = vi.fn()
22
+ combineCleanups.push(cleanup)
23
+ return cleanup
24
+ },
25
+ }))
26
+ vi.mock('@atlaskit/pragmatic-drag-and-drop/element/adapter', () => ({
27
+ draggable: () => () => {},
28
+ dropTargetForElements: () => () => {},
29
+ }))
30
+ vi.mock('@atlaskit/pragmatic-drag-and-drop-auto-scroll/element', () => ({
31
+ autoScrollForElements: () => () => {},
32
+ }))
33
+ vi.mock('@atlaskit/pragmatic-drag-and-drop-hitbox/closest-edge', () => ({
34
+ attachClosestEdge: (data: unknown) => data,
35
+ extractClosestEdge: () => null,
36
+ }))
37
+
38
+ describe('useSortable — per-item registration disposal (F3 leak)', () => {
39
+ it('disposes the item cleanup on unmount and on re-register; no accumulation under churn', async () => {
40
+ const { useSortable } = await import('../use-sortable')
41
+ const items = signal([{ id: '1' }])
42
+ const { itemRef } = useSortable({ items, by: (i) => i.id, onReorder: () => {} })
43
+
44
+ combineCleanups.length = 0
45
+
46
+ // Register one item.
47
+ const ref = itemRef('1')
48
+ ref(document.createElement('div'))
49
+ expect(combineCleanups).toHaveLength(1)
50
+ expect(combineCleanups[0]).not.toHaveBeenCalled()
51
+
52
+ // Unmount (ref(null)) MUST run that registration's cleanup. Pre-fix
53
+ // the null branch was a no-op → cleanup never ran → leak.
54
+ ref(null)
55
+ expect(combineCleanups[0]).toHaveBeenCalledTimes(1)
56
+
57
+ // Re-register the SAME key with a new element: a fresh registration,
58
+ // and the prior one stays disposed (not double-registered).
59
+ itemRef('1')(document.createElement('div'))
60
+ expect(combineCleanups).toHaveLength(2)
61
+ expect(combineCleanups[1]).not.toHaveBeenCalled()
62
+ ;(itemRef('1') as (el: HTMLElement | null) => void)(null)
63
+ expect(combineCleanups[1]).toHaveBeenCalledTimes(1)
64
+
65
+ // Churn: 200 register/unmount cycles for the same key. Every
66
+ // registration's cleanup must have been called exactly once — the
67
+ // live set never accumulates dead registrations.
68
+ const base = combineCleanups.length
69
+ for (let i = 0; i < 200; i++) {
70
+ const r = itemRef(`k${i}`)
71
+ r(document.createElement('div'))
72
+ r(null)
73
+ }
74
+ const churned = combineCleanups.slice(base)
75
+ expect(churned).toHaveLength(200)
76
+ for (const c of churned) expect(c).toHaveBeenCalledTimes(1)
77
+ })
78
+
79
+ it('component onCleanup drains BOTH container and per-item registrations (F3)', async () => {
80
+ // Covers the `onCleanup` block: when the sortable's owner is
81
+ // disposed, the shared container `cleanups[]` AND the per-key
82
+ // `itemCleanups` map are both drained (F3 added the itemCleanups
83
+ // drain). Runs inside a disposable `effect` so useSortable's
84
+ // `onCleanup` actually registers + fires.
85
+ const { useSortable } = await import('../use-sortable')
86
+ combineCleanups.length = 0
87
+
88
+ let itemRefFn: ((el: HTMLElement | null) => void) | null = null
89
+ let containerRefFn: ((el: HTMLElement | null) => void) | null = null
90
+ const owner = effect(() => {
91
+ const items = signal([{ id: '1' }])
92
+ const s = useSortable({ items, by: (i) => i.id, onReorder: () => {} })
93
+ containerRefFn = s.containerRef
94
+ itemRefFn = s.itemRef('1')
95
+ })
96
+
97
+ // Populate the container cleanups (dropTarget + autoScroll + keydown)
98
+ // and one per-item registration so BOTH drain paths execute.
99
+ containerRefFn!(document.createElement('div'))
100
+ itemRefFn!(document.createElement('div'))
101
+ const itemCleanup = combineCleanups[combineCleanups.length - 1]!
102
+ expect(itemCleanup).not.toHaveBeenCalled()
103
+
104
+ // Dispose the owner → useSortable's onCleanup runs.
105
+ owner.dispose()
106
+
107
+ // The per-item registration was disposed via the itemCleanups drain.
108
+ expect(itemCleanup).toHaveBeenCalledTimes(1)
109
+ })
110
+ })
@@ -71,7 +71,12 @@ export function useSortable<T>(options: UseSortableOptions<T>): UseSortableResul
71
71
  const overEdge = signal<DropEdge | null>(null)
72
72
  const axis = options.axis ?? 'vertical'
73
73
 
74
+ // Shared (container-level) pdnd cleanups — drained once on unmount.
74
75
  const cleanups: (() => void)[] = []
76
+ // Per-item pdnd cleanups, keyed by sort key. Disposed individually on
77
+ // item unmount / re-register so a churning list doesn't accumulate
78
+ // dead registrations for the sortable's whole lifetime.
79
+ const itemCleanups = new Map<string | number, () => void>()
75
80
 
76
81
  /** Perform the reorder based on current active/over/edge state. */
77
82
  function performReorder() {
@@ -179,7 +184,21 @@ export function useSortable<T>(options: UseSortableOptions<T>): UseSortableResul
179
184
 
180
185
  function itemRef(key: string | number): (el: HTMLElement | null) => void {
181
186
  return (el: HTMLElement | null) => {
182
- // Pyreon ref-on-unmount: see containerRef comment.
187
+ // Per-key disposal. The ref fires with the element on mount and
188
+ // with `null` on unmount. The old code pushed every registration
189
+ // onto the shared `cleanups[]` and made the null branch a pure
190
+ // no-op — so for a `<For>`-rendered sortable (the documented usage:
191
+ // todo list / kanban) every removed item's pdnd `draggable` /
192
+ // `dropTargetForElements` registration (and its pointer/drag
193
+ // listeners on the now-detached node) leaked until the WHOLE
194
+ // sortable unmounted. Dispose the prior registration for this key
195
+ // on BOTH unmount (el === null) and re-registration (same key,
196
+ // new element) so the live set tracks live items, not all-ever.
197
+ const prev = itemCleanups.get(key)
198
+ if (prev) {
199
+ prev()
200
+ itemCleanups.delete(key)
201
+ }
183
202
  if (!el) return
184
203
  el.dataset.pyreonSortKey = String(key)
185
204
  if (!el.hasAttribute('tabindex')) el.setAttribute('tabindex', '0')
@@ -228,13 +247,15 @@ export function useSortable<T>(options: UseSortableOptions<T>): UseSortableResul
228
247
  }),
229
248
  )
230
249
 
231
- cleanups.push(cleanup)
250
+ itemCleanups.set(key, cleanup)
232
251
  }
233
252
  }
234
253
 
235
254
  onCleanup(() => {
236
255
  for (const cleanup of cleanups) cleanup()
237
256
  cleanups.length = 0
257
+ for (const cleanup of itemCleanups.values()) cleanup()
258
+ itemCleanups.clear()
238
259
  activeId.set(null)
239
260
  overId.set(null)
240
261
  overEdge.set(null)