@wardic/core 1.0.0-rc.7 → 1.0.0-rc.9

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.
package/src/Machine.ts CHANGED
@@ -47,7 +47,7 @@ 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"
@@ -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
@@ -1596,8 +1655,9 @@ export const springBlend = (
1596
1655
  // Per leg (#460): the combinators called data-first.
1597
1656
  Effect.map(blendersFor(from, to), (blenders) =>
1598
1657
  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)),
1658
+ // Its one key read by index (#663).
1659
+ map(springValues({ progress: 100 }, options, { from: { progress: 0 }, velocity: { progress: 0 }, records: "value" }), (frame) =>
1660
+ blendAt(blenders, frame.positions[0] / 100, frame.done)),
1601
1661
  "wardic.springBlend",
1602
1662
  { attributes: { keys: blenders.length } },
1603
1663
  ))
package/src/Playback.ts CHANGED
@@ -15,12 +15,13 @@
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"
@@ -86,7 +87,15 @@ const runFrom = <F extends Frame.Frame<unknown>>(self: Machine.Machine<F>, at: n
86
87
  const heldOf = <F extends Frame.Frame<unknown>>(frame: F): F => {
87
88
  const velocity: unknown = (frame as { readonly velocity?: unknown }).velocity
88
89
  if (Predicate.isNumber(velocity)) return { ...frame, velocity: 0 }
89
- if (Predicate.isObject(velocity)) return { ...frame, velocity: Rec.map(velocity as Record<string, number>, ZERO) }
90
+ if (Predicate.isObject(velocity)) {
91
+ const held = { ...frame, velocity: Rec.map(velocity as Record<string, number>, ZERO) }
92
+ // `springValues`' by index too (#663), its arrays its own: the run rewrites its own when it resumes.
93
+ if (Frame.isSpringValues(held as unknown as Frame.MultiPhysics)) {
94
+ const spring = held as unknown as Frame.SpringValues
95
+ return { ...held, positions: Arr.copy(spring.positions), velocities: Arr.map(spring.velocities, ZERO) }
96
+ }
97
+ return held
98
+ }
90
99
  return frame
91
100
  }
92
101
 
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),