@wardic/dom 1.0.1 → 1.1.1

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.
Files changed (289) hide show
  1. package/dist/Appear.d.ts +3 -2
  2. package/dist/Appear.d.ts.map +1 -1
  3. package/dist/Appear.js +4 -3
  4. package/dist/Appear.js.map +1 -1
  5. package/dist/DomProjectionSystem.js +3 -3
  6. package/dist/DomProjectionSystem.js.map +1 -1
  7. package/dist/FlushGate.d.ts +6 -6
  8. package/dist/FlushGate.d.ts.map +1 -1
  9. package/dist/FlushGate.js +11 -8
  10. package/dist/FlushGate.js.map +1 -1
  11. package/dist/ReorderController.js +3 -3
  12. package/dist/ReorderController.js.map +1 -1
  13. package/dist/Report.d.ts +1 -1
  14. package/dist/Report.d.ts.map +1 -1
  15. package/dist/Report.js +3 -2
  16. package/dist/Report.js.map +1 -1
  17. package/dist/Style.d.ts +2 -1
  18. package/dist/Style.d.ts.map +1 -1
  19. package/dist/Style.js +3 -1
  20. package/dist/Style.js.map +1 -1
  21. package/dist/TimelineValue.d.ts +18 -0
  22. package/dist/TimelineValue.d.ts.map +1 -0
  23. package/dist/TimelineValue.js +20 -0
  24. package/dist/TimelineValue.js.map +1 -0
  25. package/dist/index.d.ts +13 -5
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +10 -1
  28. package/dist/index.js.map +1 -1
  29. package/dist/internal/appear.d.ts +10 -4
  30. package/dist/internal/appear.d.ts.map +1 -1
  31. package/dist/internal/appear.js +18 -8
  32. package/dist/internal/appear.js.map +1 -1
  33. package/dist/internal/batch-handoff.d.ts +25 -0
  34. package/dist/internal/batch-handoff.d.ts.map +1 -0
  35. package/dist/internal/batch-handoff.js +64 -0
  36. package/dist/internal/batch-handoff.js.map +1 -0
  37. package/dist/internal/drag-defaults.js +4 -1
  38. package/dist/internal/drag-defaults.js.map +1 -1
  39. package/dist/internal/gate-environment.d.ts +4 -0
  40. package/dist/internal/gate-environment.d.ts.map +1 -0
  41. package/dist/internal/gate-environment.js +21 -0
  42. package/dist/internal/gate-environment.js.map +1 -0
  43. package/dist/internal/projection.d.ts.map +1 -1
  44. package/dist/internal/projection.js +22 -21
  45. package/dist/internal/projection.js.map +1 -1
  46. package/dist/internal/renderer.d.ts +13 -0
  47. package/dist/internal/renderer.d.ts.map +1 -1
  48. package/dist/internal/renderer.js +21 -0
  49. package/dist/internal/renderer.js.map +1 -1
  50. package/dist/internal/reorder.d.ts +2 -2
  51. package/dist/internal/reorder.d.ts.map +1 -1
  52. package/dist/internal/reorder.js +2 -2
  53. package/dist/internal/reorder.js.map +1 -1
  54. package/dist/internal/scroll.d.ts +17 -0
  55. package/dist/internal/scroll.d.ts.map +1 -1
  56. package/dist/internal/scroll.js +23 -15
  57. package/dist/internal/scroll.js.map +1 -1
  58. package/dist/internal/starting-style.d.ts +0 -83
  59. package/dist/internal/starting-style.d.ts.map +1 -1
  60. package/dist/internal/starting-style.js +16 -143
  61. package/dist/internal/starting-style.js.map +1 -1
  62. package/dist/internal/timeline.d.ts +56 -0
  63. package/dist/internal/timeline.d.ts.map +1 -0
  64. package/dist/internal/timeline.js +389 -0
  65. package/dist/internal/timeline.js.map +1 -0
  66. package/dist/unstable/AppearAnimations.d.ts +191 -0
  67. package/dist/unstable/AppearAnimations.d.ts.map +1 -0
  68. package/dist/unstable/AppearAnimations.js +561 -0
  69. package/dist/unstable/AppearAnimations.js.map +1 -0
  70. package/dist/unstable/BatchMembers.d.ts +513 -0
  71. package/dist/unstable/BatchMembers.d.ts.map +1 -0
  72. package/dist/unstable/BatchMembers.js +2490 -0
  73. package/dist/unstable/BatchMembers.js.map +1 -0
  74. package/dist/unstable/DomValues.d.ts +25 -0
  75. package/dist/unstable/DomValues.d.ts.map +1 -0
  76. package/dist/unstable/DomValues.js +45 -0
  77. package/dist/unstable/DomValues.js.map +1 -0
  78. package/dist/unstable/DragConstraints.d.ts +48 -0
  79. package/dist/unstable/DragConstraints.d.ts.map +1 -0
  80. package/dist/unstable/DragConstraints.js +49 -0
  81. package/dist/unstable/DragConstraints.js.map +1 -0
  82. package/dist/unstable/DragControls.d.ts +50 -0
  83. package/dist/unstable/DragControls.d.ts.map +1 -0
  84. package/dist/unstable/DragControls.js +55 -0
  85. package/dist/unstable/DragControls.js.map +1 -0
  86. package/dist/unstable/DragDefaults.d.ts +34 -0
  87. package/dist/unstable/DragDefaults.d.ts.map +1 -0
  88. package/dist/unstable/DragDefaults.js +47 -0
  89. package/dist/unstable/DragDefaults.js.map +1 -0
  90. package/dist/unstable/DragTrack.d.ts +236 -0
  91. package/dist/unstable/DragTrack.d.ts.map +1 -0
  92. package/dist/unstable/DragTrack.js +646 -0
  93. package/dist/unstable/DragTrack.js.map +1 -0
  94. package/dist/unstable/DragTransition.d.ts +22 -0
  95. package/dist/unstable/DragTransition.d.ts.map +1 -0
  96. package/dist/unstable/DragTransition.js +31 -0
  97. package/dist/unstable/DragTransition.js.map +1 -0
  98. package/dist/unstable/ElementSetup.d.ts +144 -0
  99. package/dist/unstable/ElementSetup.d.ts.map +1 -0
  100. package/dist/unstable/ElementSetup.js +297 -0
  101. package/dist/unstable/ElementSetup.js.map +1 -0
  102. package/dist/unstable/Feature.d.ts +221 -0
  103. package/dist/unstable/Feature.d.ts.map +1 -0
  104. package/dist/unstable/Feature.js +281 -0
  105. package/dist/unstable/Feature.js.map +1 -0
  106. package/dist/unstable/GestureFeatures.d.ts +30 -0
  107. package/dist/unstable/GestureFeatures.d.ts.map +1 -0
  108. package/dist/unstable/GestureFeatures.js +32 -0
  109. package/dist/unstable/GestureFeatures.js.map +1 -0
  110. package/dist/unstable/GestureInfo.d.ts +61 -0
  111. package/dist/unstable/GestureInfo.d.ts.map +1 -0
  112. package/dist/unstable/GestureInfo.js +23 -0
  113. package/dist/unstable/GestureInfo.js.map +1 -0
  114. package/dist/unstable/Imperative.d.ts +214 -0
  115. package/dist/unstable/Imperative.d.ts.map +1 -0
  116. package/dist/unstable/Imperative.js +768 -0
  117. package/dist/unstable/Imperative.js.map +1 -0
  118. package/dist/unstable/Instant.d.ts +46 -0
  119. package/dist/unstable/Instant.d.ts.map +1 -0
  120. package/dist/unstable/Instant.js +49 -0
  121. package/dist/unstable/Instant.js.map +1 -0
  122. package/dist/unstable/LayoutGroup.d.ts +14 -0
  123. package/dist/unstable/LayoutGroup.d.ts.map +1 -0
  124. package/dist/unstable/LayoutGroup.js +31 -0
  125. package/dist/unstable/LayoutGroup.js.map +1 -0
  126. package/dist/unstable/LayoutMember.d.ts +127 -0
  127. package/dist/unstable/LayoutMember.d.ts.map +1 -0
  128. package/dist/unstable/LayoutMember.js +176 -0
  129. package/dist/unstable/LayoutMember.js.map +1 -0
  130. package/dist/unstable/LiveStyle.d.ts +171 -0
  131. package/dist/unstable/LiveStyle.d.ts.map +1 -0
  132. package/dist/unstable/LiveStyle.js +329 -0
  133. package/dist/unstable/LiveStyle.js.map +1 -0
  134. package/dist/unstable/MemberHold.d.ts +3 -0
  135. package/dist/unstable/MemberHold.d.ts.map +1 -0
  136. package/dist/unstable/MemberHold.js +223 -0
  137. package/dist/unstable/MemberHold.js.map +1 -0
  138. package/dist/unstable/OwnerScope.d.ts +16 -0
  139. package/dist/unstable/OwnerScope.d.ts.map +1 -0
  140. package/dist/unstable/OwnerScope.js +35 -0
  141. package/dist/unstable/OwnerScope.js.map +1 -0
  142. package/dist/unstable/PresenceController.d.ts +128 -0
  143. package/dist/unstable/PresenceController.d.ts.map +1 -0
  144. package/dist/unstable/PresenceController.js +1427 -0
  145. package/dist/unstable/PresenceController.js.map +1 -0
  146. package/dist/unstable/Press.d.ts +26 -0
  147. package/dist/unstable/Press.d.ts.map +1 -0
  148. package/dist/unstable/Press.js +19 -0
  149. package/dist/unstable/Press.js.map +1 -0
  150. package/dist/unstable/ServerInterpolators.d.ts +17 -0
  151. package/dist/unstable/ServerInterpolators.d.ts.map +1 -0
  152. package/dist/unstable/ServerInterpolators.js +20 -0
  153. package/dist/unstable/ServerInterpolators.js.map +1 -0
  154. package/dist/unstable/WardConfig.d.ts +567 -0
  155. package/dist/unstable/WardConfig.d.ts.map +1 -0
  156. package/dist/unstable/WardConfig.js +2 -0
  157. package/dist/unstable/WardConfig.js.map +1 -0
  158. package/dist/unstable/features/All.d.ts +24 -0
  159. package/dist/unstable/features/All.d.ts.map +1 -0
  160. package/dist/unstable/features/All.js +20 -0
  161. package/dist/unstable/features/All.js.map +1 -0
  162. package/dist/unstable/features/Colors.d.ts +12 -0
  163. package/dist/unstable/features/Colors.d.ts.map +1 -0
  164. package/dist/unstable/features/Colors.js +38 -0
  165. package/dist/unstable/features/Colors.js.map +1 -0
  166. package/dist/unstable/features/DomAnimation.d.ts +16 -0
  167. package/dist/unstable/features/DomAnimation.d.ts.map +1 -0
  168. package/dist/unstable/features/DomAnimation.js +19 -0
  169. package/dist/unstable/features/DomAnimation.js.map +1 -0
  170. package/dist/unstable/features/DomMax.d.ts +12 -0
  171. package/dist/unstable/features/DomMax.d.ts.map +1 -0
  172. package/dist/unstable/features/DomMax.js +14 -0
  173. package/dist/unstable/features/DomMax.js.map +1 -0
  174. package/dist/unstable/features/Drag.d.ts +5 -0
  175. package/dist/unstable/features/Drag.d.ts.map +1 -0
  176. package/dist/unstable/features/Drag.js +32 -0
  177. package/dist/unstable/features/Drag.js.map +1 -0
  178. package/dist/unstable/features/Focus.d.ts +5 -0
  179. package/dist/unstable/features/Focus.d.ts.map +1 -0
  180. package/dist/unstable/features/Focus.js +20 -0
  181. package/dist/unstable/features/Focus.js.map +1 -0
  182. package/dist/unstable/features/Hover.d.ts +5 -0
  183. package/dist/unstable/features/Hover.d.ts.map +1 -0
  184. package/dist/unstable/features/Hover.js +20 -0
  185. package/dist/unstable/features/Hover.js.map +1 -0
  186. package/dist/unstable/features/InView.d.ts +5 -0
  187. package/dist/unstable/features/InView.d.ts.map +1 -0
  188. package/dist/unstable/features/InView.js +20 -0
  189. package/dist/unstable/features/InView.js.map +1 -0
  190. package/dist/unstable/features/Input.d.ts +28 -0
  191. package/dist/unstable/features/Input.d.ts.map +1 -0
  192. package/dist/unstable/features/Input.js +31 -0
  193. package/dist/unstable/features/Input.js.map +1 -0
  194. package/dist/unstable/features/Keys.d.ts +95 -0
  195. package/dist/unstable/features/Keys.d.ts.map +1 -0
  196. package/dist/unstable/features/Keys.js +44 -0
  197. package/dist/unstable/features/Keys.js.map +1 -0
  198. package/dist/unstable/features/Layout.d.ts +43 -0
  199. package/dist/unstable/features/Layout.d.ts.map +1 -0
  200. package/dist/unstable/features/Layout.js +44 -0
  201. package/dist/unstable/features/Layout.js.map +1 -0
  202. package/dist/unstable/features/Pan.d.ts +5 -0
  203. package/dist/unstable/features/Pan.d.ts.map +1 -0
  204. package/dist/unstable/features/Pan.js +20 -0
  205. package/dist/unstable/features/Pan.js.map +1 -0
  206. package/dist/unstable/features/Press.d.ts +13 -0
  207. package/dist/unstable/features/Press.d.ts.map +1 -0
  208. package/dist/unstable/features/Press.js +10 -0
  209. package/dist/unstable/features/Press.js.map +1 -0
  210. package/dist/unstable/features/Scroll.d.ts +20 -0
  211. package/dist/unstable/features/Scroll.d.ts.map +1 -0
  212. package/dist/unstable/features/Scroll.js +34 -0
  213. package/dist/unstable/features/Scroll.js.map +1 -0
  214. package/dist/unstable/features/TimelineKeys.d.ts +7 -0
  215. package/dist/unstable/features/TimelineKeys.d.ts.map +1 -0
  216. package/dist/unstable/features/TimelineKeys.js +103 -0
  217. package/dist/unstable/features/TimelineKeys.js.map +1 -0
  218. package/package.json +4 -3
  219. package/src/Appear.ts +4 -2
  220. package/src/DomProjectionSystem.ts +3 -3
  221. package/src/FlushGate.ts +15 -12
  222. package/src/ReorderController.ts +3 -3
  223. package/src/Report.ts +3 -7
  224. package/src/Style.ts +4 -1
  225. package/src/TimelineValue.ts +19 -0
  226. package/src/index.ts +15 -5
  227. package/src/internal/appear.ts +20 -8
  228. package/src/internal/batch-handoff.ts +75 -0
  229. package/src/internal/drag-defaults.ts +4 -4
  230. package/src/internal/gate-environment.ts +21 -0
  231. package/src/internal/projection.ts +22 -21
  232. package/src/internal/renderer.ts +22 -0
  233. package/src/internal/reorder.ts +4 -4
  234. package/src/internal/scroll.ts +32 -15
  235. package/src/internal/starting-style.ts +5 -204
  236. package/src/internal/timeline.ts +480 -0
  237. package/src/unstable/AppearAnimations.ts +837 -0
  238. package/src/unstable/BatchMembers.ts +3085 -0
  239. package/src/unstable/DomValues.ts +53 -0
  240. package/src/unstable/DragConstraints.ts +107 -0
  241. package/src/unstable/DragControls.ts +98 -0
  242. package/src/unstable/DragDefaults.ts +57 -0
  243. package/src/unstable/DragTrack.ts +959 -0
  244. package/src/unstable/DragTransition.ts +46 -0
  245. package/src/unstable/ElementSetup.ts +411 -0
  246. package/src/unstable/Feature.ts +494 -0
  247. package/src/unstable/GestureFeatures.ts +38 -0
  248. package/src/unstable/GestureInfo.ts +85 -0
  249. package/src/unstable/Imperative.ts +1037 -0
  250. package/src/unstable/Instant.ts +93 -0
  251. package/src/unstable/LayoutGroup.ts +36 -0
  252. package/src/unstable/LayoutMember.ts +319 -0
  253. package/src/unstable/LiveStyle.ts +497 -0
  254. package/src/unstable/MemberHold.ts +211 -0
  255. package/src/unstable/OwnerScope.ts +35 -0
  256. package/src/unstable/PresenceController.ts +1747 -0
  257. package/src/unstable/Press.ts +43 -0
  258. package/src/unstable/ServerInterpolators.ts +39 -0
  259. package/src/unstable/WardConfig.ts +704 -0
  260. package/src/unstable/features/All.ts +39 -0
  261. package/src/unstable/features/Colors.ts +40 -0
  262. package/src/unstable/features/DomAnimation.ts +19 -0
  263. package/src/unstable/features/DomMax.ts +14 -0
  264. package/src/unstable/features/Drag.ts +34 -0
  265. package/src/unstable/features/Focus.ts +21 -0
  266. package/src/unstable/features/Hover.ts +21 -0
  267. package/src/unstable/features/InView.ts +21 -0
  268. package/src/unstable/features/Input.ts +37 -0
  269. package/src/unstable/features/Keys.ts +124 -0
  270. package/src/unstable/features/Layout.ts +75 -0
  271. package/src/unstable/features/Pan.ts +21 -0
  272. package/src/unstable/features/Press.ts +21 -0
  273. package/src/unstable/features/Scroll.ts +35 -0
  274. package/src/unstable/features/TimelineKeys.ts +115 -0
  275. package/dist/internal/gate.d.ts +0 -188
  276. package/dist/internal/gate.d.ts.map +0 -1
  277. package/dist/internal/gate.js +0 -458
  278. package/dist/internal/gate.js.map +0 -1
  279. package/dist/internal/report.d.ts +0 -30
  280. package/dist/internal/report.d.ts.map +0 -1
  281. package/dist/internal/report.js +0 -60
  282. package/dist/internal/report.js.map +0 -1
  283. package/dist/internal/runner.d.ts +0 -21
  284. package/dist/internal/runner.d.ts.map +0 -1
  285. package/dist/internal/runner.js +0 -48
  286. package/dist/internal/runner.js.map +0 -1
  287. package/src/internal/gate.ts +0 -598
  288. package/src/internal/report.ts +0 -69
  289. package/src/internal/runner.ts +0 -64
@@ -0,0 +1,959 @@
1
+ /**
2
+ * The binding-orchestrated drag track (ADR-0004 response half), for every DOM binding: each binding
3
+ * runs it through `driveDragTrack`, handing it a `watch` for its element's watch Scope, a `fork` for
4
+ * its responses and a `setDragging` for its `dragging` state.
5
+ *
6
+ * DESIGN (locked via grilling, subtask 22c): on a drag-enabled element `x`/`y`
7
+ * are DIRECT-SET by the pointer (1:1, NOT sprung) while the spring spine keeps
8
+ * driving every OTHER key + `whileDrag`. Ownership of `x`/`y` is DYNAMIC: the
9
+ * drag owns them while held AND during the release tail; once the release
10
+ * settles, ownership returns to the spring (so a later `animate.x`/`y` or a
11
+ * layout/Reorder FLIP can take over from the rested position). A Retarget that
12
+ * changes `x` or `y` during the release takes them over sooner (#340, ADR-0016
13
+ * *Amendment (#340)*), as a newer animation of Motion's value does: the release
14
+ * stops where it is, tells no end, and the spring goes on from there, at the
15
+ * release's velocity ({@link DragTrack.takeRelease}).
16
+ *
17
+ * This module is the RESPONSE half (binding orchestration). Core ships the
18
+ * RECOGNITION (`drag()`, kept to its axis and holding its **Drag claim**), the
19
+ * constraint (`Drag.constrain`) and the inertia PRIMITIVE (`inertia`, `inertiaVec`); the
20
+ * constraints on the element's x/y, the release driver, the gate writes, and the
21
+ * snapshot sync are response concerns and live here (ADR-0004, ADR-0016 *Amendment (#140)*).
22
+ *
23
+ * STRUCTURE (ADR-0032, ADR-0055 *Amendment (#379)*): input is handled inside its event, by two
24
+ * watches in the element's watch Scope, with no fiber of the track's own at rest:
25
+ * - the drag's samples (`DragRecognizer.watchDrag`): a gesture's bookkeeping, then the direct-set,
26
+ * or at the "end" the release;
27
+ * - bare pointer-downs from the Primary pointer ("grabs", `InputFeed.watch`), which stop a
28
+ * running release, so a re-grab mid-release stops the tail where it is, and the ends of its
29
+ * presses, which let a projection node's release go on after a press that didn't drag.
30
+ * Each response is an Effect, run by `fork` on the provider runtime: its fiber starts at once and
31
+ * runs to its end inside the event (stopping a release interrupts a parked fiber in the caller's
32
+ * stack), and a release it starts, in a `FiberHandle`, is forked from it, keeping the runtime's
33
+ * scheduler, so it is on the Frame loop before its task ends. The next sample or grab interrupts it,
34
+ * which removes it from the loop at once. A response that fails is reported, and the next event's
35
+ * runs (ADR-0055 *Amendment (#379)*, Q4).
36
+ *
37
+ * **Drag controls** (#431, Motion's `useDragControls`) given to the element start its drag from a
38
+ * pointer pressed elsewhere: the track hands it to the element's pointer feed
39
+ * (`InputSource.adoptPointer`), whose adopted down the recognizer starts from as from a press on the
40
+ * element, and its grab stops a running release. `snapToCursor` moves the element's centre under the
41
+ * pointer there and then; `distanceThreshold` is that gesture's threshold. Their `stop` ends the drag
42
+ * as a release would; `cancel` ends it where it is, with no release and no `onDragEnd`. With
43
+ * `listen: false` (Motion's `dragListener={false}`) the element's own presses neither drag it nor stop
44
+ * its release. The track subscribes to the controls while its watch Scope is open, so an element an
45
+ * `<AnimatePresence>` is taking out (disposed) starts none, as Motion's doesn't.
46
+ *
47
+ * The samples subscribe before the grabs, so one event's samples reach the track
48
+ * before its grab: a Stale gesture's "end" (ADR-0019, rule B) starts its release and
49
+ * the grab of the same down stops it before its first frame, on every Environment.
50
+ * The track's state is a plain `MutableRef`: each response runs to completion, and a
51
+ * release is stopped before the next response changes the state.
52
+ *
53
+ * Unstable, for Wardic's own packages: public, with no semver promise.
54
+ *
55
+ * @module
56
+ */
57
+ // Unstable: ADR-0071, decision 5. #672: solid's `drag-track.ts`, moved to dom for every DOM binding's drag.
58
+ import * as Drag from "@wardic/core/Drag"
59
+ import type * as FrameLoop from "@wardic/core/FrameLoop"
60
+ import * as Inertia from "@wardic/core/Inertia"
61
+ import * as InputFeed from "@wardic/core/InputFeed"
62
+ import * as InputSource from "@wardic/core/InputSource"
63
+ import type * as Point from "@wardic/core/Point"
64
+ import * as DragRecognizer from "@wardic/core/DragRecognizer"
65
+ import type * as Values from "@wardic/core/Values"
66
+ import * as Machine from "@wardic/core/Machine"
67
+ import * as Microtask from "@wardic/core/Microtask"
68
+ import * as Probe from "@wardic/core/Probe"
69
+ import type * as FlushGate from "../FlushGate.ts"
70
+ import type * as ProjectionSystem from "../ProjectionSystem.ts"
71
+ import * as Report from "../Report.ts"
72
+ import * as Bool from "effect/Boolean"
73
+ import * as Cause from "effect/Cause"
74
+ import * as Deferred from "effect/Deferred"
75
+ import * as Effect from "effect/Effect"
76
+ import * as Exit from "effect/Exit"
77
+ import * as FiberHandle from "effect/FiberHandle"
78
+ import { constFalse, constUndefined, constVoid, identity } from "effect/Function"
79
+ import * as Match from "effect/Match"
80
+ import * as MutableRef from "effect/MutableRef"
81
+ import * as Option from "effect/Option"
82
+ import * as Predicate from "effect/Predicate"
83
+ import type * as Scope from "effect/Scope"
84
+ import * as Stream from "effect/Stream"
85
+ import * as UndefinedOr from "effect/UndefinedOr"
86
+ import type * as DragConstraints from "./DragConstraints.ts"
87
+ import type * as WardicServices from "@wardic/core/unstable/WardicServices"
88
+ import type * as WardConfig from "./WardConfig.ts"
89
+ import * as DragControls from "./DragControls.ts"
90
+ import * as DragTransition from "./DragTransition.ts"
91
+ import * as WardicConfigValue from "@wardic/core/unstable/WardicConfigValue"
92
+
93
+
94
+ /**
95
+ * The element's values as its drag reads and writes them: `values` and `velocities` by key, when `x`
96
+ * and `y` last moved, and the clock's time. The drag goes on from where the element is, and leaves it
97
+ * where the drag left it, its speed with it.
98
+ */
99
+ // The spine's mutable Snapshot, the same object `runSpine` holds: each key's last value, a number or a
100
+ // string (ADR-0024).
101
+ export interface DragSnapshot {
102
+ values: Values.Values
103
+ velocities: Record<string, number>
104
+ /** When the drag last moved `x` and `y`: their velocity holds 30 ms from then. */
105
+ drawnAt: Record<string, number>
106
+ /** The time now, on the element's provider's Clock. */
107
+ readonly now: () => number
108
+ }
109
+
110
+ /** A drag's config, read once as it starts: the `drag` props of a Wardic element. */
111
+ export interface DragTrackConfig {
112
+ /** `true` = free; `"x"`/`"y"` = axis-locked. */
113
+ readonly axis: boolean | "x" | "y"
114
+ /**
115
+ * `true`: the drag takes no **Drag claim** and is never refused, so a draggable ancestor can
116
+ * drag with it (Motion's `dragPropagation`). Default `false`.
117
+ */
118
+ readonly propagation?: boolean | undefined
119
+ /**
120
+ * Optional numeric constraints (per axis) on the element's x/y, whichever gesture moved it there
121
+ * (Motion's): the drag keeps it within them, and a Fling springs back
122
+ * to an edge it would cross. A projection node's x/y is its drag offset.
123
+ */
124
+ readonly constraints?: Drag.Bounds | undefined
125
+ /** Rubber-band resistance past `constraints`: 0 a hard clamp. Default {@link DragTransition.DEFAULT_DRAG_ELASTIC}. */
126
+ readonly elastic?: number | undefined
127
+ /** Fling on release (default true); `false` releases with no velocity. */
128
+ readonly momentum?: boolean | undefined
129
+ /** Spring back to the origin on release: both edges at 0. */
130
+ readonly snapToOrigin?: boolean | undefined
131
+ /**
132
+ * Motion's `dragDirectionLock`: each gesture starts once it passes 10 px on one
133
+ * axis, and drags, claims and releases on that axis alone (`DragRecognizer.drag`'s `lock`).
134
+ */
135
+ readonly directionLock?: boolean | undefined
136
+ /** What the release runs on: the element's drag transition, resolved ({@link DragTransition.dragTransitionOf}). */
137
+ readonly transition: ProjectionSystem.DragTransition
138
+ /**
139
+ * Motion's `dragListener`: `false`, the element's own presses start no drag and stop no
140
+ * release; only its **Drag controls**' starts do. Default `true`.
141
+ */
142
+ readonly listen?: boolean | undefined
143
+ }
144
+
145
+ /** What the track's watches and responses run with: the provider's services and the gestures' input (#511). */
146
+ export type TrackServices = WardicServices.WardicServices | InputSource.InputSource
147
+
148
+ /** Options for {@link driveDragTrack}. */
149
+ export interface DragTrackOptions {
150
+ /**
151
+ * Runs the track in the element's watch Scope: its watches subscribed now, with no
152
+ * fiber, until the element goes and the Scope closes, which stops a running release. A throw from a
153
+ * watch's fold goes to the `onDefect` it's given (#209: each binding owns the Scope).
154
+ */
155
+ readonly watch: (track: (onDefect: InputFeed.OnDefect) => Effect.Effect<void, never, Scope.Scope | TrackServices>) => void
156
+ /**
157
+ * Runs a response forked on the provider runtime: its fiber starts at once and runs to its
158
+ * end inside the event. A release it forks keeps the runtime's scheduler, a host's if it gave one,
159
+ * not a `runSync`'s sync one, which would resume
160
+ * the release's yields on a microtask.
161
+ */
162
+ readonly fork: (response: Effect.Effect<void, never, TrackServices>) => void
163
+ /** The provider-owned flush gate; the drag writes `x`/`y` into `id`. */
164
+ readonly gate: FlushGate.Shape
165
+ /** The element's gate id (shared with the spine, so writes merge). */
166
+ readonly id: string
167
+ /** The live element the drag recognizer attaches to. */
168
+ readonly element: HTMLElement
169
+ /** The spine's Snapshot object (shared — see {@link DragSnapshot}). */
170
+ readonly snapshot: DragSnapshot
171
+ /** Merge a value-map into the presence snapshot (mirrors the spine's recordFrame). */
172
+ readonly recordFrame: (value: Values.Values) => void
173
+ /** Flip the reactive `dragging` signal the spine reads for `whileDrag` + key stripping. */
174
+ readonly setDragging: (dragging: boolean) => void
175
+ /**
176
+ * Told before the drag reads where the element rests, or writes `x`/`y`: a Batch member's element
177
+ * takes its transform from its Batch then (#665's PR 4), so the drag starts where the Batch drew it,
178
+ * and is the only one drawing it. Omitted, nothing is told.
179
+ */
180
+ readonly claim?: (() => void) | undefined
181
+ /** The static drag config (read once at setup). */
182
+ readonly config: DragTrackConfig
183
+ /**
184
+ * When the element is a projection node, the drag feeds the projection
185
+ * instead of writing raw `x`/`y`, which stay the element's own, drawn on top:
186
+ * grab → `dragStart`, each sample → `dragMove(offset)`, release →
187
+ * `dragEnd(velocity)` (its Release offset — the free tail is skipped), a press →
188
+ * `stopRelease`, a press that ends with no drag → `resumeRelease`. Absent ⇒ free drag.
189
+ */
190
+ readonly projection?: ProjectionSystem.DragHandle | undefined
191
+ /**
192
+ * The gesture's axis, read at each gesture's "start" and held until its "end"
193
+ * (`true` = free), in place of `config.axis`: the axis it moves on and claims
194
+ * (internal: a `<Reorder>` item, whose group decides the axis per gesture, D5).
195
+ */
196
+ readonly axisAt?: (() => boolean | "x" | "y") | undefined
197
+ /**
198
+ * Each sample, after its projection write: after `dragMove` for "start" and
199
+ * "move", after `dragEnd` for "end", so a reader of the handle's `dragOffset()`
200
+ * sees this sample (internal: a `<Reorder>` item's group).
201
+ */
202
+ readonly onSample?: ((sample: Drag.Sample) => void) | undefined
203
+ /**
204
+ * The release came to rest on its own (Motion's `onDragTransitionEnd`): its Fling or its
205
+ * spring back finished, or it was dropped where it rests. Told after the gate's flush of the rest
206
+ * value. Not for one a grab stopped. A free drag's only: a layout node's release is its Release
207
+ * offset, which the projection tells.
208
+ */
209
+ readonly onReleaseEnd?: (() => void) | undefined
210
+ /**
211
+ * Whether its animations are skipped (its nearest `WardicConfig`'s `skipAnimations`), read at
212
+ * each release: a skipped one lands at once where it would come to rest. Omitted, never.
213
+ */
214
+ readonly skipAnimations?: (() => boolean) | undefined
215
+ /**
216
+ * An element constraint (Motion's `dragConstraints={ref}`): the container whose box
217
+ * the element's must stay inside, in place of `config.constraints`. It's measured at each drag's
218
+ * start, and again when the element or the container resizes; a resize that changes the bounds
219
+ * moves an element at rest (or releasing, which it stops) to keep its place within them (Motion's
220
+ * `scalePositionWithinConstraints`). A container not there at a drag's start leaves it unconstrained.
221
+ */
222
+ readonly container?: DragConstraints.DragContainer | undefined
223
+ /**
224
+ * The **Drag controls** given to the element (Motion's `dragControls`): the track subscribes to
225
+ * them while its watch Scope is open.
226
+ */
227
+ readonly controls?: DragControls.WardicDragControls | undefined
228
+ /**
229
+ * Where the drag's points are (its nearest `WardicConfig`'s `transformPagePoint`): read at
230
+ * each down, and applied to its every point and to the box its snap to the cursor measures.
231
+ * Omitted, as they are.
232
+ */
233
+ readonly mapPoint?: (() => WardConfig.WardicTransformPoint | undefined) | undefined
234
+ }
235
+
236
+ /** A point mapping now (#443): the config's, else the point as it is. */
237
+ const mapPointOf = (mapPoint: DragTrackOptions["mapPoint"]): WardConfig.WardicTransformPoint =>
238
+ UndefinedOr.match(UndefinedOr.match(mapPoint, { onUndefined: constUndefined, onDefined: (read) => read() }), { onUndefined: () => identity, onDefined: identity })
239
+
240
+ /** A drag config's axis as core's: `true` (free) is `"xy"`. */
241
+ const toDragAxis = (axis: boolean | "x" | "y"): Drag.Axis =>
242
+ Match.value(axis).pipe(
243
+ Match.when("x", () => "x" as const),
244
+ Match.when("y", () => "y" as const),
245
+ Match.orElse(() => "xy" as const),
246
+ )
247
+
248
+ /**
249
+ * The track's state: `base` is the rebase point, `position` the live absolute position,
250
+ * `held` whether a gesture is on (from its "start" to its "end"), `axis` the axes it
251
+ * moves on, read at its "start", `bounds` the constraints: the config's, or an element
252
+ * constraint's as last measured (#273), and whether a release runs, or a grab stopped one (#279).
253
+ */
254
+ interface DragTrackState {
255
+ readonly base: Point.Point
256
+ readonly position: Point.Point
257
+ readonly held: boolean
258
+ readonly axis: Drag.Axis
259
+ readonly bounds: Drag.Bounds | undefined
260
+ /** A free drag's release runs (#279): set at its start, cleared at its rest. */
261
+ readonly releasing: boolean
262
+ /** A grab stopped that release (#279): a press that ends with no drag starts it again, from rest. */
263
+ readonly stopped: boolean
264
+ /**
265
+ * A layout node's snap to the cursor at its controls' start (#431): its drag offset, the projection's
266
+ * drag begun there, until the gesture starts from it or the press ends with none.
267
+ */
268
+ readonly snapped: Point.Point | undefined
269
+ }
270
+
271
+ /** The track's one cell, written by its handlers and by its release's sink, never at once. */
272
+ type TrackState = MutableRef.MutableRef<DragTrackState>
273
+
274
+ const ORIGIN: Point.Point = { x: 0, y: 0 }
275
+
276
+ /**
277
+ * A projection node's release velocity (ADR-0016, *Amendment (velocity family)*, decision 2):
278
+ * the "end" sample's, the pointer's over its last 100 ms (ADR-0004, *Amendment (#139)*); at
279
+ * rest with `dragMomentum={false}`.
280
+ */
281
+ const releaseVelocity = (config: DragTrackConfig, velocity: Point.Point): Point.Point =>
282
+ Bool.match(config.momentum === false, {
283
+ onTrue: () => ORIGIN,
284
+ onFalse: () => velocity,
285
+ })
286
+
287
+ /**
288
+ * Where the drag puts the element: `p`, kept within the constraints on each axis the gesture moves,
289
+ * pulled back past an edge by `dragElastic` (Motion's `applyConstraints`). They bound the element's
290
+ * x/y, whichever gesture moved it there (ADR-0016, *Amendment (#140)*); an axis it doesn't move stays
291
+ * where it is. Per pointer event: statement `if`s.
292
+ */
293
+ const constrainedTo = (p: Point.Point, axis: Drag.Axis, bounds: Drag.Bounds | undefined, config: DragTrackConfig): Point.Point => {
294
+ if (Predicate.isUndefined(bounds)) return p
295
+ const elastic = DragTransition.elasticOf(config.elastic)
296
+ let x = p.x
297
+ let y = p.y
298
+ if (axis !== "y") x = Drag.constrain(p.x, bounds.x, elastic)
299
+ if (axis !== "x") y = Drag.constrain(p.y, bounds.y, elastic)
300
+ return { x, y }
301
+ }
302
+
303
+ /** Both edges at the origin: what `dragSnapToOrigin` releases toward (Motion's `{ min: 0, max: 0 }`). */
304
+ const AT_ORIGIN: Drag.Bounds = { x: { min: 0, max: 0 }, y: { min: 0, max: 0 } }
305
+ const NO_EDGES: Drag.Bounds = {}
306
+
307
+ /** The edges a release springs back to: both at the origin with `snapToOrigin`, else the constraints. */
308
+ const edgesOf = (config: DragTrackConfig, bounds: Drag.Bounds | undefined): Drag.Bounds =>
309
+ Bool.match(config.snapToOrigin === true, {
310
+ onTrue: () => AT_ORIGIN,
311
+ onFalse: () => UndefinedOr.match(bounds, { onUndefined: () => NO_EDGES, onDefined: identity }),
312
+ })
313
+
314
+ /** Whether `v` sits within `range`, or there's none: a hard clamp leaves it where it is. */
315
+ const within = (v: number, range: Drag.AxisRange | undefined): boolean => Drag.constrain(v, range, 0) === v
316
+
317
+ /**
318
+ * Whether a release with `velocity` from `position` rests where it was dropped: no speed, no
319
+ * `modifyTarget` to move it, and within its edges on each axis it moves. Motion's inertia is done on
320
+ * its first frame then; Wardic commits it at once.
321
+ */
322
+ const restsWhereDropped = (position: Point.Point, velocity: Point.Point, axis: Drag.Axis, edges: Drag.Bounds, transition: ProjectionSystem.DragTransition): boolean =>
323
+ Bool.every([
324
+ velocity.x === 0,
325
+ velocity.y === 0,
326
+ Predicate.isUndefined(transition.modifyTarget),
327
+ Bool.or(axis === "y", within(position.x, edges.x)),
328
+ Bool.or(axis === "x", within(position.y, edges.y)),
329
+ ])
330
+ /**
331
+ * Read the current rested `x`/`y` off the snapshot (defaulting to 0, as for a string
332
+ * translate the drag can't continue from). Captured at a drag's "start" so a drag (or a
333
+ * re-grab mid-release) continues from where the element currently sits instead of
334
+ * snapping to origin.
335
+ */
336
+ const snapshotPos = (snapshot: DragSnapshot): Point.Point => {
337
+ const { x, y } = snapshot.values
338
+ return { x: numberOrZero(x), y: numberOrZero(y) }
339
+ }
340
+
341
+ /** Its `claim` told, when it has one (#665's PR 4): before each read of where it rests and each write. */
342
+ const claimed = (opts: DragTrackOptions): void => {
343
+ if (Predicate.isNotUndefined(opts.claim)) opts.claim()
344
+ }
345
+
346
+ /** Where the element rests now, read off the snapshot once its `claim` is told: a Batch member's where its Batch drew it. */
347
+ const restingAt = (opts: DragTrackOptions): Point.Point => {
348
+ claimed(opts)
349
+ return snapshotPos(opts.snapshot)
350
+ }
351
+
352
+ /** A value that's a number, else 0. Built once, at module load. */
353
+ const numberOrZero: (value: unknown) => number = Match.type<unknown>().pipe(
354
+ Match.when(Match.number, (n) => n),
355
+ Match.orElse(() => 0),
356
+ )
357
+
358
+ /**
359
+ * Direct-set `x`/`y` onto the element + the shared snapshot for one drag frame.
360
+ * The drag is the SOLE owner of `x`/`y` while dragging, so it sets them on the
361
+ * snapshot with zeroed velocities (the spring never fights them — the spine
362
+ * strips the translate keys from its target while `dragging()` is true).
363
+ */
364
+ const directSet = (opts: DragTrackOptions, p: Point.Point): void => {
365
+ // Projection node (ADR-0016): feed the live pointer offset to the projection, whose
366
+ // target moves the node; its own x/y are its animations', drawn on top (#363), so
367
+ // the drag leaves them be. `base` stays 0 and `position` is the pure pointer offset,
368
+ // which IS the dragDelta.
369
+ if (Predicate.isNotUndefined(opts.projection)) {
370
+ opts.projection.dragMove({ x: p.x, y: p.y })
371
+ return
372
+ }
373
+ claimed(opts)
374
+ opts.gate.write(opts.id, { x: p.x, y: p.y })
375
+ opts.snapshot.values.x = p.x
376
+ opts.snapshot.values.y = p.y
377
+ opts.snapshot.velocities.x = 0
378
+ opts.snapshot.velocities.y = 0
379
+ // Moved now (#464): its release's velocity holds from here.
380
+ const now = opts.snapshot.now()
381
+ opts.snapshot.drawnAt.x = now
382
+ opts.snapshot.drawnAt.y = now
383
+ opts.recordFrame({ x: p.x, y: p.y })
384
+ }
385
+
386
+ /**
387
+ * A gesture's bookkeeping, run as each sample arrives, before its response: "start"
388
+ * marks the gesture held; "end" clears it and, for a projection node (ADR-0016), starts its
389
+ * Release offset at the sample's velocity (`dragEnd`) and hands back `dragging`. The "end" is
390
+ * reported after its response, once the release runs, as Motion's `onDragEnd` comes after its
391
+ * release starts: a target for `x`/`y` the app sets there takes it over (#340).
392
+ */
393
+ const settleGesture = (sample: Drag.Sample, state: TrackState, opts: DragTrackOptions): void =>
394
+ Match.value(sample.phase).pipe(
395
+ Match.when("start", () => {
396
+ MutableRef.update(state, (st) => ({ ...st, held: true }))
397
+ }),
398
+ Match.when("move", constVoid),
399
+ Match.when("end", () => {
400
+ MutableRef.update(state, (st) => ({ ...st, held: false }))
401
+ UndefinedOr.match(opts.projection, {
402
+ onUndefined: constVoid,
403
+ onDefined: (projection) => {
404
+ projection.dragEnd(releaseVelocity(opts.config, sample.velocity))
405
+ opts.setDragging(false)
406
+ },
407
+ })
408
+ }),
409
+ Match.exhaustive,
410
+ )
411
+
412
+ /** A down from the Primary pointer: a grab, which stops a running release where it is. */
413
+ const isGrab: Predicate.Predicate<InputSource.PointerLikeEvent> = Predicate.and(
414
+ (e: InputSource.PointerLikeEvent) => e.type === "down",
415
+ InputSource.isPrimaryPointer,
416
+ )
417
+
418
+ /** A down the element adopted from its **Drag controls** (#431). */
419
+ const isAdopted: Predicate.Predicate<InputSource.PointerLikeEvent> = (e) => e.adopted === true
420
+
421
+ /**
422
+ * A layout node's snap to the cursor ended with no drag (#431): its drag ended at rest, back to its
423
+ * slot. Whether there was one. A module's, not a track's (#507): a track given no Drag controls never
424
+ * snaps, and makes nothing for it.
425
+ */
426
+ const unsnap = (state: TrackState, projection: ProjectionSystem.DragHandle): boolean => {
427
+ if (Predicate.isUndefined(MutableRef.get(state).snapped)) return false
428
+ MutableRef.update(state, (s) => ({ ...s, snapped: undefined }))
429
+ projection.dragEnd(ORIGIN)
430
+ return true
431
+ }
432
+
433
+ /**
434
+ * Snap the element's centre under `down`'s point (#431, Motion's `snapToCursor`), on the axes it drags,
435
+ * measured from its box now: a plain element's x/y moved by the difference, a layout node's projection
436
+ * drag begun at it. A module's (#507), as {@link unsnap} is.
437
+ */
438
+ const snapTo = (opts: DragTrackOptions, state: TrackState, down: InputSource.PointerLikeEvent): void => {
439
+ const box = opts.element.getBoundingClientRect()
440
+ const axis = UndefinedOr.match(opts.axisAt, { onUndefined: () => toDragAxis(opts.config.axis), onDefined: (at) => toDragAxis(at()) })
441
+ // The pointer and the box's centre, each mapped as the drag's points are (#443, Motion's).
442
+ const map = mapPointOf(opts.mapPoint)
443
+ const cursor = map({ x: down.x, y: down.y })
444
+ const centre = map({ x: box.left + box.width / 2, y: box.top + box.height / 2 })
445
+ const by = onAxes({ x: cursor.x - centre.x, y: cursor.y - centre.y }, axis)
446
+ UndefinedOr.match(opts.projection, {
447
+ onUndefined: () => {
448
+ const from = restingAt(opts)
449
+ const to = { x: from.x + by.x, y: from.y + by.y }
450
+ MutableRef.update(state, (s) => ({ ...s, position: to }))
451
+ directSet(opts, to)
452
+ },
453
+ onDefined: (projection) => {
454
+ const was = MutableRef.get(state).snapped
455
+ const at = UndefinedOr.match(was, { onUndefined: () => by, onDefined: (prev) => ({ x: prev.x + by.x, y: prev.y + by.y }) })
456
+ if (Predicate.isUndefined(was)) projection.dragStart()
457
+ projection.dragMove(at)
458
+ MutableRef.update(state, (s) => ({ ...s, snapped: at }))
459
+ },
460
+ })
461
+ }
462
+
463
+ /**
464
+ * The end of a Primary pointer's press: its up, its cancel, or a move with no button pressed
465
+ * (its release was lost, ADR-0019). A press with no drag lets a projection node's stopped
466
+ * release go on; any other unpressed move (a hover) finds none stopped.
467
+ */
468
+ const isPressEnd: Predicate.Predicate<InputSource.PointerLikeEvent> = Predicate.and(
469
+ (e: InputSource.PointerLikeEvent) =>
470
+ Bool.some([e.type === "up", e.type === "cancel", Bool.and(e.type === "move", Bool.not(e.pressed))]),
471
+ InputSource.isPrimaryPointer,
472
+ )
473
+
474
+ /** Hand `sample` to `onSample`, when there is one (a `<Reorder>` item's group). */
475
+ const reportSample = (opts: DragTrackOptions, sample: Drag.Sample): void =>
476
+ UndefinedOr.match(opts.onSample, { onUndefined: constVoid, onDefined: (onSample) => onSample(sample) })
477
+
478
+ /**
479
+ * Where a drag starts from (ADR-0004, ADR-0016): wherever a plain element rests (the snapshot's
480
+ * x/y); a projection node's slot, the origin, since its drag offset moves its projection target
481
+ * from its slot, and its own x/y are drawn on top of that (#363), or where a snap to the cursor put
482
+ * it (#431).
483
+ */
484
+ const startOf = (opts: DragTrackOptions, st: DragTrackState): Point.Point =>
485
+ UndefinedOr.match(opts.projection, {
486
+ onUndefined: () => restingAt(opts),
487
+ onDefined: () => UndefinedOr.match(st.snapped, { onUndefined: () => ORIGIN, onDefined: identity }),
488
+ })
489
+
490
+ /**
491
+ * Follow the pointer for a "start" or "move" sample: absolute position = base + drag
492
+ * offset, kept within the constraints. A "start" rebases to where the drag starts from
493
+ * ({@link startOf}); for a projection node, the origin, and the "start" begins the
494
+ * projection's drag first, so `dragStart` runs before the first `dragMove`, unless a snap to the
495
+ * cursor began it (#431).
496
+ */
497
+ const follow = (sample: Drag.Sample, state: TrackState, opts: DragTrackOptions): void => {
498
+ const st = MutableRef.get(state)
499
+ const starts = sample.phase === "start"
500
+ const base = Bool.match(starts, { onTrue: () => startOf(opts, st), onFalse: () => st.base })
501
+ // The axes the gesture moves on, as the recognizer read them at its start: a `<Reorder>` item's
502
+ // group decides them per gesture (D5), a direction lock at its lock (#273).
503
+ const axis = Bool.match(starts, { onTrue: () => sample.axis, onFalse: () => st.axis })
504
+ const p = constrainedTo({ x: base.x + sample.offset.x, y: base.y + sample.offset.y }, axis, st.bounds, opts.config)
505
+ MutableRef.set(state, { ...st, base, position: p, axis, snapped: undefined })
506
+ opts.setDragging(true)
507
+ if (Bool.and(starts, Predicate.isUndefined(st.snapped))) UndefinedOr.match(opts.projection, { onUndefined: constVoid, onDefined: (projection) => projection.dragStart() })
508
+ directSet(opts, p)
509
+ reportSample(opts, sample)
510
+ }
511
+
512
+ /**
513
+ * The release driver (ADR-0004 binding orchestration; ADR-0016, *Amendment (#140)*), Motion's: an
514
+ * inertia on the drag transition on each axis the gesture moved, from the release position and
515
+ * velocity (none with `momentum: false`), toward its edges: both at the origin with `snapToOrigin`,
516
+ * else the constraints. A Fling glides on and springs back to an edge it would cross, or starts past.
517
+ * One that rests where it was dropped (no velocity, within its edges) commits the exact release
518
+ * position once (so the element rests at the pointer-up point, not the last move), and hands `x`/`y`
519
+ * back at the end of the release's task.
520
+ *
521
+ * Each release frame taps `gate.write` + snapshot sync + recordFrame, and the track's
522
+ * state, so a re-grab mid-tail rebases from where the tail reached. On completion
523
+ * `setDragging(false)` hands ownership of `x`/`y` back to the spring. The inertia runs on the
524
+ * Frame loop (ADR-0027); interrupting the release removes its run from the loop at once,
525
+ * before the next handler touches the state.
526
+ *
527
+ * A release that dies, on a drag transition core rejects (#66, ADR-0025 *Amendment (#66)*) or a
528
+ * throw in its frames, fails alone: reported, the element rests where it was dropped, and the track
529
+ * goes on to the next drag.
530
+ *
531
+ * Its velocity stays on the Snapshot, each frame's, from the release's own at its start; and once
532
+ * `taken` is done (#340, {@link DragTrack.takeRelease}), it stops where it is and tells no end: a
533
+ * Retarget has `x`/`y`, from that position and velocity.
534
+ */
535
+ const release = (
536
+ opts: DragTrackOptions,
537
+ state: TrackState,
538
+ position: Point.Point,
539
+ velocity: Point.Point,
540
+ taken: Deferred.Deferred<void>,
541
+ ): Effect.Effect<void, never, FrameLoop.FrameLoop> => {
542
+ const cfg = opts.config
543
+
544
+ // A run's sink on the Frame loop is a plain call (ADR-0027, decision 8). One a takeover beat to
545
+ // the loop writes nothing: the fiber's interruption comes through the scheduler (#340).
546
+ const tapFrame = (p: Point.Point, v: Point.Point): undefined => {
547
+ if (Bool.not(MutableRef.get(state).releasing)) return undefined
548
+ MutableRef.update(state, (s) => ({ ...s, position: p }))
549
+ directSet(opts, p)
550
+ keepVelocity(opts, v)
551
+ return undefined
552
+ }
553
+
554
+ // At rest, the drag hands back, then its release's end is told (#273) after the gate's next flush,
555
+ // with the rest value on the element, as Motion's comes with it set: only here, so a release a
556
+ // grab interrupted tells nothing.
557
+ const done = Effect.sync(() => {
558
+ MutableRef.update(state, (s) => ({ ...s, releasing: false }))
559
+ opts.setDragging(false)
560
+ UndefinedOr.map(opts.onReleaseEnd, (ended) => opts.gate.afterFlush(ended))
561
+ })
562
+ const { axis, bounds } = MutableRef.get(state)
563
+ const edges = edgesOf(cfg, bounds)
564
+ const v = releaseVelocity(cfg, velocity)
565
+ const transition = cfg.transition
566
+ // Before its first frame, a takeover starts from the release's velocity, on the axes it runs on.
567
+ keepVelocity(opts, onAxes(v, axis))
568
+
569
+ // The position now; the hand-back at the end of the release's task, once every other
570
+ // consumer of the same event has handled it: a press that ends with the drag (`whileTap`)
571
+ // then finds `x`/`y` still the drag's, so a key the drag moved stays where it was dropped.
572
+ // A takeover ends the release at once, its end untold (#340).
573
+ const untilTaken = (run: Effect.Effect<void, never, FrameLoop.FrameLoop>) => Effect.raceFirst(run, Deferred.await(taken))
574
+ if (restsWhereDropped(position, v, axis, edges, transition)) {
575
+ return untilTaken(
576
+ Effect.sync(() => {
577
+ if (MutableRef.get(state).releasing) directSet(opts, position)
578
+ }).pipe(Effect.andThen(Microtask.next), Effect.andThen(done)),
579
+ )
580
+ }
581
+ // Skipped (#468): it lands at once where its inertia would come to rest, on each axis it runs on,
582
+ // with no velocity. Motion's skipped release goes to its keyframes' last, the drag's origin; Wardic's
583
+ // lands where the release would. Its end is told as a release's at rest.
584
+ if (UndefinedOr.match(opts.skipAnimations, { onUndefined: constFalse, onDefined: (skips) => skips() })) {
585
+ return untilTaken(
586
+ Effect.sync(() => {
587
+ keepVelocity(opts, ORIGIN)
588
+ if (MutableRef.get(state).releasing) directSet(opts, restOfRelease(position, v, axis, edges, transition))
589
+ }).pipe(
590
+ Effect.catchDefect((defect) => Report.failure("wardic.drag: a release failed", Cause.die(defect)).pipe(Effect.andThen(Effect.sync(() => tapFrame(position, ORIGIN))))),
591
+ Effect.andThen(Microtask.next),
592
+ Effect.andThen(done),
593
+ ),
594
+ )
595
+ }
596
+ // Only the axes the gesture moved run (Motion's `shouldDrag`): the other stays where it is.
597
+ return untilTaken(
598
+ Match.value(axis).pipe(
599
+ Match.when("x", () =>
600
+ Machine.inertia(position.x, v.x, { ...transition, ...edges.x }).pipe(
601
+ Machine.run((frame) => tapFrame({ x: frame.value, y: position.y }, { x: frame.velocity, y: 0 })),
602
+ )),
603
+ Match.when("y", () =>
604
+ Machine.inertia(position.y, v.y, { ...transition, ...edges.y }).pipe(
605
+ Machine.run((frame) => tapFrame({ x: position.x, y: frame.value }, { x: 0, y: frame.velocity })),
606
+ )),
607
+ Match.orElse(() => Machine.inertiaVec(position, v, { ...transition, ...edges }).pipe(Machine.run((frame) => tapFrame(frame.value, frame.velocity)))),
608
+ Effect.catchDefect((defect) =>
609
+ Report.failure("wardic.drag: a release failed", Cause.die(defect)).pipe(Effect.andThen(Effect.sync(() => tapFrame(position, ORIGIN))))),
610
+ Effect.andThen(done),
611
+ ),
612
+ )
613
+ }
614
+
615
+ /**
616
+ * Where a release from `position` at `v` comes to rest (#468): each axis it runs on where its inertia
617
+ * would ({@link Inertia.restOf}), the other where it is.
618
+ */
619
+ const restOfRelease = (position: Point.Point, v: Point.Point, axis: Drag.Axis, edges: Drag.Bounds, transition: ProjectionSystem.DragTransition): Point.Point =>
620
+ Match.value(axis).pipe(
621
+ Match.when("x", () => ({ x: Inertia.restOf(position.x, v.x, { ...transition, ...edges.x }), y: position.y })),
622
+ Match.when("y", () => ({ x: position.x, y: Inertia.restOf(position.y, v.y, { ...transition, ...edges.y }) })),
623
+ Match.orElse(() => ({ x: Inertia.restOf(position.x, v.x, { ...transition, ...edges.x }), y: Inertia.restOf(position.y, v.y, { ...transition, ...edges.y }) })),
624
+ )
625
+
626
+ /** `v` on the axes a release runs on, 0 on the other. */
627
+ const onAxes = (v: Point.Point, axis: Drag.Axis): Point.Point =>
628
+ Match.value(axis).pipe(
629
+ Match.when("x", () => ({ x: v.x, y: 0 })),
630
+ Match.when("y", () => ({ x: 0, y: v.y })),
631
+ Match.orElse(() => v),
632
+ )
633
+
634
+ /** Whether `retargeted` names an axis a release on `axis` runs on (#340). */
635
+ const onReleaseAxis = (axis: Drag.Axis, retargeted: { readonly x: boolean; readonly y: boolean }): boolean =>
636
+ Match.value(axis).pipe(
637
+ Match.when("x", () => retargeted.x),
638
+ Match.when("y", () => retargeted.y),
639
+ Match.orElse(() => Bool.or(retargeted.x, retargeted.y)),
640
+ )
641
+
642
+ /** A release's velocity on the Snapshot, after its direct-set zeroed it (#340). */
643
+ const keepVelocity = (opts: DragTrackOptions, v: Point.Point): void => {
644
+ opts.snapshot.velocities.x = v.x
645
+ opts.snapshot.velocities.y = v.y
646
+ const now = opts.snapshot.now()
647
+ opts.snapshot.drawnAt.x = now
648
+ opts.snapshot.drawnAt.y = now
649
+ }
650
+
651
+ /** A running drag track, for the spine's Retargets (#340). */
652
+ export interface DragTrack {
653
+ /**
654
+ * Take `x`/`y` from a free drag's running release (#340) for a Retarget that gives `retargeted`
655
+ * new targets, as a newer animation of Motion's value stops the one running: the release stops
656
+ * where it is and tells no end, and `dragging` is handed back at the end of the task (not in the
657
+ * caller's Retarget, which reads it). `false`, and nothing done, when no release runs, a gesture
658
+ * is on, or none of `retargeted` is an axis the release runs on (Motion's runs one per axis).
659
+ */
660
+ readonly takeRelease: (retargeted: { readonly x: boolean; readonly y: boolean }) => boolean
661
+ }
662
+
663
+ /**
664
+ * Run the drag track for one element. Recognize `drag(element)` on its axis (read at
665
+ * each gesture's start for a `<Reorder>` item) with its **Drag claim** (none with
666
+ * `propagation`), then consume it, kept within its constraints, and the element's bare
667
+ * pointer-down grabs, each in place as it arrives (ADR-0032; the module doc). A drag
668
+ * refused its claim emits nothing, so a draggable ancestor of the element that claimed
669
+ * neither follows the pointer nor turns `whileDrag` on; its grab still stops its running
670
+ * release (ADR-0019, *Amendment (#29)*). The track runs in the element's watch Scope
671
+ * (`opts.watch`, #379), with no fiber at rest: closing it stops the watches (dropping the
672
+ * shared feed's count) and any in-flight release. A response that fails, or a release, is
673
+ * reported, and the track goes on.
674
+ *
675
+ * The drag config is read ONCE at setup (the caller passes a static
676
+ * {@link DragTrackConfig} — drag config is a document, not reactive); only the
677
+ * `dragging` signal the spine reads is reactive.
678
+ */
679
+ export const driveDragTrack = (opts: DragTrackOptions): DragTrack => {
680
+ const { config, element } = opts
681
+ const state: TrackState = MutableRef.make<DragTrackState>({
682
+ base: ORIGIN,
683
+ position: ORIGIN,
684
+ held: false,
685
+ axis: "xy",
686
+ bounds: config.constraints,
687
+ releasing: false,
688
+ stopped: false,
689
+ snapped: undefined,
690
+ })
691
+ // The running release's takeover (#340): done by `takeRelease`, from outside the track's responses.
692
+ let taken: Deferred.Deferred<void> = Deferred.makeUnsafe<void>()
693
+
694
+ opts.watch((onDefect) =>
695
+ Effect.gen(function* () {
696
+ // The running release, if any: the next sample or grab interrupts it.
697
+ const releasing = yield* FiberHandle.make<void>()
698
+ const probe = yield* Probe.Probe
699
+
700
+ // --- An element constraint (#273, ADR-0047) ------------------------------------------------
701
+ // Measured at each drag's start, and when the element or its container resizes: the observers'
702
+ // fiber, one container at a time, from the first time the container is there.
703
+ const watching = yield* FiberHandle.make<void>()
704
+ let watched: Element | undefined = undefined
705
+ let warned = false
706
+ const setBounds = (bounds: Drag.Bounds | undefined): void => {
707
+ MutableRef.update(state, (st) => ({ ...st, bounds }))
708
+ }
709
+ /** The bounds measured against `container`'s `box`; none, reported, if measuring throws (an app's `onMeasureDragConstraints`). */
710
+ const measured = (container: DragConstraints.DragContainer, box: Element): Effect.Effect<Drag.Bounds | undefined> =>
711
+ Effect.sync((): Drag.Bounds | undefined => container.bounds(box)).pipe(
712
+ Effect.catchDefect((defect) => Report.failure("wardic.drag: measuring dragConstraints failed", Cause.die(defect)).pipe(Effect.as(undefined))),
713
+ )
714
+ /**
715
+ * A resize of the element or its container: the bounds measured again. The first measure only
716
+ * records them; a later one that changed them moves an element at rest, or releasing (stopped),
717
+ * to keep its place within them (Motion's `scalePositionWithinConstraints`). A held drag, or a
718
+ * layout node (back on its slot at rest), only takes the new bounds.
719
+ */
720
+ const resized = (container: DragConstraints.DragContainer, box: Element): Effect.Effect<void> =>
721
+ measured(container, box).pipe(
722
+ Effect.flatMap((next) => {
723
+ const st = MutableRef.get(state)
724
+ if (Bool.or(Predicate.isUndefined(next), Predicate.isUndefined(st.bounds))) return Effect.sync(() => setBounds(next))
725
+ const from = st.bounds as Drag.Bounds
726
+ const to = next as Drag.Bounds
727
+ if (Drag.sameBounds(from, to)) return Effect.void
728
+ if (Bool.or(st.held, Predicate.isNotUndefined(opts.projection))) return Effect.sync(() => setBounds(to))
729
+ return FiberHandle.clear(releasing).pipe(
730
+ Effect.andThen(Effect.sync(() => {
731
+ const kept = Drag.keepPlace(restingAt(opts), toDragAxis(config.axis), from, to)
732
+ MutableRef.update(state, (s) => ({ ...s, bounds: to, position: kept }))
733
+ directSet(opts, kept)
734
+ opts.setDragging(false)
735
+ })),
736
+ )
737
+ }),
738
+ )
739
+ /** Watch the element and `box` for resizes, from now on: once per container. */
740
+ const watchResizes = (container: DragConstraints.DragContainer, box: Element): Effect.Effect<void> =>
741
+ Effect.suspend(() => {
742
+ if (watched === box) return Effect.void
743
+ watched = box
744
+ return FiberHandle.run(
745
+ watching,
746
+ Stream.runForEach(Stream.merge(probe.observeResize(element), probe.observeResize(box)), () => resized(container, box)),
747
+ ).pipe(Effect.asVoid)
748
+ })
749
+ /** At a drag's start: the bounds measured against the container, which is watched from then on; none if it isn't there. */
750
+ const measureAtStart: Effect.Effect<void> = UndefinedOr.match(opts.container, {
751
+ onUndefined: () => Effect.void,
752
+ onDefined: (container) =>
753
+ Effect.suspend(() =>
754
+ UndefinedOr.match(container.element(), {
755
+ onUndefined: () =>
756
+ Effect.sync(() => setBounds(undefined)).pipe(
757
+ Effect.andThen(Bool.match(warned, {
758
+ onTrue: () => Effect.void,
759
+ onFalse: () => {
760
+ warned = true
761
+ return Effect.logWarning("wardic.drag: dragConstraints' container isn't there at a drag's start, so it drags unconstrained")
762
+ },
763
+ })),
764
+ ),
765
+ onDefined: (box) => measured(container, box).pipe(Effect.tap((bounds) => Effect.sync(() => setBounds(bounds))), Effect.andThen(watchResizes(container, box))),
766
+ })
767
+ ),
768
+ })
769
+ // A container already there is watched from the start: its first delivery records the bounds.
770
+ // Forked, so its fiber runs on the runtime's scheduler, not this setup's `runSync`'s.
771
+ UndefinedOr.match(opts.container, {
772
+ onUndefined: constVoid,
773
+ onDefined: (container) => UndefinedOr.match(container.element(), { onUndefined: constVoid, onDefined: (box) => opts.fork(watchResizes(container, box)) }),
774
+ })
775
+
776
+ const input = yield* InputSource.InputSource
777
+
778
+ // The gesture's axis, read by the recognizer at each start: a `<Reorder>` item's group
779
+ // decides it per gesture (D5). Then the numeric shaping (Probe-free, ADR-0004).
780
+ const axisAt = opts.axisAt
781
+ const axis: Drag.Axis | (() => Drag.Axis) = UndefinedOr.match(axisAt, {
782
+ onUndefined: () => toDragAxis(config.axis),
783
+ onDefined: (at) => () => toDragAxis(at()),
784
+ })
785
+ const { propagation = false } = config
786
+ const listens = config.listen !== false
787
+
788
+ // --- Drag controls (#431), only for an element given them (#507) ----------------------------
789
+ const controlled = Predicate.isNotUndefined(opts.controls)
790
+ // The threshold of the gesture an adopted down claims: its controls' `distanceThreshold`, set for
791
+ // the length of the start that adopts it. Any other down's is the recognizer's own, and a track
792
+ // with no controls has none to set.
793
+ let adoptedThreshold: number | undefined = undefined
794
+ let threshold: ((down: InputSource.PointerLikeEvent) => Option.Option<number>) | undefined
795
+ if (controlled) {
796
+ threshold = (down) => {
797
+ if (down.adopted === true) return Option.fromUndefinedOr(adoptedThreshold)
798
+ return Option.none()
799
+ }
800
+ }
801
+
802
+ /** A free drag's release from `position` with `velocity`, run in `releasing` (#279: recorded as running). */
803
+ const startRelease = (position: Point.Point, velocity: Point.Point): Effect.Effect<void, never, FrameLoop.FrameLoop> =>
804
+ Effect.sync(() => {
805
+ MutableRef.update(state, (s) => ({ ...s, releasing: true, stopped: false }))
806
+ taken = Deferred.makeUnsafe<void>()
807
+ return taken
808
+ }).pipe(
809
+ Effect.flatMap((ended) => FiberHandle.run(releasing, release(opts, state, position, velocity, ended))),
810
+ Effect.asVoid,
811
+ )
812
+
813
+ // A free drag's "end" starts its release; a projection node's Release offset already
814
+ // began in `settleGesture`, so its drag track skips the tail (ADR-0016, Q3c).
815
+ const releaseFrom = (sample: Drag.Sample): Effect.Effect<void, never, FrameLoop.FrameLoop> =>
816
+ UndefinedOr.match(opts.projection, {
817
+ onDefined: () => Effect.void,
818
+ onUndefined: () => {
819
+ const { axis, base, bounds } = MutableRef.get(state)
820
+ const position = constrainedTo({ x: base.x + sample.offset.x, y: base.y + sample.offset.y }, axis, bounds, config)
821
+ return startRelease(position, sample.velocity)
822
+ },
823
+ })
824
+
825
+ // Each sample: its bookkeeping, then its response. A "start" or "move" stops a running
826
+ // release first, as each new event did under `switchMap`.
827
+ const onSample = (sample: Drag.Sample): Effect.Effect<void, never, FrameLoop.FrameLoop> =>
828
+ Effect.suspend(() => {
829
+ settleGesture(sample, state, opts)
830
+ return Match.value(sample.phase).pipe(
831
+ // A start measures an element constraint first (#273), so it follows within this drag's bounds.
832
+ Match.when("start", () =>
833
+ FiberHandle.clear(releasing).pipe(Effect.andThen(measureAtStart), Effect.andThen(Effect.sync(() => follow(sample, state, opts))))),
834
+ Match.when("move", () =>
835
+ FiberHandle.clear(releasing).pipe(Effect.andThen(Effect.sync(() => follow(sample, state, opts))))),
836
+ // A cancelled end (#431) stays where it is: no release, `x`/`y` handed back.
837
+ Match.when("end", () =>
838
+ Bool.match(sample.cancelled === true, {
839
+ onTrue: () => Effect.sync(() => UndefinedOr.match(opts.projection, { onUndefined: () => opts.setDragging(false), onDefined: constVoid })),
840
+ onFalse: () => releaseFrom(sample),
841
+ }).pipe(Effect.andThen(Effect.sync(() => reportSample(opts, sample))))),
842
+ Match.exhaustive,
843
+ )
844
+ })
845
+
846
+ // A grab stops a running release where it is. With no gesture held, the stopped
847
+ // release (or the one a same-down "end" just started) hands `x`/`y` back to the
848
+ // spring; a drag that follows takes them again at its "start", where it rebases (a
849
+ // new down from a pointer whose release was lost also ends the old gesture, a Stale
850
+ // gesture, ADR-0019). A projection node's Release offset stops where it is too, and a
851
+ // drag that follows starts from there; a press that ends with no drag lets it go on,
852
+ // from rest (Motion's `stopAnimation` at the press and `resumeAnimation`, ADR-0016,
853
+ // *Amendment (velocity family)*). The InputSource is a Shared source, so this shares
854
+ // the recognizer's single listener-set (CONTEXT.md Shared source contract).
855
+ const onGrab: Effect.Effect<void> = FiberHandle.clear(releasing).pipe(
856
+ Effect.andThen(Effect.sync(() => {
857
+ // A free drag's release this grab stopped, to start again if the press ends with no drag (#279).
858
+ MutableRef.update(state, (s) => ({ ...s, stopped: s.releasing, releasing: false }))
859
+ UndefinedOr.match(opts.projection, { onUndefined: constVoid, onDefined: (projection) => projection.stopRelease() })
860
+ if (!MutableRef.get(state).held) opts.setDragging(false)
861
+ })),
862
+ )
863
+ /**
864
+ * A free drag's release a grab stopped, started again from rest where it stopped, toward its edges
865
+ * (#279, Motion's `resumeAnimation`): only with `snapToOrigin` or constraints, which give it
866
+ * somewhere to go. Motion's starts one at any press that ends with no drag, so a click at rest on
867
+ * a constrained element tells `onDragTransitionEnd`; this only starts the one the grab stopped, as
868
+ * a layout node's `resumeRelease` does.
869
+ */
870
+ const resumeFree: Effect.Effect<void, never, FrameLoop.FrameLoop> = Effect.suspend(() => {
871
+ const st = MutableRef.get(state)
872
+ if (Bool.not(st.stopped)) return Effect.void
873
+ MutableRef.update(state, (s) => ({ ...s, stopped: false }))
874
+ if (Bool.and(config.snapToOrigin !== true, Predicate.isUndefined(st.bounds))) return Effect.void
875
+ return startRelease(st.position, ORIGIN)
876
+ })
877
+ // The samples subscribe first, so a drag's own up has ended its gesture by now: only a
878
+ // press that never dragged finds no gesture held.
879
+ // A layout node a snap to the cursor moved (#431) goes back to its slot instead.
880
+ const onPressEnd: Effect.Effect<void, never, FrameLoop.FrameLoop> = Effect.suspend(() => {
881
+ if (MutableRef.get(state).held) return Effect.void
882
+ return UndefinedOr.match(opts.projection, {
883
+ onUndefined: () => resumeFree,
884
+ onDefined: (projection) =>
885
+ Effect.sync(() => {
886
+ if (unsnap(state, projection)) return
887
+ projection.resumeRelease()
888
+ }),
889
+ })
890
+ })
891
+
892
+ /**
893
+ * A response, run inside its event (`opts.fork`): one that fails is reported, and the next
894
+ * event's runs (ADR-0055 *Amendment (#379)*, Q4). A release reports its own (#66).
895
+ */
896
+ const respond = (response: Effect.Effect<void, never, FrameLoop.FrameLoop>): void =>
897
+ opts.fork(response.pipe(Effect.catchCause((cause) => Report.failure("wardic.drag: a response failed", cause))))
898
+
899
+ // Subscribed in this order, so the samples hear an event before the grabs. The constraints
900
+ // bound the element's x/y, so they're applied where the gesture's offset meets its base, in
901
+ // `follow` and at the release (ADR-0016, *Amendment (#140)*).
902
+ const drag = yield* DragRecognizer.watchDrag(element, { axis, claim: Bool.not(propagation), lock: config.directionLock === true, listen: listens, adoptable: controlled, threshold, mapPoint: UndefinedOr.map(opts.mapPoint, WardicConfigValue.pointMapOptionOf) }, {
903
+ on: (sample) => respond(onSample(sample)),
904
+ onDefect,
905
+ })
906
+ // A down grabs when the drag listens to it (#431): with `listen: false`, only an adopted one.
907
+ const grabs = Bool.match(listens, { onTrue: () => isGrab, onFalse: () => Predicate.and(isGrab, isAdopted) })
908
+ // The adoptable feed with controls (#507), so a grab hears an adopted down; the plain one without.
909
+ const pointerFeed = Bool.match(controlled, { onTrue: () => InputSource.adoptablePointerOf(input, element), onFalse: () => input.pointer(element) })
910
+ yield* InputFeed.watch(pointerFeed, {
911
+ on: (event) => {
912
+ if (grabs(event)) return respond(onGrab)
913
+ if (isPressEnd(event)) respond(onPressEnd)
914
+ },
915
+ onDefect,
916
+ })
917
+
918
+ // Its controls, subscribed until the Scope closes (#431). A start adopts the pointer, whose down
919
+ // the recognizer claims and the grabs stop a release at, inside the call; then the snap.
920
+ if (Predicate.isNotUndefined(opts.controls)) {
921
+ /** A layout node's snap that no gesture took, ended once its controls end the drag. */
922
+ const unsnapped = (): void => UndefinedOr.match(opts.projection, { onUndefined: constVoid, onDefined: (projection) => void unsnap(state, projection) })
923
+ const unsubscribe = DragControls.subscribeDragControls(opts.controls, {
924
+ start: (event, options) => {
925
+ adoptedThreshold = options.distanceThreshold
926
+ const down = input.adoptPointer(element, event)
927
+ adoptedThreshold = undefined
928
+ if (Option.isNone(down)) return
929
+ if (options.snapToCursor === true) snapTo(opts, state, down.value)
930
+ },
931
+ stop: () => {
932
+ drag.stop()
933
+ unsnapped()
934
+ },
935
+ cancel: () => {
936
+ drag.cancel()
937
+ unsnapped()
938
+ },
939
+ })
940
+ yield* Effect.addFinalizer(() => Effect.sync(unsubscribe))
941
+ }
942
+ }),
943
+ )
944
+
945
+ return {
946
+ takeRelease: (retargeted) => {
947
+ const st = MutableRef.get(state)
948
+ if (Bool.or(st.held, Bool.not(st.releasing))) return false
949
+ if (Bool.not(onReleaseAxis(st.axis, retargeted))) return false
950
+ MutableRef.update(state, (s) => ({ ...s, releasing: false }))
951
+ Deferred.doneUnsafe(taken, Exit.void)
952
+ // A gesture begun since, in this task, keeps `dragging`.
953
+ queueMicrotask(() => {
954
+ if (Bool.not(MutableRef.get(state).held)) opts.setDragging(false)
955
+ })
956
+ return true
957
+ },
958
+ }
959
+ }