@wardic/core 1.0.0-rc.1 → 1.0.0-rc.10

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 (114) hide show
  1. package/dist/Batch.d.ts +244 -0
  2. package/dist/Batch.d.ts.map +1 -0
  3. package/dist/Batch.js +1862 -0
  4. package/dist/Batch.js.map +1 -0
  5. package/dist/Clock.d.ts.map +1 -1
  6. package/dist/Clock.js +20 -12
  7. package/dist/Clock.js.map +1 -1
  8. package/dist/DragRecognizer.d.ts +16 -1
  9. package/dist/DragRecognizer.d.ts.map +1 -1
  10. package/dist/DragRecognizer.js +87 -1
  11. package/dist/DragRecognizer.js.map +1 -1
  12. package/dist/Frame.d.ts +13 -0
  13. package/dist/Frame.d.ts.map +1 -1
  14. package/dist/Frame.js +2 -0
  15. package/dist/Frame.js.map +1 -1
  16. package/dist/FrameLoop.d.ts +12 -1
  17. package/dist/FrameLoop.d.ts.map +1 -1
  18. package/dist/FrameLoop.js +23 -1
  19. package/dist/FrameLoop.js.map +1 -1
  20. package/dist/InputSource.d.ts +21 -0
  21. package/dist/InputSource.d.ts.map +1 -1
  22. package/dist/InputSource.js.map +1 -1
  23. package/dist/LiveValue.d.ts +4 -3
  24. package/dist/LiveValue.d.ts.map +1 -1
  25. package/dist/LiveValue.js +7 -5
  26. package/dist/LiveValue.js.map +1 -1
  27. package/dist/Machine.d.ts +18 -4
  28. package/dist/Machine.d.ts.map +1 -1
  29. package/dist/Machine.js +96 -107
  30. package/dist/Machine.js.map +1 -1
  31. package/dist/Playback.d.ts +1 -1
  32. package/dist/Playback.d.ts.map +1 -1
  33. package/dist/Playback.js +18 -8
  34. package/dist/Playback.js.map +1 -1
  35. package/dist/Probe.d.ts +10 -49
  36. package/dist/Probe.d.ts.map +1 -1
  37. package/dist/Probe.js +7 -6
  38. package/dist/Probe.js.map +1 -1
  39. package/dist/Projection.d.ts +4 -3
  40. package/dist/Projection.d.ts.map +1 -1
  41. package/dist/Projection.js +5 -2
  42. package/dist/Projection.js.map +1 -1
  43. package/dist/Recognizer.d.ts +35 -2
  44. package/dist/Recognizer.d.ts.map +1 -1
  45. package/dist/Recognizer.js +211 -16
  46. package/dist/Recognizer.js.map +1 -1
  47. package/dist/Reorder.d.ts +3 -3
  48. package/dist/Reorder.d.ts.map +1 -1
  49. package/dist/Reorder.js +28 -14
  50. package/dist/Reorder.js.map +1 -1
  51. package/dist/ScrollSource.d.ts +62 -0
  52. package/dist/ScrollSource.d.ts.map +1 -0
  53. package/dist/ScrollSource.js +21 -0
  54. package/dist/ScrollSource.js.map +1 -0
  55. package/dist/Sequence.d.ts +7 -2
  56. package/dist/Sequence.d.ts.map +1 -1
  57. package/dist/Sequence.js +8 -2
  58. package/dist/Sequence.js.map +1 -1
  59. package/dist/SharedSource.d.ts +35 -15
  60. package/dist/SharedSource.d.ts.map +1 -1
  61. package/dist/SharedSource.js +84 -108
  62. package/dist/SharedSource.js.map +1 -1
  63. package/dist/index.d.ts +3 -1
  64. package/dist/index.d.ts.map +1 -1
  65. package/dist/index.js +3 -1
  66. package/dist/index.js.map +1 -1
  67. package/dist/internal/frameLoop.d.ts +33 -7
  68. package/dist/internal/frameLoop.d.ts.map +1 -1
  69. package/dist/internal/frameLoop.js +214 -38
  70. package/dist/internal/frameLoop.js.map +1 -1
  71. package/dist/internal/hearing.d.ts +13 -0
  72. package/dist/internal/hearing.d.ts.map +1 -0
  73. package/dist/internal/hearing.js +71 -0
  74. package/dist/internal/hearing.js.map +1 -0
  75. package/dist/internal/keyframes.d.ts +40 -0
  76. package/dist/internal/keyframes.d.ts.map +1 -0
  77. package/dist/internal/keyframes.js +70 -0
  78. package/dist/internal/keyframes.js.map +1 -0
  79. package/dist/internal/playhead.d.ts +16 -0
  80. package/dist/internal/playhead.d.ts.map +1 -0
  81. package/dist/internal/playhead.js +6 -0
  82. package/dist/internal/playhead.js.map +1 -0
  83. package/dist/internal/stepper.d.ts +6 -0
  84. package/dist/internal/stepper.d.ts.map +1 -1
  85. package/dist/internal/stepper.js +16 -0
  86. package/dist/internal/stepper.js.map +1 -1
  87. package/dist/internal/watch.d.ts +6 -0
  88. package/dist/internal/watch.d.ts.map +1 -1
  89. package/dist/internal/watch.js +7 -0
  90. package/dist/internal/watch.js.map +1 -1
  91. package/package.json +1 -1
  92. package/src/Batch.ts +2128 -0
  93. package/src/Clock.ts +29 -17
  94. package/src/DragRecognizer.ts +97 -2
  95. package/src/Frame.ts +16 -0
  96. package/src/FrameLoop.ts +24 -1
  97. package/src/InputSource.ts +25 -0
  98. package/src/LiveValue.ts +9 -7
  99. package/src/Machine.ts +106 -135
  100. package/src/Playback.ts +18 -15
  101. package/src/Probe.ts +16 -58
  102. package/src/Projection.ts +11 -5
  103. package/src/Recognizer.ts +242 -17
  104. package/src/Reorder.ts +39 -18
  105. package/src/ScrollSource.ts +68 -0
  106. package/src/Sequence.ts +20 -5
  107. package/src/SharedSource.ts +113 -146
  108. package/src/index.ts +3 -1
  109. package/src/internal/frameLoop.ts +246 -43
  110. package/src/internal/hearing.ts +76 -0
  111. package/src/internal/keyframes.ts +98 -0
  112. package/src/internal/playhead.ts +19 -0
  113. package/src/internal/stepper.ts +16 -0
  114. package/src/internal/watch.ts +15 -0
package/src/Machine.ts CHANGED
@@ -47,13 +47,12 @@ import type { Covariant } from "effect/Types"
47
47
  import * as UndefinedOr from "effect/UndefinedOr"
48
48
  import * as Clock from "./Clock.ts"
49
49
  import * as Easing from "./Easing.ts"
50
- import type * as Frame from "./Frame.ts"
50
+ import * as Frame from "./Frame.ts"
51
51
  import * as FrameLoop from "./FrameLoop.ts"
52
52
  import * as InterpolatorRegistry from "./InterpolatorRegistry.ts"
53
53
  import { sourceOf } from "./internal/driven.ts"
54
- import { array, boolean, cachedResolver, checked, field, finite, literals, nonEmptyArray, nonNegative, number, optionalField, refine, union } from "./internal/options.ts"
54
+ import { cachedResolver, checked, finite } from "./internal/options.ts"
55
55
  import {
56
- EaseDefinition,
57
56
  initDecay,
58
57
  initTween,
59
58
  type Pass,
@@ -66,6 +65,7 @@ import {
66
65
  runAt,
67
66
  intervalAt,
68
67
  intervalProgress,
68
+ keyframeAt,
69
69
  stepDecay,
70
70
  stopTimesOf,
71
71
  timeIn,
@@ -74,11 +74,11 @@ import {
74
74
  } from "./internal/stepper.ts"
75
75
  import * as Inertia from "./Inertia.ts"
76
76
  import type * as Point from "./Point.ts"
77
- import * as Scalar from "./Scalar.ts"
78
77
  import * as Spring from "./Spring.ts"
79
78
  import type * as Supersede from "./Supersede.ts"
80
79
  import type * as Values from "./Values.ts"
81
80
  import { dual } from "./internal/dual.ts"
81
+ import { ModifierFields, type ResolvedKeyframes, type ResolvedModifiers, resolvedRunOf, resolveKeyframes } from "./internal/keyframes.ts"
82
82
  import { tracedStream } from "./internal/tracing.ts"
83
83
 
84
84
  // --- The data type -----------------------------------------------------------
@@ -734,10 +734,20 @@ const isMultiPhysics = (frame: Frame.Frame<unknown>): frame is Frame.MultiPhysic
734
734
  */
735
735
  const owned = <F extends Frame.Frame<unknown>>(frame: F): F => {
736
736
  if (!Predicate.isObject(frame.value)) return frame
737
- if (isMultiPhysics(frame)) return { ...frame, value: { ...frame.value }, velocity: { ...frame.velocity } }
737
+ if (isMultiPhysics(frame)) {
738
+ // A `springValues` Frame's arrays are its run's, rewritten each tick: its copy is its records alone (#663).
739
+ if (Frame.isSpringValues(frame)) return { value: { ...frame.value }, velocity: { ...frame.velocity }, done: frame.done } as unknown as F
740
+ return { ...frame, value: { ...frame.value }, velocity: { ...frame.velocity } }
741
+ }
738
742
  return { ...frame, value: { ...frame.value } }
739
743
  }
740
744
 
745
+ /**
746
+ * What `toStream` emits for a machine's Frame: a copy its consumer owns, and for `springValues`'
747
+ * Frame its records alone, since its arrays are its run's (#663).
748
+ */
749
+ export type Streamed<F> = F extends Frame.SpringValues ? Frame.MultiPhysics : F
750
+
741
751
  /**
742
752
  * `self` as a `Stream` over the Clock's ticks: a frame a tick, through its settled frame when it settles.
743
753
  * Each subscriber gets a run of its own, and each frame is its to keep. For one value in an Effect
@@ -751,14 +761,14 @@ const owned = <F extends Frame.Frame<unknown>>(frame: F): F => {
751
761
  // emitted, so a consumer that collects Frames never sees one rewritten (ADR-0030, decision 11). A
752
762
  // Stream, not a Channel: measured 2026-11-02, Stream's own layers were ~5% of a pull of the Clock's
753
763
  // ticks, and elements never use `toStream`, so a Channel would gain nothing measurable (#618, M5).
754
- export const toStream = <F extends Frame.Frame<unknown>>(self: Machine<F>): Stream.Stream<F, never, Clock.Clock> =>
764
+ export const toStream = <F extends Frame.Frame<unknown>>(self: Machine<F>): Stream.Stream<Streamed<F>, never, Clock.Clock> =>
755
765
  Stream.unwrap(
756
766
  Effect.gen(function* () {
757
767
  const clock = yield* Clock.Clock
758
768
  yield* Effect.logDebug(`${self.span.name}: subscribe`)
759
769
  const step = self.start()
760
770
  // Per frame (ADR-0025): the step, then the copy, plain calls in one closure built here.
761
- const frames = clock.ticks.pipe(Stream.map((dt: number) => owned(step(dt))))
771
+ const frames = clock.ticks.pipe(Stream.map((dt: number) => owned(step(dt)) as Streamed<F>))
762
772
  return Bool.match(self.settles, {
763
773
  onTrue: () => frames.pipe(Stream.takeUntil(isSettled)),
764
774
  onFalse: () => frames,
@@ -1053,6 +1063,13 @@ export interface InertiaValuesStart {
1053
1063
  export interface SpringValuesStart {
1054
1064
  readonly from?: Readonly<Record<string, number>>
1055
1065
  readonly velocity?: Readonly<Record<string, number>>
1066
+ /**
1067
+ * Which of its Frame's records the run writes each tick: both (the default), or `"value"` alone. A
1068
+ * sink that reads its velocities by index (`velocities`) asks for `"value"`, and the run skips
1069
+ * storing a number under each key a frame. Its `velocity` record then holds its start.
1070
+ */
1071
+ // #663: a keyed store of a number allocates in V8 (about 12 B), an indexed one doesn't.
1072
+ readonly records?: "value"
1056
1073
  }
1057
1074
 
1058
1075
  /** A start's values when it gives none. */
@@ -1155,18 +1172,23 @@ export const inertiaVec = (initial: Point.Point, velocity: Point.Point, options:
1155
1172
  *
1156
1173
  * Each frame carries a per-key `velocity` beside `value`; a settled key reports 0. Seeding `from` and
1157
1174
  * `velocity` from a frame of the run before keeps its velocity when the target changes. Each frame is
1158
- * rewritten every tick: copy one, or its records, to keep it; the settled frame is stable.
1175
+ * rewritten every tick: copy one, or its records, to keep it; the settled frame is stable. Its
1176
+ * `positions` and `velocities` are the same values by index, in `keys`' order, on the run's own
1177
+ * Frame: a sink that reads every frame reads those, and `start.records` stops the run writing a
1178
+ * record it doesn't read. `toStream`'s copies carry the records alone.
1159
1179
  *
1160
1180
  * @example
1161
1181
  * const card = Machine.springValues({ x: 120, y: 40, scale: 1.1 }, Spring.stiff, { from: { x: 0, y: 0, scale: 1 } })
1162
1182
  */
1163
- // Each run steps in place (ADR-0030, decision 11); `toStream` copies each Frame it emits.
1183
+ // Each run steps in place (ADR-0030, decision 11); `toStream` copies each Frame it emits, its records
1184
+ // alone. A run asked for its values alone skips its velocity record (ADR-0030, *Amendment (#663)*).
1164
1185
  export const springValues = (
1165
1186
  target: Readonly<Record<string, number>>,
1166
1187
  options: SpringOptions = NO_OPTIONS,
1167
1188
  start: SpringValuesStart = NO_START,
1168
- ): Machine<Frame.MultiPhysics> => {
1169
- const { from = NO_START_VALUES, velocity = NO_START_VALUES } = start
1189
+ ): Machine<Frame.SpringValues> => {
1190
+ const { from = NO_START_VALUES, velocity = NO_START_VALUES, records } = start
1191
+ const writesVelocity = Predicate.isUndefined(records)
1170
1192
  const keys = Rec.keys(target)
1171
1193
  /** A key's start: its own value, else the options', else 0. */
1172
1194
  const startOf = (values: Record<string, number>, key: string, fallback: number | undefined): number =>
@@ -1196,7 +1218,7 @@ export const springValues = (
1196
1218
  record[key] = xs[i]
1197
1219
  return record
1198
1220
  })
1199
- const frame = { value: recordOf(positions), velocity: recordOf(velocities), done: false }
1221
+ const frame = { value: recordOf(positions), velocity: recordOf(velocities), done: false, keys, positions, velocities }
1200
1222
  const frameValue = frame.value
1201
1223
  const frameVelocity = frame.velocity
1202
1224
  const delay = spring.delay
@@ -1205,39 +1227,76 @@ export const springValues = (
1205
1227
  // spring's fields read as this closure's locals. Each key steps by `Spring.step`'s exact
1206
1228
  // step (#370), written out in place: called per key, `Spring.step` returns a new state and
1207
1229
  // result. The keys share one matrix, worked out once a frame. One spring per key gives the
1208
- // same Frames (machines.test.ts).
1209
- const step = (dt: number): Frame.MultiPhysics => {
1210
- const clamped = Math.min(dt, MAX_DT)
1211
- let done = true
1212
- for (let i = 0; i < count; i++) {
1213
- const goal = targets[i]
1214
- const time = elapsed[i] + clamped
1215
- let position = positions[i]
1216
- let speed = velocities[i]
1217
- // Only the part of this tick past the delay moves it.
1218
- const simDt = Math.max(0, Math.min(clamped, time - delay))
1219
- if (simDt > 0) {
1220
- const m = Spring.matrixFor(spring, simDt)
1221
- const d = position - goal
1222
- position = goal + m.dd * d + m.dv * speed
1223
- speed = m.vd * d + m.vv * speed
1230
+ // same Frames (machines.test.ts). A run that writes its velocity record steps by the loop as it
1231
+ // always was; one that doesn't, by the same loop less that store (#663). One is chosen per run:
1232
+ // a choice per key cost the default path about 4% in V8, the records written after the loop
1233
+ // 21% in JSC, and the velocity record written after it 5% in Chromium (ADR-0030, *Amendment (#663)*).
1234
+ const stepOf = (): ((dt: number) => Frame.SpringValues) => {
1235
+ if (Bool.not(writesVelocity)) {
1236
+ return (dt) => {
1237
+ const clamped = Math.min(dt, MAX_DT)
1238
+ let done = true
1239
+ for (let i = 0; i < count; i++) {
1240
+ const goal = targets[i]
1241
+ const time = elapsed[i] + clamped
1242
+ let position = positions[i]
1243
+ let speed = velocities[i]
1244
+ const simDt = Math.max(0, Math.min(clamped, time - delay))
1245
+ if (simDt > 0) {
1246
+ const m = Spring.matrixFor(spring, simDt)
1247
+ const d = position - goal
1248
+ position = goal + m.dd * d + m.dv * speed
1249
+ speed = m.vd * d + m.vv * speed
1250
+ }
1251
+ const settled = simDt > 0 && Math.abs(speed) <= restSpeeds[i] && Math.abs(position - goal) <= restDeltas[i]
1252
+ if (settled) {
1253
+ position = goal
1254
+ speed = 0
1255
+ }
1256
+ if (!settled) done = false
1257
+ positions[i] = position
1258
+ velocities[i] = speed
1259
+ elapsed[i] = time
1260
+ frameValue[keys[i]] = position
1261
+ }
1262
+ frame.done = done
1263
+ return frame
1224
1264
  }
1225
- const settled = simDt > 0 && Math.abs(speed) <= restSpeeds[i] && Math.abs(position - goal) <= restDeltas[i]
1226
- if (settled) {
1227
- position = goal
1228
- speed = 0
1265
+ }
1266
+ return (dt) => {
1267
+ const clamped = Math.min(dt, MAX_DT)
1268
+ let done = true
1269
+ for (let i = 0; i < count; i++) {
1270
+ const goal = targets[i]
1271
+ const time = elapsed[i] + clamped
1272
+ let position = positions[i]
1273
+ let speed = velocities[i]
1274
+ // Only the part of this tick past the delay moves it.
1275
+ const simDt = Math.max(0, Math.min(clamped, time - delay))
1276
+ if (simDt > 0) {
1277
+ const m = Spring.matrixFor(spring, simDt)
1278
+ const d = position - goal
1279
+ position = goal + m.dd * d + m.dv * speed
1280
+ speed = m.vd * d + m.vv * speed
1281
+ }
1282
+ const settled = simDt > 0 && Math.abs(speed) <= restSpeeds[i] && Math.abs(position - goal) <= restDeltas[i]
1283
+ if (settled) {
1284
+ position = goal
1285
+ speed = 0
1286
+ }
1287
+ if (!settled) done = false
1288
+ positions[i] = position
1289
+ velocities[i] = speed
1290
+ elapsed[i] = time
1291
+ const key = keys[i]
1292
+ frameValue[key] = position
1293
+ frameVelocity[key] = speed
1229
1294
  }
1230
- if (!settled) done = false
1231
- positions[i] = position
1232
- velocities[i] = speed
1233
- elapsed[i] = time
1234
- const key = keys[i]
1235
- frameValue[key] = position
1236
- frameVelocity[key] = speed
1295
+ frame.done = done
1296
+ return frame
1237
1297
  }
1238
- frame.done = done
1239
- return frame
1240
1298
  }
1299
+ const step = stepOf()
1241
1300
  if (Bool.not(Spring.endsOnTime(spring))) return step
1242
1301
  // A spring that ends at its duration (#359) starts each key from rest, as Motion's, never rests
1243
1302
  // (thresholds no key meets), and lands every key at its duration, after its delay. Set here,
@@ -1247,7 +1306,7 @@ export const springValues = (
1247
1306
  velocities[i] = 0
1248
1307
  restDeltas[i] = -1
1249
1308
  restSpeeds[i] = -1
1250
- frameVelocity[keys[i]] = 0
1309
+ if (writesVelocity) frameVelocity[keys[i]] = 0
1251
1310
  }
1252
1311
  const duration = spring.duration
1253
1312
  let time = 0
@@ -1261,7 +1320,7 @@ export const springValues = (
1261
1320
  velocities[i] = 0
1262
1321
  const key = keys[i]
1263
1322
  frameValue[key] = goal
1264
- frameVelocity[key] = 0
1323
+ if (writesVelocity) frameVelocity[key] = 0
1265
1324
  }
1266
1325
  frame.done = true
1267
1326
  return frame
@@ -1272,11 +1331,6 @@ export const springValues = (
1272
1331
 
1273
1332
  // --- The composites ----------------------------------------------------------
1274
1333
 
1275
- const NOT_REPEAT = "Expected a whole number of repeats, 0 or above, or Infinity"
1276
-
1277
- /** A repeat count: a whole number, 0 or above, or `Infinity` to loop forever. */
1278
- const Repeat = refine(number, (n) => n === Infinity || (Number.isInteger(n) && n >= 0) || NOT_REPEAT)
1279
-
1280
1334
  /** `animate`'s bag, resolved: its run (its tween's `delay` 0), and the rest as given. */
1281
1335
  interface ResolvedAnimate extends ResolvedModifiers {
1282
1336
  readonly run: ResolvedRun
@@ -1284,36 +1338,6 @@ interface ResolvedAnimate extends ResolvedModifiers {
1284
1338
  readonly seconds: number
1285
1339
  }
1286
1340
 
1287
- /** The {@link Modifiers}, resolved: as given, defaults filled in. */
1288
- interface ResolvedModifiers {
1289
- readonly repeat: number
1290
- readonly repeatDelay: number
1291
- readonly repeatType: "loop" | "reverse" | "mirror"
1292
- readonly reverse: boolean
1293
- }
1294
-
1295
- /** The {@link Modifiers}' fields (#66): `animate`'s bag and `keyframes`' (#429) add them to a tween's. */
1296
- const ModifierFields = {
1297
- repeat: field(Repeat, 0),
1298
- repeatDelay: field(nonNegative, 0),
1299
- repeatType: field(literals(["loop", "reverse", "mirror"]), "loop"),
1300
- reverse: field(boolean, false),
1301
- }
1302
-
1303
- /** A decoded bag's run: its tween played `repeat + 1` times, with its tween's `delay` 0 (held outside, by `delay`). */
1304
- const resolvedRunOf = (tween: ResolvedTween, modifiers: ResolvedModifiers): ResolvedRun => {
1305
- const cycle = tween.duration + modifiers.repeatDelay
1306
- return {
1307
- tween: { ...tween, delay: 0 },
1308
- repeats: modifiers.repeat,
1309
- gap: modifiers.repeatDelay,
1310
- cycle,
1311
- total: cycle * (modifiers.repeat + 1) - modifiers.repeatDelay,
1312
- reverses: modifiers.repeatType === "reverse",
1313
- mirrors: modifiers.repeatType === "mirror",
1314
- }
1315
- }
1316
-
1317
1341
  /**
1318
1342
  * `animate`'s bag (#66, ADR-0025 *Amendment (#66)*): a tween's fields and the repeat modifiers,
1319
1343
  * checked once a bag. A fractional or negative `repeat`, a negative `repeatDelay`, or a tween field
@@ -1384,56 +1408,6 @@ const animateRun = (a: number, b: number, run: ResolvedRun, repeat: number, span
1384
1408
  })
1385
1409
  }
1386
1410
 
1387
- /** `keyframes`' bag resolved (#429): its run (its tween's `delay` 0), an easing per Interval, and each stop's place in time. */
1388
- interface ResolvedKeyframes extends ResolvedModifiers {
1389
- readonly run: ResolvedRun
1390
- readonly delay: number
1391
- readonly seconds: number
1392
- readonly eases: Arr.NonEmptyReadonlyArray<Easing.Easing>
1393
- readonly times: ReadonlyArray<number> | undefined
1394
- }
1395
-
1396
- /** One `ease` or several (#360): a list is an array whose first item isn't a number, which a bézier's is. */
1397
- const EaseOrList = union([EaseDefinition, nonEmptyArray(EaseDefinition)])
1398
-
1399
- /** Whether `times` run from 0 to 1, none before the one ahead of it. */
1400
- const inOrder = (times: ReadonlyArray<number>): boolean => {
1401
- // The pairs only once every time is in range (#460, Q8).
1402
- if (!Arr.every(times, (time) => Bool.and(time >= 0, time <= 1))) return false
1403
- return Arr.every(Arr.zip(times, Arr.drop(times, 1)), ([before, after]) => after >= before)
1404
- }
1405
-
1406
- const NOT_IN_ORDER = "Expected times from 0 to 1, in order"
1407
-
1408
- /** Each stop's place in time (#429): numbers from 0 to 1, in order. */
1409
- const Times = refine(array(finite), (times) => inOrder(times) || NOT_IN_ORDER)
1410
-
1411
- /**
1412
- * `keyframes`' bag (#66): a tween's fields, its `ease` one Definition or one per Interval (#360), its
1413
- * `times` and the repeat modifiers (#429), checked once a bag.
1414
- */
1415
- const resolveKeyframes: (options: KeyframesOptions) => ResolvedKeyframes = cachedResolver(
1416
- { ...TweenFields, ...ModifierFields, ease: field(EaseOrList, Easing.linear), times: optionalField(Times) },
1417
- (decoded): ResolvedKeyframes => {
1418
- const list: Arr.NonEmptyReadonlyArray<Easing.Definition> = Bool.match(Bool.and(Arr.isArray(decoded.ease), Bool.not(Easing.isBezier(decoded.ease))), {
1419
- onTrue: () => decoded.ease as Arr.NonEmptyReadonlyArray<Easing.Definition>,
1420
- onFalse: (): Arr.NonEmptyReadonlyArray<Easing.Definition> => [decoded.ease as Easing.Definition],
1421
- })
1422
- const tween = resolvedTweenOf({ ...decoded, ease: list[0] })
1423
- return {
1424
- run: resolvedRunOf(tween, decoded),
1425
- delay: tween.delay,
1426
- seconds: tween.seconds,
1427
- eases: Arr.map(list, Easing.resolve),
1428
- times: decoded.times,
1429
- repeat: decoded.repeat,
1430
- repeatDelay: decoded.repeatDelay,
1431
- repeatType: decoded.repeatType,
1432
- reverse: decoded.reverse,
1433
- }
1434
- },
1435
- )
1436
-
1437
1411
  /** Stops a run can go between: a single one twice, so it holds; none, as 0. */
1438
1412
  const stopsOf = <A>(stops: ReadonlyArray<A>, none: A): Arr.NonEmptyReadonlyArray<A> =>
1439
1413
  Arr.match(stops, {
@@ -1480,11 +1454,7 @@ export const keyframes = (stops: ReadonlyArray<number>, options: KeyframesOption
1480
1454
  return (dt): Frame.Frame<number> => {
1481
1455
  elapsed += Math.min(dt, MAX_DT)
1482
1456
  const done = passAt(elapsed, run, reverse, pass)
1483
- const time = timeIn(pass, duration)
1484
- const i = intervalAt(time, stopTimes)
1485
- const eased = eases[i % eases.length](intervalProgress(time, stopTimes, i))
1486
- if (pass.flipped) return { value: Scalar.lerp(at[last - i], at[last - i - 1], eased), done }
1487
- return { value: Scalar.lerp(at[i], at[i + 1], eased), done }
1457
+ return { value: keyframeAt(at, stopTimes, eases, pass, duration), done }
1488
1458
  }
1489
1459
  },
1490
1460
  })
@@ -1596,8 +1566,9 @@ export const springBlend = (
1596
1566
  // Per leg (#460): the combinators called data-first.
1597
1567
  Effect.map(blendersFor(from, to), (blenders) =>
1598
1568
  withSpan(
1599
- map(springValues({ progress: 100 }, options, { from: { progress: 0 }, velocity: { progress: 0 } }), (frame) =>
1600
- blendAt(blenders, UndefinedOr.getOrThrow(frame.value.progress) / 100, frame.done)),
1569
+ // Its one key read by index (#663).
1570
+ map(springValues({ progress: 100 }, options, { from: { progress: 0 }, velocity: { progress: 0 }, records: "value" }), (frame) =>
1571
+ blendAt(blenders, frame.positions[0] / 100, frame.done)),
1601
1572
  "wardic.springBlend",
1602
1573
  { attributes: { keys: blenders.length } },
1603
1574
  ))
package/src/Playback.ts CHANGED
@@ -15,15 +15,17 @@
15
15
  // #433, Motion's playback controls. Machines step by `dt` (a design principle), so a time is reached by
16
16
  // starting a machine partway through (`Machine.startAt`), as an Appear animation's Handoff is
17
17
  // (ADR-0044). Tier 2 (ADR-0008): for binding authors.
18
+ import * as Arr from "effect/Array"
18
19
  import { constant } from "effect/Function"
19
20
  import * as Inspectable from "effect/Inspectable"
20
21
  import { type Pipeable, pipeArguments } from "effect/Pipeable"
21
22
  import * as Predicate from "effect/Predicate"
22
23
  import * as Rec from "effect/Record"
23
- import type * as Frame from "./Frame.ts"
24
+ import * as Frame from "./Frame.ts"
24
25
  import * as Machine from "./Machine.ts"
25
26
  import { measuredAs } from "./internal/driven.ts"
26
27
  import { dual } from "./internal/dual.ts"
28
+ import { tickOf, writableOf } from "./internal/playhead.ts"
27
29
 
28
30
  const TypeId = "~@wardic/core/Playback"
29
31
 
@@ -43,13 +45,6 @@ export interface Playhead extends Pipeable, Inspectable.Inspectable {
43
45
  readonly paused: boolean
44
46
  }
45
47
 
46
- /** A Playhead as the machines and this module's operations write it. */
47
- interface Writable {
48
- time: number
49
- speed: number
50
- paused: boolean
51
- }
52
-
53
48
  const Proto = {
54
49
  [TypeId]: TypeId,
55
50
  ...Inspectable.BaseProto,
@@ -86,7 +81,15 @@ const runFrom = <F extends Frame.Frame<unknown>>(self: Machine.Machine<F>, at: n
86
81
  const heldOf = <F extends Frame.Frame<unknown>>(frame: F): F => {
87
82
  const velocity: unknown = (frame as { readonly velocity?: unknown }).velocity
88
83
  if (Predicate.isNumber(velocity)) return { ...frame, velocity: 0 }
89
- if (Predicate.isObject(velocity)) return { ...frame, velocity: Rec.map(velocity as Record<string, number>, ZERO) }
84
+ if (Predicate.isObject(velocity)) {
85
+ const held = { ...frame, velocity: Rec.map(velocity as Record<string, number>, ZERO) }
86
+ // `springValues`' by index too (#663), its arrays its own: the run rewrites its own when it resumes.
87
+ if (Frame.isSpringValues(held as unknown as Frame.MultiPhysics)) {
88
+ const spring = held as unknown as Frame.SpringValues
89
+ return { ...held, positions: Arr.copy(spring.positions), velocities: Arr.map(spring.velocities, ZERO) }
90
+ }
91
+ return held
92
+ }
90
93
  return frame
91
94
  }
92
95
 
@@ -122,7 +125,7 @@ export const drive: {
122
125
  settles: self.settles,
123
126
  span: self.span,
124
127
  start: () => {
125
- const writable = head as unknown as Writable
128
+ const writable = writableOf(head)
126
129
  const at = head.time
127
130
  let time = at
128
131
  let step = runFrom(self, at)
@@ -144,7 +147,7 @@ export const drive: {
144
147
  return held
145
148
  }
146
149
  held = undefined
147
- const by = Math.max(-Machine.MAX_DT, Math.min(Machine.MAX_DT, dt * head.speed))
150
+ const by = tickOf(head, dt, Machine.MAX_DT)
148
151
  if (by >= 0) {
149
152
  time += by
150
153
  writable.time = time
@@ -173,7 +176,7 @@ export const seek: {
173
176
  (time: number): (head: Playhead) => void
174
177
  (head: Playhead, time: number): void
175
178
  } = dual(2, (head: Playhead, time: number): void => {
176
- const writable = head as unknown as Writable
179
+ const writable = writableOf(head)
177
180
  writable.time = Math.max(0, time)
178
181
  })
179
182
 
@@ -189,20 +192,20 @@ export const setSpeed: {
189
192
  (speed: number): (head: Playhead) => void
190
193
  (head: Playhead, speed: number): void
191
194
  } = dual(2, (head: Playhead, speed: number): void => {
192
- const writable = head as unknown as Writable
195
+ const writable = writableOf(head)
193
196
  writable.speed = speed
194
197
  })
195
198
 
196
199
  /** Holds the machines driven through `head` where they are. */
197
200
  // #618 (F1).
198
201
  export const pause = (head: Playhead): void => {
199
- const writable = head as unknown as Writable
202
+ const writable = writableOf(head)
200
203
  writable.paused = true
201
204
  }
202
205
 
203
206
  /** Plays the machines driven through `head` on from where they're held. */
204
207
  // #618 (F1).
205
208
  export const play = (head: Playhead): void => {
206
- const writable = head as unknown as Writable
209
+ const writable = writableOf(head)
207
210
  writable.paused = false
208
211
  }
package/src/Probe.ts CHANGED
@@ -1,23 +1,23 @@
1
1
  /**
2
2
  * The Probe: what an Environment can tell the engine about what it draws. It measures a target's box;
3
- * holds the viewport's size, whether the user prefers reduced motion, whether the Environment is shown,
4
- * and a container's scroll, live, as Atoms; and observes a target's resizes and whether it's in view.
5
- * Layout animations, in-view gestures, scroll-linked values and reduced motion all read it.
3
+ * holds the viewport's size, whether the user prefers reduced motion and whether the Environment is
4
+ * shown, live, as Atoms; and observes a target's resizes and whether it's in view. Layout animations,
5
+ * in-view gestures and reduced motion all read it. A container's scroll is `ScrollSource`'s.
6
6
  *
7
7
  * @example
8
8
  * import * as Probe from "@wardic/core/Probe"
9
9
  *
10
10
  * const probe = yield* Probe.Probe
11
11
  * const bounds = yield* probe.measure(card)
12
- * const scroll = yield* probe.scroll({ container: list })
13
12
  */
14
- // ADR-0028, decision 11. `prefersReducedMotion`, `pageVisible` and `margin`'s string syntax are the web's
15
- // names: #610.
13
+ // ADR-0028, decision 11. #610 (ADR-0036 *Amendment (#610)*): `visible`, not the web's `pageVisible`;
14
+ // `margin` is `unknown`, the Environment's syntax; `prefersReducedMotion` keeps its name, plain words in
15
+ // any Environment, where `reducedMotion` is the provider's setting. #518: `scroll` moved to `ScrollSource`,
16
+ // which the `scroll` Feature builds, so an app that reads no scroll ships none of its code.
16
17
  import * as Context from "effect/Context"
17
18
  import type * as Effect from "effect/Effect"
18
- import type * as Scope from "effect/Scope"
19
19
  import type * as Stream from "effect/Stream"
20
- import type { Atom } from "effect/reactivity"
20
+ import type * as Atom from "effect/reactivity/Atom"
21
21
  import type * as InputFeed from "./InputFeed.ts"
22
22
 
23
23
  /** A target's box as an Environment measures it, in its coordinates: its corner, its size and its four edges. */
@@ -48,52 +48,17 @@ export interface Viewport {
48
48
  readonly height: number
49
49
  }
50
50
 
51
- /** One axis of a container's scroll. */
52
- // #234, ADR-0043 *Amendment (#233, #234)*.
53
- export interface ScrollAxis {
54
- /** How far the container is scrolled on the axis, in the Environment's units. */
55
- readonly current: number
56
- /** How far it can scroll on the axis: 0 for a container that doesn't overflow. */
57
- readonly length: number
58
- /**
59
- * Progress, 0 to 1: through the container's scroll range, or, on the options' `axis` with a
60
- * `target` or an `offset`, through the offsets the target passes.
61
- */
62
- readonly progress: number
63
- }
64
-
65
- /** A container's scroll on both axes: what the Probe's `scroll` holds. */
66
- export interface Scroll {
67
- /** Across. */
68
- readonly x: ScrollAxis
69
- /** Down. */
70
- readonly y: ScrollAxis
71
- }
72
-
73
- /** What the Probe's `scroll` reads, as Motion's `scroll` options. */
74
- export interface ScrollOptions {
75
- /** The scroll container: the Environment's own when left out. */
76
- readonly container?: unknown
77
- /** An element whose progress through the container `progress` reads, on `axis`. */
78
- readonly target?: unknown
79
- /**
80
- * Where on `axis` the target's progress runs, from 0 to 1, in the Environment's own terms, which it
81
- * reads and resolves, as `@wardic/dom`'s `ScrollOffset` does. Core passes it through, as it does
82
- * `container` and `target`.
83
- */
84
- readonly offset?: unknown
85
- /** The axis `target` and `offset` apply to. Default `"y"`. */
86
- readonly axis?: "x" | "y"
87
- }
88
-
89
51
  /** What an intersection is observed against, as Motion's `viewport`. Default: the viewport, no margin, any of the target. */
90
52
  // #273, ADR-0047.
91
53
  export interface IntersectionOptions {
92
54
  /** The container it's observed against: the viewport when left out. */
93
55
  readonly root?: unknown
94
- /** How far the container's edges are grown, or shrunk, before it counts, in the Environment's own syntax. */
95
- // #610: a string, the web's syntax in `@wardic/dom`.
96
- readonly margin?: string | undefined
56
+ /**
57
+ * How far the container's edges are grown, or shrunk, before it counts, in the Environment's own
58
+ * syntax: core passes it on, as it does `ScrollSource.Options.offset`.
59
+ */
60
+ // #610: `unknown`, as `ScrollSource.Options.offset` is; `@wardic/dom` takes the web's string (`rootMargin`).
61
+ readonly margin?: unknown
97
62
  /** How much of the target must be in it to count: from 0, any of it, to 1, all of it. */
98
63
  readonly amount?: number | undefined
99
64
  }
@@ -119,15 +84,8 @@ export class Probe extends Context.Service<Probe, {
119
84
  * Whether the Environment is shown, live, as Motion's `usePageInView`: `false` while it's hidden, put
120
85
  * in the background, say.
121
86
  */
122
- // #443.
123
- readonly pageVisible: Atom.Atom<boolean>
124
- /**
125
- * A container's scroll, live: an Atom the Environment sets as the container scrolls or resizes, as it
126
- * happens, so a value derived from it is written in the frame that shows the scroll. One listener a
127
- * container, however many read it, held while the Scope is.
128
- */
129
- // #234, ADR-0043 *Amendment (#233, #234)*.
130
- readonly scroll: (options?: ScrollOptions) => Effect.Effect<Atom.Atom<Scroll>, never, Scope.Scope>
87
+ // #443; `pageVisible` until #610.
88
+ readonly visible: Atom.Atom<boolean>
131
89
  /** A target's box each time it resizes. */
132
90
  readonly observeResize: (target: unknown) => Stream.Stream<Bounds>
133
91
  /**
package/src/Projection.ts CHANGED
@@ -264,10 +264,12 @@ export const placeRelativeBox = (relative: Box.Box, parent: Box.Box, anchor: Poi
264
264
  * Which parts of a layout change a node's layout animation animates, as Motion's: `true`, its position
265
265
  * and size; `"position"`, its position, its size taking the new one at once; `"size"`, its size; `"x"` or
266
266
  * `"y"`, that axis; `"preserve-aspect"`, both while its width-to-height ratio stays within 0.2, else its
267
- * position.
267
+ * position; `false`, none: the node has no layout animation of its own, and rides its ancestors'.
268
268
  */
269
- // A **Layout mode** (CONTEXT.md; ADR-0009, *Amendment (#42)*): Motion's `animationType`.
270
- export type LayoutMode = true | "position" | "size" | "preserve-aspect" | "x" | "y"
269
+ // A **Layout mode** (CONTEXT.md; ADR-0009, *Amendment (#42)*): Motion's `animationType`. `false` (#691):
270
+ // a node only a `layoutRoot` or a `layoutScroll` makes, with no `layout` or `layoutId`, as Motion attaches
271
+ // no layout listener to one (create-projection-node `addLayoutListener`).
272
+ export type LayoutMode = boolean | "position" | "size" | "preserve-aspect" | "x" | "y"
271
273
 
272
274
  /** How far two width/height ratios may be apart for `"preserve-aspect"` to animate both (Motion's `isNear(…, 0.2)`). */
273
275
  const ASPECT_NEAR = 0.2
@@ -301,22 +303,26 @@ const movedTo = (box: Box.Box, to: Box.Box): Box.Box => ({
301
303
  * Where a layout animation from `origin` to `target` starts under `mode`, as Motion's: `"size"` moves
302
304
  * `origin` to `target`'s top-left, so only its size animates; `"x"` and `"y"` take the other axis from
303
305
  * `target`; a position-only mode gives `origin` `target`'s size at its own top-left, so only its position
304
- * animates; `true` leaves it as it is. Once, at each layout animation's start.
306
+ * animates; `true` leaves it as it is; `false` gives `target` itself, so nothing animates. Once, at each
307
+ * layout animation's start.
305
308
  */
306
309
  // Motion's `notifyLayoutUpdate`, create-projection-node.ts:2163-2205 at
307
310
  // 16c61222c4166bbef5ff6ab0051192faa79fa8c8.
308
311
  export const shapeOrigin = (mode: LayoutMode, origin: Box.Box, target: Box.Box): Box.Box => shaperOf(mode)(mode, origin, target)
309
312
 
310
- /** How a mode shapes a layout animation's origin: one of four, chosen by a matcher built once. */
313
+ /** How a mode shapes a layout animation's origin: one of five, chosen by a matcher built once. */
311
314
  type Shaper = (mode: LayoutMode, origin: Box.Box, target: Box.Box) => Box.Box
312
315
  const sizeOnly: Shaper = (_, origin, target) => movedTo(origin, target)
313
316
  const xOnly: Shaper = (_, origin, target) => ({ x: origin.x, y: target.y })
314
317
  const yOnly: Shaper = (_, origin, target) => ({ x: target.x, y: origin.y })
318
+ // Layout mode `false` (#691): no layout animation of its own, so it starts where it lands.
319
+ const atTarget: Shaper = (_, __, target) => target
315
320
  const positionOrWhole: Shaper = (mode, origin, target) =>
316
321
  Bool.match(animatesPositionOnly(mode, origin, target), { onTrue: () => withSizeOf(origin, target), onFalse: () => origin })
317
322
  // #618 (H1): `Match.value` built its matcher again for every node's layout animation; this one is built
318
323
  // once, and its shapers are module-level, so a call allocates only the box it returns.
319
324
  const shaperOf: (mode: LayoutMode) => Shaper = /*#__PURE__*/ Match.type<LayoutMode>().pipe(
325
+ Match.when(false, () => atTarget),
320
326
  Match.when("size", () => sizeOnly),
321
327
  Match.when("x", () => xOnly),
322
328
  Match.when("y", () => yOnly),