@wardic/core 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist/Batch.d.ts +4 -2
  2. package/dist/Batch.d.ts.map +1 -1
  3. package/dist/Batch.js +80 -13
  4. package/dist/Batch.js.map +1 -1
  5. package/dist/DefaultTransition.d.ts.map +1 -1
  6. package/dist/DefaultTransition.js +1 -1
  7. package/dist/DefaultTransition.js.map +1 -1
  8. package/dist/LiveValue.d.ts +61 -0
  9. package/dist/LiveValue.d.ts.map +1 -1
  10. package/dist/LiveValue.js +79 -5
  11. package/dist/LiveValue.js.map +1 -1
  12. package/dist/Machine.d.ts +10 -1
  13. package/dist/Machine.d.ts.map +1 -1
  14. package/dist/Machine.js +18 -1
  15. package/dist/Machine.js.map +1 -1
  16. package/dist/Renderer.js +2 -2
  17. package/dist/Renderer.js.map +1 -1
  18. package/dist/Spans.d.ts +41 -0
  19. package/dist/Spans.d.ts.map +1 -0
  20. package/dist/Spans.js +39 -0
  21. package/dist/Spans.js.map +1 -0
  22. package/dist/index.d.ts +1 -0
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +1 -0
  25. package/dist/index.js.map +1 -1
  26. package/dist/internal/frameLoop.d.ts +1 -1
  27. package/dist/internal/frameLoop.d.ts.map +1 -1
  28. package/dist/internal/frameLoop.js +42 -19
  29. package/dist/internal/frameLoop.js.map +1 -1
  30. package/dist/internal/options.d.ts.map +1 -1
  31. package/dist/internal/options.js +2 -2
  32. package/dist/internal/options.js.map +1 -1
  33. package/dist/internal/tracing.d.ts +34 -14
  34. package/dist/internal/tracing.d.ts.map +1 -1
  35. package/dist/internal/tracing.js +56 -24
  36. package/dist/internal/tracing.js.map +1 -1
  37. package/package.json +1 -1
  38. package/src/Batch.ts +68 -12
  39. package/src/DefaultTransition.ts +1 -1
  40. package/src/LiveValue.ts +129 -5
  41. package/src/Machine.ts +22 -1
  42. package/src/Renderer.ts +2 -2
  43. package/src/Spans.ts +50 -0
  44. package/src/index.ts +1 -0
  45. package/src/internal/frameLoop.ts +44 -20
  46. package/src/internal/options.ts +2 -2
  47. package/src/internal/tracing.ts +82 -28
package/src/Batch.ts CHANGED
@@ -173,6 +173,14 @@ const COUNTED = 8
173
173
  const STARTS = 16
174
174
  /** The start pass drew it this step: its loop keeps it, unstepped, and clears the flag. */
175
175
  const STARTED = 32
176
+ /**
177
+ * Its values changed outside a step (it entered, took a new Subject, a change started it from a given value,
178
+ * or a play, seek, stop or cancel put it somewhere): its next step writes it, though that step may not move
179
+ * it. A step writes a member it moves, or one due; not one a delay or a paused Playhead holds still. A bit
180
+ * of the flags each step reads anyway: the plain list's check stays one compare, and only a member due takes
181
+ * the slow path.
182
+ */
183
+ const DUE = 64
176
184
 
177
185
  /** A Batch's state, which only this module's operations write. */
178
186
  interface State<S> {
@@ -517,7 +525,8 @@ export const enter: {
517
525
  const slot = slotFor(state)
518
526
  state.subjects[slot] = subject
519
527
  // A start a member that left never took isn't this one's.
520
- state.flags[slot] = (state.flags[slot] & ~(STARTS | STARTED)) | LIVE
528
+ // Drawn at its first step, at its start, though a delay holds it there.
529
+ state.flags[slot] = (state.flags[slot] & ~(STARTS | STARTED)) | LIVE | DUE
521
530
  const base = slot * state.stride
522
531
  Arr.forEach(state.keys, (key, k) => {
523
532
  const given = start[key]
@@ -571,6 +580,8 @@ export const replace: {
571
580
  const state = stateOf(self)
572
581
  if ((state.flags[slot] & LIVE) === 0) return
573
582
  state.subjects[slot] = subject
583
+ // The new Subject has nothing drawn on it yet.
584
+ state.flags[slot] = state.flags[slot] | DUE
574
585
  })
575
586
 
576
587
  /** A slot for a new member: one a `leave` freed, else the next, the arrays doubled when they're full. */
@@ -669,6 +680,8 @@ export const retarget: {
669
680
  if ((flag & COUNTED) !== 0) continue
670
681
  const base = slot * stride
671
682
  let moves = false
683
+ // Started from a value given: drawn there at its next step, though a delay holds it there.
684
+ let jumped = false
672
685
  for (let k = 0; k < stride; k++) {
673
686
  const at = base + k
674
687
  let target = targetOf(slot, k)
@@ -680,6 +693,7 @@ export const retarget: {
680
693
  if (!Number.isNaN(jump)) {
681
694
  if (Number.isNaN(target)) target = targets[at]
682
695
  state.positions[at] = jump
696
+ jumped = true
683
697
  state.velocities[at] = 0
684
698
  if (Predicate.isNotUndefined(speedOf)) state.velocities[at] = speedOf(slot, k)
685
699
  targets[at] = Number.NaN
@@ -690,6 +704,7 @@ export const retarget: {
690
704
  }
691
705
  if (!moves) continue
692
706
  flags[slot] = flag | COUNTED
707
+ if (jumped) flags[slot] = flags[slot] | DUE
693
708
  movers[n] = slot
694
709
  n++
695
710
  }
@@ -778,8 +793,10 @@ export const isMoving = <S>(self: Batch<S>): boolean => {
778
793
  }
779
794
 
780
795
  /**
781
- * Steps every moving member by `dt` seconds and writes each one: whether nothing moves now. The run
782
- * on the Frame loop calls it each tick.
796
+ * Steps every moving member by `dt` seconds and writes each one the step moves, or whose values a change
797
+ * outside a step set (an `enter`, a `replace`, a start from a given value, a play, seek, stop or cancel):
798
+ * whether nothing moves now. One a delay or a paused Playhead holds still isn't written. The run on the
799
+ * Frame loop calls it each tick.
783
800
  */
784
801
  // Per frame (ADR-0025, ADR-0030 decisions 10 and 11): `dt` clamped once (#78), then the two moving
785
802
  // lists. Each steps a key by `springValues`' exact step, or `Machine.tween`'s, its spring's matrix
@@ -813,6 +830,7 @@ export const draw = <S>(self: Batch<S>): void => {
813
830
  const { flags, subjects, positions, stride, write } = state
814
831
  for (let slot = 0; slot < state.used; slot++) {
815
832
  if ((flags[slot] & LIVE) === 0) continue
833
+ flags[slot] = flags[slot] & ~DUE
816
834
  write(subjects[slot] as S, positions, slot * stride)
817
835
  }
818
836
  state.flush()
@@ -851,8 +869,10 @@ const stepPlain = <S>(state: State<S>, clamped: number): void => {
851
869
  for (let a = 0; a < count; a++) {
852
870
  const slot = plainList[a]
853
871
  const flag = flags[slot]
872
+ // Whether to write it: this step moved a key, or a change outside a step left it due.
873
+ let moved = false
854
874
  // A member a change gave another transition is the ruled list's now; one that left is dropped here,
855
- // unwritten. One the start pass drew this step is kept, unstepped (#706).
875
+ // unwritten. One the start pass drew this step is kept, unstepped (#706). One due is stepped and written.
856
876
  if (flag !== (LIVE | IN_PLAIN)) {
857
877
  if (flag === (LIVE | IN_PLAIN | STARTED)) {
858
878
  flags[slot] = LIVE | IN_PLAIN
@@ -860,8 +880,14 @@ const stepPlain = <S>(state: State<S>, clamped: number): void => {
860
880
  kept++
861
881
  continue
862
882
  }
863
- if ((flag & IN_PLAIN) !== 0) flags[slot] = flag & ~IN_PLAIN
864
- continue
883
+ if (flag === (LIVE | IN_PLAIN | DUE)) {
884
+ flags[slot] = LIVE | IN_PLAIN
885
+ moved = true
886
+ }
887
+ if (!moved) {
888
+ if ((flag & IN_PLAIN) !== 0) flags[slot] = flag & ~IN_PLAIN
889
+ continue
890
+ }
865
891
  }
866
892
  const base = slot * stride
867
893
  let still = true
@@ -873,16 +899,19 @@ const stepPlain = <S>(state: State<S>, clamped: number): void => {
873
899
  let speed = velocities[at]
874
900
  // Only the part of this tick past the delay moves it: all of it, or, the frame its delay ends, a part.
875
901
  const simDt = Math.max(0, Math.min(clamped, time - delays[at]))
902
+ // A key moves only in these two branches, so they say so: no compare per key (bench:core's `batch`).
876
903
  if (simDt === clamped) {
877
904
  const d = position - goal
878
905
  position = goal + dd * d + dv * speed
879
906
  speed = vd * d + vv * speed
907
+ moved = true
880
908
  }
881
909
  if (simDt > 0 && simDt < clamped) {
882
910
  const part = Spring.matrixFor(spring, simDt)
883
911
  const d = position - goal
884
912
  position = goal + part.dd * d + part.dv * speed
885
913
  speed = part.vd * d + part.vv * speed
914
+ moved = true
886
915
  }
887
916
  const settled = simDt > 0 && Math.abs(speed) <= restSpeeds[at] && Math.abs(position - goal) <= restDeltas[at]
888
917
  if (settled) {
@@ -894,7 +923,8 @@ const stepPlain = <S>(state: State<S>, clamped: number): void => {
894
923
  velocities[at] = speed
895
924
  elapsed[at] = time
896
925
  }
897
- write(subjects[slot] as S, positions, base)
926
+ // One a delay holds still isn't written: an unchanged write is a style write all the same.
927
+ if (moved) write(subjects[slot] as S, positions, base)
898
928
  // At rest: still its member's, in neither list.
899
929
  if (still) {
900
930
  flags[slot] = LIVE
@@ -975,6 +1005,9 @@ const stepRuled = <S>(state: State<S>, clamped: number): void => {
975
1005
  }
976
1006
  const base = slot * stride
977
1007
  let still = true
1008
+ // Whether to write it: this step moved a key, or a change outside a step left it due.
1009
+ let moved = (flag & DUE) !== 0
1010
+ if (moved) flags[slot] = flag & ~DUE
978
1011
  for (let k = 0; k < stride; k++) {
979
1012
  const at = base + k
980
1013
  const rule = rules[at]
@@ -983,7 +1016,9 @@ const stepRuled = <S>(state: State<S>, clamped: number): void => {
983
1016
  elapsed[at] = time
984
1017
  // A tween, out of line.
985
1018
  if (kind === TWEEN) {
1019
+ const was = positions[at]
986
1020
  if (!stepTween(state, at, entries[rule].tween, time, clamped)) still = false
1021
+ if (positions[at] !== was) moved = true
987
1022
  continue
988
1023
  }
989
1024
  // A spring, as `springValues`': only the part of this tick past the delay moves it.
@@ -997,17 +1032,21 @@ const stepRuled = <S>(state: State<S>, clamped: number): void => {
997
1032
  const d = position - goal
998
1033
  position = goal + matrices[mi] * d + matrices[mi + 1] * speed
999
1034
  speed = matrices[mi + 2] * d + matrices[mi + 3] * speed
1035
+ moved = true
1000
1036
  }
1001
1037
  if (simDt > 0 && simDt < clamped) {
1002
1038
  const part = Spring.matrixFor(entries[rule].spring, simDt)
1003
1039
  const d = position - goal
1004
1040
  position = goal + part.dd * d + part.dv * speed
1005
1041
  speed = part.vd * d + part.vv * speed
1042
+ moved = true
1006
1043
  }
1007
- // A spring rests by its thresholds; one that ends at its duration, there (#359).
1044
+ // A spring rests by its thresholds; one that ends at its duration, there (#359), with no step if its
1045
+ // duration is 0: a settle that moves it says so, once a key.
1008
1046
  let settled = simDt > 0 && Math.abs(speed) <= restSpeeds[at] && Math.abs(position - goal) <= restDeltas[at]
1009
1047
  if (kind === TIMED) settled = time - delay >= entries[rule].spring.duration
1010
1048
  if (settled) {
1049
+ if (position !== goal) moved = true
1011
1050
  position = goal
1012
1051
  speed = 0
1013
1052
  }
@@ -1015,7 +1054,7 @@ const stepRuled = <S>(state: State<S>, clamped: number): void => {
1015
1054
  positions[at] = position
1016
1055
  velocities[at] = speed
1017
1056
  }
1018
- write(subjects[slot] as S, positions, base)
1057
+ if (moved) write(subjects[slot] as S, positions, base)
1019
1058
  // At rest: still its member's, in neither list (a member is in one at most).
1020
1059
  if (still) {
1021
1060
  flags[slot] = LIVE
@@ -1051,6 +1090,9 @@ const stepRuledDriven = <S>(state: State<S>): void => {
1051
1090
  }
1052
1091
  const base = slot * stride
1053
1092
  let still = true
1093
+ // Whether to write it: this step moved a key, or a change outside a step left it due. Paused, none moves.
1094
+ let moved = (flag & DUE) !== 0
1095
+ if (moved) flags[slot] = flag & ~DUE
1054
1096
  for (let k = 0; k < stride; k++) {
1055
1097
  const at = base + k
1056
1098
  const rule = rules[at]
@@ -1058,19 +1100,25 @@ const stepRuledDriven = <S>(state: State<S>): void => {
1058
1100
  const dt = dts[rule]
1059
1101
  // Played backwards: its value worked out again where its Playhead has gone back to, out of line.
1060
1102
  if (dt < 0) {
1103
+ const was = positions[at]
1061
1104
  if (!stepBack(state, at, rule, dt)) still = false
1105
+ if (positions[at] !== was) moved = true
1062
1106
  continue
1063
1107
  }
1064
1108
  const time = elapsed[at] + dt
1065
1109
  elapsed[at] = time
1066
1110
  // A tween, out of line.
1067
1111
  if (kind === TWEEN) {
1112
+ const was = positions[at]
1068
1113
  if (!stepTween(state, at, entries[rule].tween, time, dt)) still = false
1114
+ if (positions[at] !== was) moved = true
1069
1115
  continue
1070
1116
  }
1071
1117
  // Keyframes, out of line: only a driven change's.
1072
1118
  if (kind === KEYFRAMES) {
1119
+ const was = positions[at]
1073
1120
  if (!stepKeyframes(state, at, rule, time, dt)) still = false
1121
+ if (positions[at] !== was) moved = true
1074
1122
  continue
1075
1123
  }
1076
1124
  // A spring, as `springValues`': only the part of this tick past the delay moves it.
@@ -1084,12 +1132,14 @@ const stepRuledDriven = <S>(state: State<S>): void => {
1084
1132
  const d = position - goal
1085
1133
  position = goal + matrices[mi] * d + matrices[mi + 1] * speed
1086
1134
  speed = matrices[mi + 2] * d + matrices[mi + 3] * speed
1135
+ moved = true
1087
1136
  }
1088
1137
  if (simDt > 0 && simDt < dt) {
1089
1138
  const part = Spring.matrixFor(entries[rule].spring, simDt)
1090
1139
  const d = position - goal
1091
1140
  position = goal + part.dd * d + part.dv * speed
1092
1141
  speed = part.vd * d + part.vv * speed
1142
+ moved = true
1093
1143
  }
1094
1144
  // A spring rests by its thresholds; one that ends at its duration, there (#359). Held by a paused
1095
1145
  // Playhead, one already on its target is at rest.
@@ -1097,6 +1147,7 @@ const stepRuledDriven = <S>(state: State<S>): void => {
1097
1147
  if (kind === TIMED) settled = time - delay >= entries[rule].spring.duration
1098
1148
  if (dt === 0) settled = speed === 0 && position === goal && time >= delay
1099
1149
  if (settled) {
1150
+ if (position !== goal) moved = true
1100
1151
  position = goal
1101
1152
  speed = 0
1102
1153
  }
@@ -1104,7 +1155,7 @@ const stepRuledDriven = <S>(state: State<S>): void => {
1104
1155
  positions[at] = position
1105
1156
  velocities[at] = speed
1106
1157
  }
1107
- write(subjects[slot] as S, positions, base)
1158
+ if (moved) write(subjects[slot] as S, positions, base)
1108
1159
  // At rest: still its member's, in neither list (a member is in one at most).
1109
1160
  if (still) {
1110
1161
  flags[slot] = LIVE
@@ -1368,7 +1419,10 @@ const startedPartway = <S>(state: State<S>, slot: number, dt: number): boolean =
1368
1419
  if (velocities[at] === 0 && positions[at] === targets[at]) continue
1369
1420
  stepKey(state, at, rules[at], dt)
1370
1421
  }
1371
- if (started) state.write(state.subjects[slot] as S, positions, base)
1422
+ if (started) {
1423
+ state.flags[slot] = state.flags[slot] & ~DUE
1424
+ state.write(state.subjects[slot] as S, positions, base)
1425
+ }
1372
1426
  return started
1373
1427
  }
1374
1428
 
@@ -1912,7 +1966,9 @@ export const play = (self: Animation): number => {
1912
1966
 
1913
1967
  /** The member at `slot` moving in the ruled list, so the next step draws it and rests it: 1 when that sets it off from rest. */
1914
1968
  const moving = <S>(state: State<S>, slot: number): number => {
1915
- const flag = state.flags[slot]
1969
+ // Its values were set outside a step (a play, a seek, a stop or a cancel): drawn at the next, moved or not.
1970
+ const flag = state.flags[slot] | DUE
1971
+ state.flags[slot] = flag
1916
1972
  if ((flag & IN_RULED) !== 0) return 0
1917
1973
  state.flags[slot] = (flag & ~IN_PLAIN) | IN_RULED
1918
1974
  state.ruledList[state.ruledCount] = slot
@@ -58,4 +58,4 @@ export const ONE_TWEEN: KeyframesTransition = { type: "tween" }
58
58
  * program.pipe(Effect.provide(DefaultTransition.layerOneSpring))
59
59
  */
60
60
  // #429.
61
- export const layerOneSpring: Layer.Layer<DefaultTransition> = Layer.succeed(DefaultTransition, { transitionFor: () => ONE_SPRING, keyframes: ONE_TWEEN })
61
+ export const layerOneSpring: Layer.Layer<DefaultTransition> = /*#__PURE__*/ Layer.succeed(DefaultTransition, { transitionFor: () => ONE_SPRING, keyframes: ONE_TWEEN })
package/src/LiveValue.ts CHANGED
@@ -173,7 +173,7 @@ export const transform: {
173
173
  <O extends number | string>(source: Atom.Atom<number>, input: ReadonlyArray<number>, output: ReadonlyArray<O>, options?: TransformOptions<O>): Effect.Effect<Atom.Atom<O>, never, InterpolatorRegistry.InterpolatorRegistry>
174
174
  <O>(source: Atom.Atom<number>, input: ReadonlyArray<number>, output: ReadonlyArray<O>, options: MixedTransformOptions<O>): Effect.Effect<Atom.Atom<O>, never, InterpolatorRegistry.InterpolatorRegistry>
175
175
  } = <O>(source: Atom.Atom<number>, input: ReadonlyArray<number>, output: ReadonlyArray<O>, options: TransformOptions<O> = {}) =>
176
- Effect.map(interpolateWith(input, output, options), (map) => Atom.make((get) => map(get(source))))
176
+ Effect.map(interpolateWith(input, output, options), (map) => transformedFrom(Atom.make((get) => map(get(source))), source, input, output, options))
177
177
 
178
178
  /** `v`'s stretch, as Motion's: the first that ends above it, else the last. Read on each change of the source. */
179
179
  const stretchAt = <O>(stretches: Arr.NonEmptyReadonlyArray<Stretch<O>>, v: number): Stretch<O> =>
@@ -542,13 +542,137 @@ export const scroll: (options?: ScrollSource.Options) => Effect.Effect<ScrollVal
542
542
  const source = yield* ScrollSource.ScrollSource
543
543
  const info = yield* source.scroll(options)
544
544
  return {
545
- scrollX: Atom.make((get) => get(info).x.current),
546
- scrollY: Atom.make((get) => get(info).y.current),
547
- scrollXProgress: Atom.make((get) => get(info).x.progress),
548
- scrollYProgress: Atom.make((get) => get(info).y.progress),
545
+ scrollX: derivedFrom(Atom.make((get) => get(info).x.current), options, "current", "x"),
546
+ scrollY: derivedFrom(Atom.make((get) => get(info).y.current), options, "current", "y"),
547
+ scrollXProgress: derivedFrom(Atom.make((get) => get(info).x.progress), options, "progress", "x"),
548
+ scrollYProgress: derivedFrom(Atom.make((get) => get(info).y.progress), options, "progress", "y"),
549
549
  }
550
550
  })
551
551
 
552
+ /** One `transform` a scroll-derived value went through: its ranges and options, as given. */
553
+ export interface ScrollTransform {
554
+ /** Its input stops, in the terms of the value it maps. */
555
+ readonly input: ReadonlyArray<number>
556
+ /** Its outputs, one a stop. */
557
+ readonly output: ReadonlyArray<unknown>
558
+ /** Its `ease` as given: one for every stretch, or a list, one a stretch; `None` for linear. */
559
+ readonly ease: Option.Option<Easing.Definition | ReadonlyArray<Easing.Definition>>
560
+ /** Whether the ends hold past the first and last stops: `clamp` not `false`. */
561
+ readonly clamp: boolean
562
+ /** Whether a `mixer` blends its outputs, rather than the registry. */
563
+ readonly mixed: boolean
564
+ }
565
+
566
+ /**
567
+ * How a Live value derives from a container's scroll, in no Environment's terms: the scroll it
568
+ * reads, as `scroll` was given it; whether it reads how far the container is scrolled (`current`) or
569
+ * its progress; on which axis; and each `transform` it went through since, in order. An Environment
570
+ * that can run a scroll-linked value with the scroll itself, as the DOM's native scroll timelines do (a
571
+ * **Timeline value**), reads it to decide whether it can, and how.
572
+ */
573
+ export interface ScrollDerivation {
574
+ /** The scroll's options, as `scroll` was given them. */
575
+ readonly scroll: ScrollSource.Options
576
+ /** How far the container is scrolled, or its progress. */
577
+ readonly read: "current" | "progress"
578
+ /** The axis the value is read on. */
579
+ readonly axis: "x" | "y"
580
+ /** The `transform`s since, first to last: none for one of `scroll`'s own values. */
581
+ readonly transforms: ReadonlyArray<ScrollTransform>
582
+ }
583
+
584
+ // Module state (#460, Q7): each of `scroll`'s values' derivation, keyed by the Atom and held weakly, so
585
+ // an Atom gone takes its entry with it.
586
+ const derivations = new WeakMap<Atom.Atom<unknown>, ScrollDerivation>()
587
+
588
+ /**
589
+ * What an Atom derives from, as `transform` or `derivesAs` said: its source, and the transform it adds
590
+ * (none for `derivesAs`'s). Read through when its derivation is asked for, so a source described after
591
+ * the Atom was made, or described again, counts. `sourceDerivation` and `derivation` are the last
592
+ * answer, kept while the source's derivation is the same object.
593
+ */
594
+ interface Link {
595
+ readonly source: Atom.Atom<unknown>
596
+ readonly step: ScrollTransform | undefined
597
+ sourceDerivation: ScrollDerivation | undefined
598
+ derivation: ScrollDerivation | undefined
599
+ }
600
+
601
+ // Module state (#460, Q7): each `transform`'s and each `derivesAs` Atom's link, keyed by the Atom and
602
+ // held weakly, so an Atom gone takes its entry with it. A link for every `transform`, scroll or not: its
603
+ // source may be described after it's made (a binding's, #240's PR 2), so none can be skipped as it's made.
604
+ const links = new WeakMap<Atom.Atom<unknown>, Link>()
605
+
606
+ /** `atom`, described as one of `scroll`'s values. */
607
+ const derivedFrom = <A>(atom: Atom.Atom<A>, scroll: ScrollSource.Options, read: ScrollDerivation["read"], axis: ScrollDerivation["axis"]): Atom.Atom<A> => {
608
+ derivations.set(atom, { scroll, read, axis, transforms: [] })
609
+ return atom
610
+ }
611
+
612
+ /** `atom`, `transform`'s over `source`: linked to it, so it derives as `source` does, then this transform, whenever `source` does. */
613
+ const transformedFrom = <A, O>(atom: Atom.Atom<A>, source: Atom.Atom<number>, input: ReadonlyArray<number>, output: ReadonlyArray<O>, options: TransformOptions<O>): Atom.Atom<A> => {
614
+ const step: ScrollTransform = { input, output, ease: Option.fromUndefinedOr(options.ease), clamp: options.clamp !== false, mixed: Predicate.isNotUndefined(options.mixer) }
615
+ links.set(atom, { source, step, sourceDerivation: undefined, derivation: undefined })
616
+ return atom
617
+ }
618
+
619
+ /**
620
+ * `atom`'s derivation now, read through its links: the same object while nothing it derives from
621
+ * changed. Statements, and nothing built while nothing changed.
622
+ */
623
+ const derivationNow = (atom: Atom.Atom<unknown>): ScrollDerivation | undefined => {
624
+ const own = derivations.get(atom)
625
+ if (Predicate.isNotUndefined(own)) return own
626
+ const link = links.get(atom)
627
+ if (Predicate.isUndefined(link)) return undefined
628
+ const from = derivationNow(link.source)
629
+ if (Predicate.isUndefined(from)) return undefined
630
+ if (Predicate.isUndefined(link.step)) return from
631
+ if (link.sourceDerivation === from) return link.derivation
632
+ link.sourceDerivation = from
633
+ link.derivation = { ...from, transforms: [...from.transforms, link.step] }
634
+ return link.derivation
635
+ }
636
+
637
+ /**
638
+ * Describe `atom` as deriving from a container's scroll as `source` does: for an Atom a binding
639
+ * keeps for a value's life and sets from `source`, made after it, as a `createScroll` does once its
640
+ * elements are there. A `transform` made over `atom` before then derives through it too. Called again,
641
+ * it derives as the new `source` does. `source` mustn't derive from `atom`.
642
+ *
643
+ * @example
644
+ * const own = Atom.make(0)
645
+ * const { scrollYProgress } = yield* LiveValue.scroll({ container })
646
+ * LiveValue.derivesAs(own, scrollYProgress)
647
+ * registry.subscribe(scrollYProgress, (v: number) => registry.set(own, v))
648
+ */
649
+ // #240 (ADR-0043 *Amendment (#240)*): a binding's Atom made before its source, as `createScroll`'s are.
650
+ // Tier 2 (ADR-0008): for binding authors.
651
+ export const derivesAs: {
652
+ (source: Atom.Atom<unknown>): <A>(atom: Atom.Atom<A>) => Atom.Atom<A>
653
+ <A>(atom: Atom.Atom<A>, source: Atom.Atom<unknown>): Atom.Atom<A>
654
+ } = dual(2, <A>(atom: Atom.Atom<A>, source: Atom.Atom<unknown>): Atom.Atom<A> => {
655
+ links.set(atom, { source, step: undefined, sourceDerivation: undefined, derivation: undefined })
656
+ return atom
657
+ })
658
+
659
+ /**
660
+ * How `atom` derives from a container's scroll, now: one of `scroll`'s values, an Atom
661
+ * `derivesAs` one, or a `transform` over either, a `transform` over that, and so on; `None` for any
662
+ * other, a `spring` or a `velocity` of one among them. Read again while nothing it derives from
663
+ * changed, it's the same object. An Environment reads it to run the value on a native scroll timeline
664
+ * where it can (a **Timeline value**).
665
+ *
666
+ * @example
667
+ * const { scrollYProgress } = yield* LiveValue.scroll({ container })
668
+ * const opacity = yield* LiveValue.transform(scrollYProgress, [0, 1], [1, 0])
669
+ * LiveValue.scrollDerivationOf(opacity)
670
+ * // Some({ scroll: { container }, read: "progress", axis: "y", transforms: [{ input: [0, 1], output: [1, 0], … }] })
671
+ */
672
+ // #240 (ADR-0043 *Amendment (#240)*), read through `transform`'s and `derivesAs`'s links. Tier 2
673
+ // (ADR-0008): for Environment authors.
674
+ export const scrollDerivationOf = (atom: Atom.Atom<unknown>): Option.Option<ScrollDerivation> => Option.fromUndefinedOr(derivationNow(atom))
675
+
552
676
  /** A machine of one number a tick, from a run's step. */
553
677
  const numberMachine = (name: string, settles: boolean, start: () => (dt: number) => Frame.Frame<number>): Machine.Machine<Frame.Frame<number>> =>
554
678
  Machine.make({ settles, span: { name, attributes: {} }, start })
package/src/Machine.ts CHANGED
@@ -701,7 +701,8 @@ export const clampDt: {
701
701
 
702
702
  /**
703
703
  * `self` under the span `name`, with `options.attributes`, in place of its own: its runs' traces and
704
- * logs carry that name.
704
+ * logs carry that name, in the spans its runs open when the app gives `Spans.layer` (`WardicSpans`)
705
+ * and a tracer reads them.
705
706
  *
706
707
  * @example
707
708
  * const bounce = Machine.spring(1).pipe(Machine.withSpan("app.bounce", { attributes: { card: id } }))
@@ -1669,3 +1670,23 @@ export const keyframesValues: (
1669
1670
  })
1670
1671
  },
1671
1672
  )
1673
+
1674
+ /** `nextFrame`'s one Frame: settled at its first step. */
1675
+ const SETTLED_AT_ONCE: Frame.Frame<number> = { value: 0, done: true }
1676
+
1677
+ /** `nextFrame`'s sink: it keeps nothing. */
1678
+ const keepNothing = (): undefined => undefined
1679
+
1680
+ /**
1681
+ * Succeeds at the Frame loop's next tick, once its runs have stepped: a run of one Frame, settled at its
1682
+ * first step. It keeps the loop ticking for that one tick, though nothing else runs. Added in a tick's Late
1683
+ * window, it succeeds in that tick's Late round. Interrupted first, it leaves the loop at once.
1684
+ *
1685
+ * @internal
1686
+ */
1687
+ // #714: for a binding's write that must wait a frame on the Frame loop, as a Batch Reorder group's own size
1688
+ // does. Here, not beside `LiveValue.eachFrame`: every app that animates has `Machine`, where importing
1689
+ // `LiveValue` cost a Batch Reorder app 3.6 KB gzipped (ADR-0072, *Amendment (#714)*, decision 5).
1690
+ export const nextFrame: Effect.Effect<void, never, FrameLoop.FrameLoop> = /*#__PURE__*/ Effect.asVoid(
1691
+ /*#__PURE__*/ run(/*#__PURE__*/ make({ settles: true, span: { name: "wardic.nextFrame", attributes: {} }, start: () => () => SETTLED_AT_ONCE }), keepNothing),
1692
+ )
package/src/Renderer.ts CHANGED
@@ -57,11 +57,11 @@ export const applyTo: {
57
57
  stream: Stream.Stream<Frame.Frame<Record<string, unknown>>, E, R>,
58
58
  ) => Effect.Effect<void, E, R | Renderer>
59
59
  <T, E, R>(stream: Stream.Stream<Frame.Frame<Record<string, unknown>>, E, R>, target: T): Effect.Effect<void, E, R | Renderer>
60
- } = dual(
60
+ } = /*#__PURE__*/ dual(
61
61
  // The target is any value: told apart by the stream (#460).
62
62
  Stream.isStream,
63
63
  // Not `Effect.fn`, which always captures a call-site stack (ADR-0040, decision 2).
64
- Effect.fnUntraced(
64
+ /*#__PURE__*/ Effect.fnUntraced(
65
65
  function* <T, E, R>(stream: Stream.Stream<Frame.Frame<Record<string, unknown>>, E, R>, target: T): Effect.fn.Return<void, E, R | Renderer> {
66
66
  yield* Effect.annotateCurrentSpan({ target: typeof target })
67
67
  const renderer = yield* Renderer
package/src/Spans.ts ADDED
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Wardic's spans, an app's to give: with {@link layer} and a tracer installed (the devtools', an
3
+ * OpenTelemetry exporter's), each animation runs in a span named for what it is, and each burst of
4
+ * frames, gesture, layout pass and exit in one of its own. Without `layer`, Wardic opens none, and an
5
+ * app ships none of their code. With `layer` and no tracer, it opens none either.
6
+ *
7
+ * @example
8
+ * const env = Layer.mergeAll(DomLive, devtools, Spans.layer)
9
+ *
10
+ * @module
11
+ */
12
+ // #746, ADR-0067 *Amendment (#746)*, decision 8: the span code was every app's, a tracer or not (Effect
13
+ // 4.1.4's runtime no longer keeps `Tracer` and `Hex`; opening a span brings them). What opens them is a
14
+ // Reference in `internal/tracing.ts`, as `FrameLoop`'s metrics are given (#518): its default opens none.
15
+ import * as Effect from "effect/Effect"
16
+ import * as Layer from "effect/Layer"
17
+ import { dual } from "./internal/dual.ts"
18
+ import * as internal from "./internal/tracing.ts"
19
+
20
+ /** A span's options: its attributes, and whether it's a root, with no parent. */
21
+ export interface Options {
22
+ readonly attributes?: Readonly<Record<string, unknown>> | undefined
23
+ readonly root?: boolean | undefined
24
+ }
25
+
26
+ /**
27
+ * Wardic's spans, given: merged beside an Environment, each animation, burst of frames, gesture,
28
+ * layout pass and exit opens its span under the tracer installed. `@wardic/dom` gives it as
29
+ * `WardicSpans`.
30
+ */
31
+ export const layer: Layer.Layer<never> = /*#__PURE__*/ Layer.succeed(internal.SpansGiven, internal.opening)
32
+
33
+ /**
34
+ * `self` under the span `name`, opened as Wardic's own are: when the app gives {@link layer} and a
35
+ * tracer reads it, with no call-site stack. As it is otherwise. For a binding's own spans.
36
+ *
37
+ * @example
38
+ * const measured = pass.pipe(Spans.withSpan("app.measure", { attributes: { nodes: 3 } }))
39
+ */
40
+ // `dual` as `Effect.withSpan` is, told apart by its first argument.
41
+ export const withSpan: {
42
+ (name: string, options?: Options): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
43
+ <A, E, R>(self: Effect.Effect<A, E, R>, name: string, options?: Options): Effect.Effect<A, E, R>
44
+ } = /*#__PURE__*/ dual(Effect.isEffect, internal.traced)
45
+
46
+ /**
47
+ * Whether a tracer reads the running fiber's spans while the app gives no {@link layer}: what a
48
+ * binding warns of in development, as it builds its Environment.
49
+ */
50
+ export const missing: Effect.Effect<boolean> = internal.missing
package/src/index.ts CHANGED
@@ -51,6 +51,7 @@ export * as Scalar from "./Scalar.ts"
51
51
  export * as ScrollSource from "./ScrollSource.ts"
52
52
  export * as Sequence from "./Sequence.ts"
53
53
  export * as SharedSource from "./SharedSource.ts"
54
+ export * as Spans from "./Spans.ts"
54
55
  export * as Spring from "./Spring.ts"
55
56
  export * as Stagger from "./Stagger.ts"
56
57
  export * as Supersede from "./Supersede.ts"