effect-browser 0.3.0-beta.22 → 0.3.0-beta.24

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 (138) hide show
  1. package/README.md +124 -5
  2. package/dist/ActionPort.d.mts +1 -1
  3. package/dist/ActionPort.mjs +1 -1
  4. package/dist/{Browser-Cq2K1FEA.mjs → Browser-CAQFhTkM.mjs} +561 -334
  5. package/dist/Browser-CAQFhTkM.mjs.map +1 -0
  6. package/dist/{Browser-wZhwZSlG.d.mts → Browser-DGR6ZEYo.d.mts} +9 -3
  7. package/dist/Browser.d.mts +1 -1
  8. package/dist/Browser.mjs +1 -1
  9. package/dist/{BrowserEvent-lLqJ3GJS.d.mts → BrowserEvent-C2p-NwuU.d.mts} +30 -6
  10. package/dist/BrowserEvent.d.mts +2 -2
  11. package/dist/BrowserEvent.mjs +20 -1
  12. package/dist/BrowserEvent.mjs.map +1 -1
  13. package/dist/{Change-Ba7INmay.d.mts → Change-CxT1mp2e.d.mts} +13 -2
  14. package/dist/Change.d.mts +1 -1
  15. package/dist/Change.mjs +7 -1
  16. package/dist/Change.mjs.map +1 -1
  17. package/dist/Clock-Du5nP-nK.d.mts +57 -0
  18. package/dist/Clock.d.mts +2 -57
  19. package/dist/Layout.d.mts +2 -0
  20. package/dist/Layout.mjs +81 -0
  21. package/dist/Layout.mjs.map +1 -0
  22. package/dist/Matching-MtcWZ_hA.d.mts +266 -0
  23. package/dist/Matching.d.mts +2 -37
  24. package/dist/Matching.mjs +22 -17
  25. package/dist/Matching.mjs.map +1 -1
  26. package/dist/Overlay-CoGw5tL_.d.mts +27 -0
  27. package/dist/Overlay.d.mts +2 -0
  28. package/dist/Overlay.mjs +30 -0
  29. package/dist/Overlay.mjs.map +1 -0
  30. package/dist/{Page-BF5izr8R.mjs → Page-BaAOshxo.mjs} +3 -1
  31. package/dist/Page-BaAOshxo.mjs.map +1 -0
  32. package/dist/{Page-CvsOEorc.d.mts → Page-CLzh5E9w.d.mts} +158 -6
  33. package/dist/Page.d.mts +2 -2
  34. package/dist/Page.mjs +1 -1
  35. package/dist/PageTime-68UPUZpZ.d.mts +29 -0
  36. package/dist/PageTime.d.mts +2 -0
  37. package/dist/PageTime.mjs +7 -0
  38. package/dist/PageTime.mjs.map +1 -0
  39. package/dist/Pose-2qqGzPvb.d.mts +32 -0
  40. package/dist/Pose.d.mts +2 -0
  41. package/dist/Pose.mjs +44 -0
  42. package/dist/Pose.mjs.map +1 -0
  43. package/dist/Site.d.mts +714 -0
  44. package/dist/Site.mjs +102 -0
  45. package/dist/Site.mjs.map +1 -0
  46. package/dist/{Stillness-CNlvayIo.mjs → Stillness-BwP0RdI4.mjs} +27 -6
  47. package/dist/Stillness-BwP0RdI4.mjs.map +1 -0
  48. package/dist/{Style-AO4V7Czt.mjs → Style-T3ezDYLI.mjs} +25 -2
  49. package/dist/{Style-AO4V7Czt.mjs.map → Style-T3ezDYLI.mjs.map} +1 -1
  50. package/dist/Style.d.mts +15 -2
  51. package/dist/Style.mjs +2 -2
  52. package/dist/Url-B7P-TWwL.d.mts +26 -0
  53. package/dist/Url.d.mts +2 -26
  54. package/dist/{Window-CMddGMt1.d.mts → Window-BDa8MtNP.d.mts} +4 -4
  55. package/dist/Window.d.mts +1 -1
  56. package/dist/{connection-BTeO0Hiz.mjs → connection-C_jWYIwe.mjs} +2 -2
  57. package/dist/{connection-BTeO0Hiz.mjs.map → connection-C_jWYIwe.mjs.map} +1 -1
  58. package/dist/{connection-2KHI4_KN.d.mts → connection-Db-YtlbE.d.mts} +2 -2
  59. package/dist/film/Film.d.mts +265 -0
  60. package/dist/film/Film.mjs +246 -0
  61. package/dist/film/Film.mjs.map +1 -0
  62. package/dist/fold.inpage-CyVZQHZL.d.mts +49 -0
  63. package/dist/index.d.mts +11 -8
  64. package/dist/index.mjs +8 -4
  65. package/dist/moment/Moment.d.mts +5 -5
  66. package/dist/plan/index.d.mts +2 -2
  67. package/dist/plan/index.mjs +2 -2
  68. package/dist/policy/Policy.d.mts +1 -1
  69. package/dist/policy/Policy.mjs +1 -1
  70. package/dist/pose.inpage-CLq7Mfdr.mjs +2042 -0
  71. package/dist/pose.inpage-CLq7Mfdr.mjs.map +1 -0
  72. package/dist/presentation/Presentation.d.mts +8 -1
  73. package/dist/presentation/Presentation.mjs +3 -1
  74. package/dist/presentation/Presentation.mjs.map +1 -1
  75. package/dist/presentation/Stage.d.mts +1 -1
  76. package/dist/providers/Cdp.d.mts +2 -2
  77. package/dist/providers/Cdp.mjs +1 -1
  78. package/dist/providers/Chromium.d.mts +1 -1
  79. package/dist/providers/Chromium.mjs +1 -1
  80. package/dist/stillness/Stillness.d.mts +1 -1
  81. package/dist/stillness/Stillness.mjs +1 -1
  82. package/dist/supervisor/Supervisor.mjs +3 -1
  83. package/dist/supervisor/Supervisor.mjs.map +1 -1
  84. package/dist/testing/index.d.mts +4 -30
  85. package/dist/testing/index.mjs +8 -2
  86. package/dist/testing/index.mjs.map +1 -1
  87. package/dist/workerd/Cdp.d.mts +2 -2
  88. package/dist/workerd/Cdp.mjs +1 -1
  89. package/package.json +35 -5
  90. package/src/Browser.ts +11 -6
  91. package/src/BrowserEvent.ts +19 -0
  92. package/src/Change.ts +7 -1
  93. package/src/Layout.ts +116 -0
  94. package/src/Matching.ts +24 -15
  95. package/src/Overlay.ts +37 -0
  96. package/src/Page.ts +45 -0
  97. package/src/PageTime.ts +47 -0
  98. package/src/Pose.ts +50 -0
  99. package/src/Site.ts +124 -0
  100. package/src/Style.ts +36 -0
  101. package/src/film/Film.ts +384 -0
  102. package/src/index.ts +4 -0
  103. package/src/internal/input/perform.ts +31 -3
  104. package/src/internal/input/pointer.ts +7 -4
  105. package/src/internal/input/targets.ts +1 -1
  106. package/src/internal/page/bridge.ts +65 -33
  107. package/src/internal/page/clock.inpage.ts +306 -0
  108. package/src/internal/page/compose.inpage.ts +101 -0
  109. package/src/internal/page/context.ts +2 -0
  110. package/src/internal/page/identity.ts +6 -5
  111. package/src/internal/page/overlay.inpage.ts +96 -0
  112. package/src/internal/page/overlay.ts +89 -0
  113. package/src/internal/page/page.ts +12 -0
  114. package/src/internal/page/pose.inpage.ts +200 -0
  115. package/src/internal/page/pose.ts +80 -0
  116. package/src/internal/page/time.inpage.ts +49 -0
  117. package/src/internal/page/time.ts +122 -0
  118. package/src/internal/page/viewport.ts +3 -2
  119. package/src/internal/pictures/pictures.ts +93 -32
  120. package/src/internal/reading/layout.inpage.ts +193 -0
  121. package/src/internal/reading/layout.ts +101 -0
  122. package/src/internal/reading/names.inpage.ts +15 -0
  123. package/src/internal/reading/outline.inpage.ts +2 -18
  124. package/src/internal/reading/reading.ts +22 -2
  125. package/src/internal/reading/ready.inpage.ts +18 -23
  126. package/src/internal/reading/ready.ts +23 -3
  127. package/src/internal/reading/text.inpage.ts +1 -14
  128. package/src/internal/timeline/changes.inpage.ts +7 -15
  129. package/src/internal/timeline/changes.ts +14 -1
  130. package/src/internal/timeline/record.inpage.ts +15 -6
  131. package/src/presentation/Presentation.ts +13 -0
  132. package/src/stillness/Pixels.ts +26 -0
  133. package/src/stillness/Stillness.ts +30 -5
  134. package/src/supervisor/runtime.ts +5 -1
  135. package/src/testing/page.ts +5 -0
  136. package/dist/Browser-Cq2K1FEA.mjs.map +0 -1
  137. package/dist/Page-BF5izr8R.mjs.map +0 -1
  138. package/dist/Stillness-CNlvayIo.mjs.map +0 -1
package/src/Site.ts ADDED
@@ -0,0 +1,124 @@
1
+ /**
2
+ * The library's readers for a site that owns its page, called in the site's own bundle on its own
3
+ * document, with no protocol in it. It is the composition the library installs in a page over the
4
+ * protocol, so a site reads the same refs, subjects, outline, text, layout and changes as an agent
5
+ * does, draws in the same overlay, and poses elements the same way.
6
+ *
7
+ * What needs the protocol stays out:
8
+ *
9
+ * - trusted input and the checks that make it safe;
10
+ * - pictures;
11
+ * - held page time;
12
+ * - a record that starts before the page's own scripts.
13
+ *
14
+ * Instead, the site acts through its own app and `note`s each act, so the changes it brings name it
15
+ * as their cause. Time is real, so a pose or a drawing is set again on each animation frame. The
16
+ * record starts when the site calls `record`.
17
+ *
18
+ * Requests and results are the page script's own plain data. Boxes are in viewport CSS pixels, and
19
+ * a change's times are epoch milliseconds, as `performance.timeOrigin + performance.now()`. A
20
+ * request the page cannot serve answers `{ invalid }`, and a ref it no longer has `{ stale }`,
21
+ * rather than throwing. `Layout`, `Change` and `Snapshot` hold their schemas. The script runs in
22
+ * the site's own world, so it shares the globals the site's code may patch.
23
+ */
24
+ import { compose } from "./internal/page/compose.inpage.ts";
25
+ import { overlay } from "./internal/page/overlay.inpage.ts";
26
+ import { pose, poseRequest } from "./internal/page/pose.inpage.ts";
27
+ import { context } from "./internal/reading/context.inpage.ts";
28
+ import { controls } from "./internal/reading/controls.inpage.ts";
29
+ import { expectations, type Want } from "./internal/reading/expect.inpage.ts";
30
+ import { layout } from "./internal/reading/layout.inpage.ts";
31
+ import { elements, names } from "./internal/reading/names.inpage.ts";
32
+ import { outline } from "./internal/reading/outline.inpage.ts";
33
+ import { ready } from "./internal/reading/ready.inpage.ts";
34
+ import { states, subjects } from "./internal/reading/subjects.inpage.ts";
35
+ import { text } from "./internal/reading/text.inpage.ts";
36
+ import { walk } from "./internal/reading/walk.inpage.ts";
37
+ import { changes } from "./internal/timeline/changes.inpage.ts";
38
+ import { fold } from "./internal/timeline/fold.inpage.ts";
39
+ import { history } from "./internal/timeline/history.inpage.ts";
40
+ import { marks } from "./internal/timeline/marks.inpage.ts";
41
+ import { record } from "./internal/timeline/record.inpage.ts";
42
+ import { sight } from "./internal/timeline/sight.inpage.ts";
43
+ import type { Picks } from "./Layout.ts";
44
+ import { match, pickRequests, request } from "./Matching.ts";
45
+ import type { FindQuery } from "./Page.ts";
46
+ import type { Pose } from "./Pose.ts";
47
+ import { addresses } from "./Url.ts";
48
+
49
+ /**
50
+ * The readers, the change record, the overlay and poses for the current document. Make one for
51
+ * each document the site shows, and `stopRecord` the last one's.
52
+ */
53
+ export const make = () => {
54
+ const { named, recorder, url, reads } = compose({
55
+ elements,
56
+ names,
57
+ walk,
58
+ match,
59
+ context,
60
+ subjects,
61
+ states,
62
+ expectations,
63
+ text,
64
+ layout,
65
+ controls,
66
+ fold,
67
+ history,
68
+ sight,
69
+ marks,
70
+ record,
71
+ changes,
72
+ ready,
73
+ outline,
74
+ overlay,
75
+ pose,
76
+ addresses,
77
+ });
78
+
79
+ const own = overlay(named);
80
+
81
+ return {
82
+ /** What a query finds in the viewport: each element's ref, subject, box and state. */
83
+ find: (query: FindQuery) => reads.find(request(query, "viewport")),
84
+ /** Where each pick is, unrounded, with a box for each of the words it asks for. */
85
+ layout: (picks: Picks) => reads.layout(pickRequests(picks)),
86
+ /** Wait up to `millis` until what a query finds in the document is as wanted. */
87
+ until: (query: FindQuery, want: Want, millis: number) =>
88
+ reads.until(request(query, "document"), want, millis),
89
+ /** The page as an outline of its subjects, each control with a ref. */
90
+ snapshot: reads.snapshot,
91
+ /** The text the viewport shows, or one element's. */
92
+ text: reads.text,
93
+ /** Wait up to `millis` for what the page is still loading, quiet for `quietMillis`. */
94
+ ready: (millis: number, quietMillis?: number) => reads.ready(millis, quietMillis),
95
+ /** One task, then a frame; says whether the page painted. */
96
+ settle: reads.settle,
97
+ /** Record what changes from now, until `stopRecord`, or two minutes with no `changes`. */
98
+ record: reads.record,
99
+ stopRecord: reads.stopRecord,
100
+ /** Keep an act the site did through its app at `target` now, for changes to name as cause. */
101
+ note: recorder.note,
102
+ /** What changed in a window of the record, each change with its box. */
103
+ changes: reads.changes,
104
+ /** Draw an SVG document over the viewport, or take it away with null; says where its parts are. */
105
+ draw: reads.overlay,
106
+ /**
107
+ * A layer of the site's own over the viewport, which no reader reads and the record leaves
108
+ * out: a closed shadow root to render into. It takes no pointer, except parts that set
109
+ * `pointer-events: auto`.
110
+ */
111
+ layer: own.mount,
112
+ /** Pose these elements and take the pose off every other. */
113
+ pose: (poses: ReadonlyArray<Pose>) => reads.pose(poses.map(poseRequest)),
114
+ unpose: reads.unpose,
115
+ /** An element's ref, which `pose` and a read's `ref` take. */
116
+ ref: named.refFor,
117
+ /** The element a ref names, while it is in the document. */
118
+ element: named.lookup,
119
+ /** An address without its userinfo or the parameters known to carry secrets. */
120
+ redact: url.redact,
121
+ };
122
+ };
123
+
124
+ export type Site = ReturnType<typeof make>;
package/src/Style.ts CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  } from "effect";
16
16
 
17
17
  import { undispatched, type BrowserError, InvalidRequest, refuse } from "./BrowserError.ts";
18
+ import type { ActionAnnounced } from "./BrowserEvent.ts";
18
19
  import type * as Motion from "./Geometry.ts";
19
20
  import { normal } from "./Geometry.ts";
20
21
  import * as Keys from "./internal/input/keys.ts";
@@ -43,6 +44,12 @@ export interface Style {
43
44
  readonly failFast: boolean;
44
45
  }) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E | BrowserError, R>)
45
46
  | undefined;
47
+ /**
48
+ * Wait for this once an action has announced its target, before its pointer travels there. It
49
+ * holds the page's turn and spends the action's deadline, which bounds it; the target is not
50
+ * found again after it.
51
+ */
52
+ readonly lead?: ((announced: ActionAnnounced) => Effect.Effect<void>) | undefined;
46
53
  /**
47
54
  * Whether viewers watch the input: then the pointer clicks into a field before typing, finds
48
55
  * what lies under it as it scrolls, and scrolls to a target out of view with the wheel.
@@ -262,6 +269,35 @@ const bursts = Effect.fnUntraced(function* (dx: number, dy: number) {
262
269
  return steps;
263
270
  });
264
271
 
272
+ /**
273
+ * A wheel that eases over `durationMillis`: a step each frame, sixty a second, slow at first and
274
+ * last, so a scroll that viewers watch glides to where it goes. A style takes it as its `wheel`;
275
+ * a film scrolls on its own time instead.
276
+ */
277
+ export const eased =
278
+ (durationMillis: number): Style["wheel"] =>
279
+ (dx, dy) => {
280
+ if (!Number.isFinite(durationMillis) || durationMillis < 0)
281
+ return refuse("scroll", "an eased scroll lasts a finite, nonnegative time");
282
+ if (durationMillis > maximumDurationMillis) return Effect.fail(overBudget("scroll"));
283
+ const count = Math.max(1, Math.round((durationMillis * 60) / 1000));
284
+
285
+ // How far along the scroll is at the end of `index` steps, smoothly in and out.
286
+ const along = (index: number) => {
287
+ const x = index / count;
288
+
289
+ return x * x * (3 - 2 * x);
290
+ };
291
+
292
+ return Effect.succeed(
293
+ Array.from({ length: count }, (_, index) => {
294
+ const share = along(index + 1) - along(index);
295
+
296
+ return { dx: dx * share, dy: dy * share, afterMillis: (index * durationMillis) / count };
297
+ }),
298
+ );
299
+ };
300
+
265
301
  /** Input performed for viewers, with this pointer and pace. */
266
302
  export const performed = (options: {
267
303
  readonly pointer: MutableRef.MutableRef<Option.Option<Point>>;
@@ -0,0 +1,384 @@
1
+ /**
2
+ * Filming a page as a function of time: a new screenshot at each time a caller asks for, of the
3
+ * page as it is at that film time, with what the caller draws over it then. Film time comes from
4
+ * a held page (`Page.hold`), which steps to each frame however long the last one took, so a
5
+ * frame costs one call to step the page and one screenshot; from a page that seeks itself; or
6
+ * from real time. The frames are pictures; encoding them is the caller's.
7
+ *
8
+ * Loading takes real time, so a film holds its time while the page loads a new document: the
9
+ * first frame that finds one waits for it to be ready, measures its layout, and says so as a
10
+ * `Passage`, which `at` sees from then on. On real time the film's start moves on by the wait.
11
+ *
12
+ * @since 0.3.0
13
+ */
14
+ import { Clock, Duration, Effect, Schema, Stream } from "effect";
15
+
16
+ import { Action } from "../Action.ts";
17
+ import { type BrowserError, refuse } from "../BrowserError.ts";
18
+ import { monotonicMillis } from "../Clock.ts";
19
+ import { Image } from "../Frame.ts";
20
+ import { Point } from "../Geometry.ts";
21
+ import { Layout, type Picks } from "../Layout.ts";
22
+ import type { Content, Overlay } from "../Overlay.ts";
23
+ import { type Page, ResolvedTarget, type ScreenshotOptions } from "../Page.ts";
24
+ import type { Held, Step } from "../PageTime.ts";
25
+
26
+ /**
27
+ * What a frame shows over the page, and, on held time, where the page is scrolled to then and how
28
+ * its elements are posed, with the page's poses open.
29
+ */
30
+ export type At = Step;
31
+
32
+ /** Frames at `fps` from `from` (0 by default) until before `to`, or at these times, in ms. */
33
+ export type Times =
34
+ | { readonly fps: number; readonly from?: number | undefined; readonly to: number }
35
+ | ReadonlyArray<number>;
36
+
37
+ /**
38
+ * Where film time comes from: a held page, stepped to each frame; a page's own seek, given each
39
+ * frame's time; or real time from the film's start, each frame taken when its time comes.
40
+ */
41
+ export type Time =
42
+ | Held
43
+ | { readonly seek: (t: number) => Effect.Effect<void, BrowserError> }
44
+ | "real";
45
+
46
+ export interface Options<E = never> {
47
+ readonly times: Times;
48
+ readonly time: Time;
49
+ /** What the page shows at a film time, each frame's and each input's, from the passages so far. */
50
+ readonly at?:
51
+ | ((t: number, film: { readonly passages: ReadonlyArray<Passage> }) => Effect.Effect<At, E>)
52
+ | undefined;
53
+ /** What to measure in each new document the film arrives in. */
54
+ readonly picks?: Picks | undefined;
55
+ /**
56
+ * Inputs sent at their film times, in time order, each before the first frame at or after it:
57
+ * the page is moved to its time, and on held time it answers as its time is stepped on. One that
58
+ * fails fails the film.
59
+ */
60
+ readonly inputs?: ReadonlyArray<Contact> | undefined;
61
+ /** The page's overlay, open, that `at`'s drawings go to. */
62
+ readonly overlay?: Overlay | undefined;
63
+ /**
64
+ * How each frame is taken. For frames with more pixels than the layout, paint the page first
65
+ * (`Page.paint`) and take them at `scale: "device"`.
66
+ */
67
+ readonly image?: Pick<ScreenshotOptions, "format" | "quality" | "scale"> | undefined;
68
+ /** How many times a frame's screenshot is taken again after it times out. Defaults to 2. */
69
+ readonly retries?: number | undefined;
70
+ }
71
+
72
+ const Count = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));
73
+ const Millis = Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0));
74
+
75
+ /** An input to send at film time `t`, in ms, with the caller's name for it. */
76
+ export const Contact = Schema.Struct({
77
+ t: Millis,
78
+ action: Action,
79
+ label: Schema.optional(Schema.String),
80
+ });
81
+
82
+ export type Contact = typeof Contact.Type;
83
+
84
+ /** An input as it was sent, at its film time. */
85
+ export class Sent extends Schema.Class<Sent>("effect-browser/Sent")({
86
+ t: Millis,
87
+ label: Schema.optional(Schema.String),
88
+ action: Action,
89
+ /** What a click resolved its target to, before its input. */
90
+ target: Schema.optional(ResolvedTarget),
91
+ /** The page's document after it. */
92
+ document: Count,
93
+ }) {}
94
+
95
+ /** The page's move to a new document, at the film time of the first frame that found it. */
96
+ export class Passage extends Schema.Class<Passage>("effect-browser/Passage")({
97
+ t: Millis,
98
+ /** The address of the document it left. */
99
+ left: Schema.String,
100
+ document: Count,
101
+ url: Schema.String,
102
+ /** Real milliseconds from the film's last step on the page before to the document's being ready. */
103
+ loadedAfter: Millis,
104
+ /** The new document's layout, ready, with the film's `picks`. */
105
+ layout: Layout,
106
+ }) {}
107
+
108
+ /** One frame: its picture at film time `t`, in ms, and what taking it cost in real time. */
109
+ export class Shot extends Schema.Class<Shot>("effect-browser/Shot")({
110
+ index: Count,
111
+ t: Millis,
112
+ image: Image,
113
+ /** The page's document and address after the picture, as `Page.state` gives them. */
114
+ document: Count,
115
+ url: Schema.String,
116
+ /** Where the document was scrolled to, in CSS pixels, on held time. */
117
+ scroll: Schema.optional(Point),
118
+ /** The inputs sent since the frame before. */
119
+ sent: Schema.Array(Sent),
120
+ /** The new document this frame was the first to find. */
121
+ passage: Schema.optional(Passage),
122
+ /** Screenshots that timed out before this one. */
123
+ retries: Count,
124
+ /** Real milliseconds from the frame's start to its picture. */
125
+ took: Millis,
126
+ /**
127
+ * When its picture was taken, in host monotonic milliseconds on the browser's clock, as a
128
+ * `Change`'s times are. On held time, the first shot at or after a change's `at` is the first
129
+ * that shows it; on real time, the page may change while it is pictured.
130
+ */
131
+ at: Schema.Finite,
132
+ }) {}
133
+
134
+ /** What a film cost, from its shots. */
135
+ export class Summary extends Schema.Class<Summary>("effect-browser/FilmSummary")({
136
+ frames: Count,
137
+ retries: Count,
138
+ /** Real milliseconds, all frames' and the slowest one's. */
139
+ took: Millis,
140
+ slowest: Millis,
141
+ /** Every input sent, and every passage, in order. */
142
+ sent: Schema.Array(Sent),
143
+ passages: Schema.Array(Passage),
144
+ }) {}
145
+
146
+ const operation = "Film.frames";
147
+
148
+ function* rated(fps: number, from: number, to: number) {
149
+ const count = Math.round(((to - from) * fps) / 1000);
150
+
151
+ for (let index = 0; index < count; index++) yield from + (index * 1000) / fps;
152
+ }
153
+
154
+ const timesOf = (times: Times): Effect.Effect<Iterable<number>, BrowserError> => {
155
+ if ("fps" in times) {
156
+ const { fps, from = 0, to } = times;
157
+
158
+ return [fps, from, to].every(Number.isFinite) && fps > 0 && from >= 0 && to >= from
159
+ ? Effect.succeed(rated(fps, from, to))
160
+ : refuse(
161
+ operation,
162
+ "a film's rate is positive and runs from a nonnegative time to a later one",
163
+ );
164
+ }
165
+
166
+ return times.every(
167
+ (t, index) => Number.isFinite(t) && t >= 0 && (index === 0 || t >= (times[index - 1] ?? t)),
168
+ )
169
+ ? Effect.succeed(times)
170
+ : refuse(operation, "a film's times are finite, nonnegative and in order");
171
+ };
172
+
173
+ // `move` takes the page to film time `t`, showing `at` there; `drawn` says whether to draw its
174
+ // overlay. On held time, it says where the document is scrolled to. `skip` leaves real time that
175
+ // film time does not count.
176
+ interface Mover {
177
+ readonly move: (
178
+ t: number,
179
+ at: At,
180
+ drawn: boolean,
181
+ ) => Effect.Effect<Point | undefined, BrowserError>;
182
+ readonly skip: (millis: number) => void;
183
+ }
184
+
185
+ const mover = (page: Page, time: Time, overlay: Overlay | undefined): Mover => {
186
+ if (typeof time === "object" && "advance" in time) {
187
+ let now = 0;
188
+
189
+ const move: Mover["move"] = (t, { scroll, poses, overlay: content }, drawn) =>
190
+ time
191
+ .advance(Duration.millis(t - now), {
192
+ scroll,
193
+ poses,
194
+ overlay: drawn ? content : undefined,
195
+ })
196
+ .pipe(
197
+ Effect.tap(() =>
198
+ Effect.sync(() => {
199
+ now = t;
200
+ }),
201
+ ),
202
+ );
203
+
204
+ return { move, skip: () => undefined };
205
+ }
206
+ let start: number | undefined;
207
+
208
+ const arrive =
209
+ time === "real"
210
+ ? (t: number) =>
211
+ Effect.suspend(() => {
212
+ const now = monotonicMillis(page.clock);
213
+
214
+ start ??= now;
215
+
216
+ return Effect.sleep(Duration.millis(Math.max(0, start + t - now))).pipe(
217
+ Effect.provideService(Clock.Clock, page.clock),
218
+ );
219
+ })
220
+ : time.seek;
221
+
222
+ const move: Mover["move"] = (t, at, drawn) =>
223
+ at.scroll === undefined && at.poses === undefined
224
+ ? arrive(t).pipe(
225
+ Effect.andThen(
226
+ drawn && at.overlay !== undefined && overlay !== undefined
227
+ ? overlay.draw(at.overlay)
228
+ : Effect.void,
229
+ ),
230
+ Effect.as(undefined),
231
+ )
232
+ : refuse(operation, "a film scrolls and poses the page only on held time");
233
+
234
+ return {
235
+ move,
236
+ skip: (millis) => {
237
+ if (start !== undefined) start += millis;
238
+ },
239
+ };
240
+ };
241
+
242
+ // A new screenshot, taken again after one that times out, with how many did.
243
+ const picture = (page: Page, image: Options["image"], retries: number) =>
244
+ Effect.gen(function* () {
245
+ let timedOut = 0;
246
+
247
+ const taken = yield* page.screenshot({ ...image, maxAge: 0 }).pipe(
248
+ Effect.tapError((error) =>
249
+ Effect.sync(() => (timedOut += error.reason._tag === "Timeout" ? 1 : 0)),
250
+ ),
251
+ Effect.retry({ times: retries, while: (error) => error.reason._tag === "Timeout" }),
252
+ );
253
+
254
+ return { image: taken, retries: timedOut };
255
+ });
256
+
257
+ const keyOf = (content: Content | undefined) =>
258
+ content === undefined ? undefined : content._tag === "Svg" ? `svg:${content.markup}` : "empty";
259
+
260
+ /**
261
+ * Frames of `page` at `times`, one at a time: each moves the page to its film time, draws what
262
+ * `at` gives there when it changed, and takes a new screenshot, again after one that times out.
263
+ */
264
+ export const frames = <E = never>(
265
+ page: Page,
266
+ options: Options<E>,
267
+ ): Stream.Stream<Shot, BrowserError | E> =>
268
+ Stream.unwrap(
269
+ Effect.gen(function* () {
270
+ const times = yield* timesOf(options.times);
271
+ const retries = options.retries ?? 2;
272
+
273
+ if (!Number.isSafeInteger(retries) || retries < 0)
274
+ return yield* refuse(operation, "retries must be a nonnegative safe integer");
275
+ const inputs = (options.inputs ?? []).toSorted((one, other) => one.t - other.t);
276
+
277
+ if (!inputs.every(({ t }) => Number.isFinite(t) && t >= 0))
278
+ return yield* refuse(operation, "an input's time is finite and nonnegative");
279
+ let next = 0;
280
+ const { move, skip } = mover(page, options.time, options.overlay);
281
+ const now = () => monotonicMillis(page.clock);
282
+ const passages: Array<Passage> = [];
283
+
284
+ const at = (t: number) =>
285
+ options.at === undefined ? Effect.succeed<At>({}) : options.at(t, { passages });
286
+
287
+ let shown: string | undefined;
288
+ let { document: seen, url: was } = yield* page.state;
289
+ let stepped = now();
290
+
291
+ // Take the page to film time `t`, and note when, as a passage's wait counts from it.
292
+ const step = (t: number, showing: At, drawn: boolean) =>
293
+ Effect.tap(move(t, showing, drawn), () => Effect.sync(() => (stepped = now())));
294
+
295
+ // A document the page moved to since the last frame, once it is ready.
296
+ const arrival = (t: number) =>
297
+ Effect.gen(function* () {
298
+ const { document, url } = yield* page.state;
299
+
300
+ if (document === seen) return undefined;
301
+ yield* page.ready();
302
+ const layout = yield* page.layout(options.picks ?? {});
303
+ const loadedAfter = now() - stepped;
304
+
305
+ skip(loadedAfter);
306
+ const passage = new Passage({ t, left: was, document, url, loadedAfter, layout });
307
+
308
+ [seen, was] = [document, url];
309
+ passages.push(passage);
310
+
311
+ return passage;
312
+ });
313
+
314
+ const send = ({ t, action, label }: Contact) =>
315
+ Effect.gen(function* () {
316
+ yield* step(t, yield* at(t), false);
317
+ const done = yield* page.perform(action);
318
+ const { document } = yield* page.state;
319
+ const target = Schema.is(ResolvedTarget)(done) ? done : undefined;
320
+
321
+ return new Sent({ t, label, action, target, document });
322
+ });
323
+
324
+ const shoot = (t: number, index: number) =>
325
+ Effect.gen(function* () {
326
+ const started = now();
327
+ const sent: Array<Sent> = [];
328
+
329
+ for (
330
+ let input = inputs[next];
331
+ input !== undefined && input.t <= t;
332
+ input = inputs[++next]
333
+ )
334
+ sent.push(yield* send(input));
335
+ const passage = yield* arrival(t);
336
+ const showing = yield* at(t);
337
+ const key = keyOf(showing.overlay);
338
+
339
+ if (key !== undefined && options.overlay === undefined)
340
+ return yield* refuse(operation, "drawing needs the page's overlay, open, in `overlay`");
341
+ const scroll = yield* step(t, showing, key !== undefined && key !== shown);
342
+
343
+ shown = key ?? shown;
344
+ const taken = yield* picture(page, options.image, retries);
345
+ const pictured = now();
346
+ const { document, url } = yield* page.state;
347
+
348
+ return new Shot({
349
+ index,
350
+ t,
351
+ image: taken.image,
352
+ document,
353
+ url,
354
+ scroll,
355
+ sent,
356
+ passage,
357
+ retries: taken.retries,
358
+ took: pictured - started,
359
+ at: pictured,
360
+ });
361
+ });
362
+
363
+ return Stream.fromIterable(times).pipe(
364
+ Stream.zipWithIndex,
365
+ Stream.mapEffect(([t, index]) => shoot(t, index)),
366
+ );
367
+ }),
368
+ );
369
+
370
+ /** Run a film's shots, which a caller has usually written out on the way, to what they cost. */
371
+ export const summary = <E, R>(shots: Stream.Stream<Shot, E, R>): Effect.Effect<Summary, E, R> =>
372
+ Stream.runFold(
373
+ shots,
374
+ () => new Summary({ frames: 0, retries: 0, took: 0, slowest: 0, sent: [], passages: [] }),
375
+ (sum, shot) =>
376
+ new Summary({
377
+ frames: sum.frames + 1,
378
+ retries: sum.retries + shot.retries,
379
+ took: sum.took + shot.took,
380
+ slowest: Math.max(sum.slowest, shot.took),
381
+ sent: [...sum.sent, ...shot.sent],
382
+ passages: shot.passage === undefined ? sum.passages : [...sum.passages, shot.passage],
383
+ }),
384
+ );
package/src/index.ts CHANGED
@@ -8,7 +8,11 @@ export * as Change from "./Change.ts";
8
8
  export * as Clock from "./Clock.ts";
9
9
  export * as Frame from "./Frame.ts";
10
10
  export * as Geometry from "./Geometry.ts";
11
+ export * as Layout from "./Layout.ts";
12
+ export * as Overlay from "./Overlay.ts";
11
13
  export * as Page from "./Page.ts";
14
+ export * as PageTime from "./PageTime.ts";
15
+ export * as Pose from "./Pose.ts";
12
16
  export * as Snapshot from "./Snapshot.ts";
13
17
  export * as Style from "./Style.ts";
14
18
  export * as Window from "./Window.ts";
@@ -6,7 +6,7 @@ import { Deferred, Duration, Effect, Exit, Option, Ref } from "effect";
6
6
 
7
7
  import { recorded, nameOf, type Action as ActionValue } from "../../Action.ts";
8
8
  import { BrowserError, PolicyTimeout, Timeout } from "../../BrowserError.ts";
9
- import { Action, Subject } from "../../BrowserEvent.ts";
9
+ import { Action, ActionAnnounced, Subject } from "../../BrowserEvent.ts";
10
10
  import type * as BrowserClock from "../../Clock.ts";
11
11
  import { type Point, redacted, type ResolvedTarget } from "../../Page.ts";
12
12
  import { Current, type Style, validated } from "../../Style.ts";
@@ -32,6 +32,11 @@ export interface InputMarks {
32
32
  readonly at: (point: Point) => Effect.Effect<void>;
33
33
  /** What the input acts on, and for a drag where it ends, as the page names them now. */
34
34
  readonly on: (subject: Named, to?: Named) => Effect.Effect<void>;
35
+ /**
36
+ * The pointer is about to travel to the action's target at `to`: announce what the action acts
37
+ * on, and wait for the style's lead.
38
+ */
39
+ readonly announce: (to: Point) => Effect.Effect<void>;
35
40
  /** The action's text is not bound for a secret field, so its record may keep it. */
36
41
  readonly reveal: Effect.Effect<void>;
37
42
  readonly input: Replies.Run;
@@ -145,6 +150,29 @@ const admission = (page: PageContext, pending: Set<Deferred.Deferred<void>>, nam
145
150
  };
146
151
  };
147
152
 
153
+ /** Announce an action from what it has found, then wait for its style's lead. */
154
+ const announcing =
155
+ (
156
+ page: PageContext,
157
+ name: Action["name"],
158
+ style: Style,
159
+ acted: Ref.Ref<Pick<Action, "subject" | "to" | "box">>,
160
+ ) =>
161
+ (to: Point) =>
162
+ Effect.gen(function* () {
163
+ const event = new ActionAnnounced({
164
+ at: page.now(),
165
+ page: page.id,
166
+ name,
167
+ correlation: yield* Correlation,
168
+ ...(yield* Ref.get(acted)),
169
+ ...to,
170
+ });
171
+
172
+ page.publish(event);
173
+ if (style.lead !== undefined) yield* style.lead(event);
174
+ });
175
+
148
176
  /** Close a run on every exit while preserving its native replies for the page's next input. */
149
177
  const useRun = <A>(
150
178
  page: PageContext,
@@ -219,6 +247,7 @@ export const make = (page: PageContext, sender: Dispatch) => {
219
247
  touched: touch,
220
248
  at: (point: Point) => Ref.set(at, Option.some(point)),
221
249
  on: (subject: Named, to?: Named) => Ref.set(acted, actedOn(subject, to)),
250
+ announce: announcing(page, name, style, acted),
222
251
  reveal: Effect.sync(() => {
223
252
  revealed = true;
224
253
  }),
@@ -362,8 +391,7 @@ export const make = (page: PageContext, sender: Dispatch) => {
362
391
  box,
363
392
  text:
364
393
  info.text === undefined ? undefined : revealed ? info.text.slice(0, 200) : redacted,
365
- x: Option.getOrUndefined(Option.map(point, (p) => p.x)),
366
- y: Option.getOrUndefined(Option.map(point, (p) => p.y)),
394
+ ...Option.match(point, { onNone: () => ({}), onSome: ({ x, y }) => ({ x, y }) }),
367
395
  ok: Exit.isSuccess(exit),
368
396
  dispatched,
369
397
  error: Option.match(failure, {
@@ -18,18 +18,21 @@ export const make = (page: PageContext, dispatch: Dispatch, viewport: Viewport)
18
18
  const { id, pointer, publish, now, span } = page;
19
19
  const { inputClocks, inputCall, dispatchMouse, sendMouse } = dispatch;
20
20
 
21
- // Travel toward a press only prepares the action; a hover is its travel, and a drag holds the
22
- // button it has already pressed.
21
+ // Travel toward a press only prepares the action, as does travel that reveals a target out of
22
+ // view before it is found; a hover is its travel, and a drag holds the button it has already
23
+ // pressed. Travel to the action's own target announces it first.
23
24
  const moveTo = (
24
25
  operation: string,
25
26
  marks: InputMarks,
26
27
  to: Point,
27
28
  cursor?: string,
28
- travel: "approach" | "hover" | "drag" = "approach",
29
+ travel: "approach" | "reveal" | "hover" | "drag" = "approach",
29
30
  ) =>
30
31
  Effect.gen(function* () {
31
32
  const dragging = travel === "drag";
32
33
  const { style } = marks;
34
+
35
+ if (travel === "approach" || travel === "hover") yield* marks.announce(to);
33
36
  const drawn = style.pointer ?? pointer;
34
37
  const size = yield* viewport.viewportFor(operation);
35
38
 
@@ -80,7 +83,7 @@ export const make = (page: PageContext, dispatch: Dispatch, viewport: Viewport)
80
83
  const remaining = at + sample.afterMillis - now();
81
84
 
82
85
  if (remaining > 0) yield* Effect.sleep(Duration.millis(remaining));
83
- yield* travel === "approach" ? marks.touched : marks.sent;
86
+ yield* travel === "approach" || travel === "reveal" ? marks.touched : marks.sent;
84
87
  yield* inputCall(
85
88
  operation,
86
89
  run.send(() =>