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

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 (248) hide show
  1. package/README.md +130 -159
  2. package/dist/Action-24sLOubi.d.mts +110 -0
  3. package/dist/Action.d.mts +2 -0
  4. package/dist/Action.mjs +141 -0
  5. package/dist/Action.mjs.map +1 -0
  6. package/dist/ActionPort.d.mts +15 -0
  7. package/dist/ActionPort.mjs +101 -0
  8. package/dist/ActionPort.mjs.map +1 -0
  9. package/dist/Browser-Cq2K1FEA.mjs +4075 -0
  10. package/dist/Browser-Cq2K1FEA.mjs.map +1 -0
  11. package/dist/{Browser-CiXHT1yO.d.mts → Browser-wZhwZSlG.d.mts} +51 -13
  12. package/dist/Browser.d.mts +2 -2
  13. package/dist/Browser.mjs +1 -1
  14. package/dist/{BrowserError-Coy7EW1N.d.mts → BrowserError-BlcmLVzn.d.mts} +22 -7
  15. package/dist/BrowserError.d.mts +2 -2
  16. package/dist/BrowserError.mjs +46 -9
  17. package/dist/BrowserError.mjs.map +1 -1
  18. package/dist/{BrowserEvent-dq6FRT8w.d.mts → BrowserEvent-lLqJ3GJS.d.mts} +93 -37
  19. package/dist/BrowserEvent.d.mts +2 -2
  20. package/dist/BrowserEvent.mjs +29 -24
  21. package/dist/BrowserEvent.mjs.map +1 -1
  22. package/dist/{Change-DqING3tr.d.mts → Change-Ba7INmay.d.mts} +4 -6
  23. package/dist/Change.d.mts +2 -2
  24. package/dist/Change.mjs +97 -2
  25. package/dist/Change.mjs.map +1 -0
  26. package/dist/Clock.d.mts +57 -0
  27. package/dist/Clock.mjs +80 -0
  28. package/dist/Clock.mjs.map +1 -0
  29. package/dist/{Frame-DkNdjjjr.d.mts → Frame-UEH2hO5T.d.mts} +10 -3
  30. package/dist/Frame.d.mts +2 -2
  31. package/dist/Frame.mjs +10 -2
  32. package/dist/Frame.mjs.map +1 -1
  33. package/dist/{Motion-DND6I-64.d.mts → Geometry-DJx9qrYy.d.mts} +7 -10
  34. package/dist/Geometry.d.mts +2 -0
  35. package/dist/Geometry.mjs +32 -0
  36. package/dist/Geometry.mjs.map +1 -0
  37. package/dist/Matching.d.mts +37 -0
  38. package/dist/Matching.mjs +72 -0
  39. package/dist/Matching.mjs.map +1 -0
  40. package/dist/Page-BF5izr8R.mjs +795 -0
  41. package/dist/Page-BF5izr8R.mjs.map +1 -0
  42. package/dist/{Page-ku6j5t-v.d.mts → Page-CvsOEorc.d.mts} +158 -185
  43. package/dist/Page.d.mts +2 -2
  44. package/dist/Page.mjs +1 -1
  45. package/dist/Released-tURbFmal.d.mts +15 -0
  46. package/dist/{Snapshot-B34hZ4Jl.d.mts → Snapshot-CtBVOtuu.d.mts} +6 -1
  47. package/dist/Snapshot.d.mts +1 -1
  48. package/dist/Snapshot.mjs.map +1 -1
  49. package/dist/Stillness-CNlvayIo.mjs +136 -0
  50. package/dist/Stillness-CNlvayIo.mjs.map +1 -0
  51. package/dist/Style-AO4V7Czt.mjs +357 -0
  52. package/dist/Style-AO4V7Czt.mjs.map +1 -0
  53. package/dist/Style.d.mts +72 -0
  54. package/dist/Style.mjs +2 -0
  55. package/dist/Url.d.mts +26 -0
  56. package/dist/Url.mjs +74 -0
  57. package/dist/Url.mjs.map +1 -0
  58. package/dist/Window-CMddGMt1.d.mts +34 -0
  59. package/dist/Window.d.mts +2 -0
  60. package/dist/Window.mjs +35 -0
  61. package/dist/Window.mjs.map +1 -0
  62. package/dist/{cdp-DL7XfEj5.d.mts → connection-2KHI4_KN.d.mts} +4 -4
  63. package/dist/{cdp-BMpYMWYe.mjs → connection-BTeO0Hiz.mjs} +5 -6
  64. package/dist/connection-BTeO0Hiz.mjs.map +1 -0
  65. package/dist/index.d.mts +15 -18
  66. package/dist/index.mjs +11 -15
  67. package/dist/moment/Moment.d.mts +86 -0
  68. package/dist/{Moment.mjs → moment/Moment.mjs} +44 -38
  69. package/dist/moment/Moment.mjs.map +1 -0
  70. package/dist/{Plan-DJ4hEEna.d.mts → plan/index.d.mts} +169 -64
  71. package/dist/{Plan-BG1WifW2.mjs → plan/index.mjs} +122 -199
  72. package/dist/plan/index.mjs.map +1 -0
  73. package/dist/{Policy-BGWjbqCl.d.mts → policy/Policy.d.mts} +16 -20
  74. package/dist/{Policy.mjs → policy/Policy.mjs} +11 -21
  75. package/dist/policy/Policy.mjs.map +1 -0
  76. package/dist/presentation/Motion.d.mts +12 -0
  77. package/dist/{Motion.mjs → presentation/Motion.mjs} +4 -27
  78. package/dist/presentation/Motion.mjs.map +1 -0
  79. package/dist/presentation/Presentation.d.mts +30 -0
  80. package/dist/presentation/Presentation.mjs +105 -0
  81. package/dist/presentation/Presentation.mjs.map +1 -0
  82. package/dist/{Stage-NHp8N81k.d.mts → presentation/Stage.d.mts} +12 -15
  83. package/dist/{Stage.mjs → presentation/Stage.mjs} +68 -76
  84. package/dist/presentation/Stage.mjs.map +1 -0
  85. package/dist/presentation/feed.d.mts +20 -0
  86. package/dist/presentation/feed.mjs +18 -0
  87. package/dist/presentation/feed.mjs.map +1 -0
  88. package/dist/providers/Cdp.d.mts +9 -0
  89. package/dist/{Cdp.mjs → providers/Cdp.mjs} +3 -8
  90. package/dist/providers/Cdp.mjs.map +1 -0
  91. package/dist/{Chromium.d.mts → providers/Chromium.d.mts} +6 -7
  92. package/dist/{Chromium.mjs → providers/Chromium.mjs} +6 -15
  93. package/dist/providers/Chromium.mjs.map +1 -0
  94. package/dist/providers/Released.d.mts +2 -0
  95. package/dist/providers/Released.mjs +12 -0
  96. package/dist/providers/Released.mjs.map +1 -0
  97. package/dist/stillness/Stillness.d.mts +12 -0
  98. package/dist/stillness/Stillness.mjs +2 -0
  99. package/dist/supervisor/Supervisor.d.mts +104 -0
  100. package/dist/supervisor/Supervisor.mjs +513 -0
  101. package/dist/supervisor/Supervisor.mjs.map +1 -0
  102. package/dist/testing/index.d.mts +337 -0
  103. package/dist/testing/index.mjs +515 -0
  104. package/dist/testing/index.mjs.map +1 -0
  105. package/dist/workerd/Cdp.d.mts +8 -3
  106. package/dist/workerd/Cdp.mjs +2 -7
  107. package/dist/workerd/Cdp.mjs.map +1 -1
  108. package/package.json +86 -36
  109. package/src/Action.ts +127 -0
  110. package/src/ActionPort.ts +128 -0
  111. package/src/Browser.ts +352 -201
  112. package/src/BrowserError.ts +63 -6
  113. package/src/BrowserEvent.ts +26 -25
  114. package/src/Change.ts +0 -2
  115. package/src/{internal/pictures/clock.ts → Clock.ts} +17 -6
  116. package/src/Frame.ts +14 -0
  117. package/src/Geometry.ts +48 -0
  118. package/src/{internal/reading/match.inpage.ts → Matching.ts} +34 -4
  119. package/src/Page.ts +111 -92
  120. package/src/Snapshot.ts +5 -0
  121. package/src/{internal/input/style.ts → Style.ts} +121 -14
  122. package/src/Window.ts +30 -0
  123. package/src/index.ts +15 -6
  124. package/src/internal/input/actions.ts +61 -119
  125. package/src/internal/input/dispatch.ts +3 -3
  126. package/src/internal/input/gate.ts +21 -18
  127. package/src/internal/input/guard.inpage.ts +25 -58
  128. package/src/internal/input/guard.ts +11 -20
  129. package/src/internal/input/keyboard.ts +12 -3
  130. package/src/internal/input/perform.ts +78 -60
  131. package/src/internal/input/pointer.ts +4 -2
  132. package/src/internal/input/settle.ts +7 -5
  133. package/src/internal/input/targets.inpage.ts +1 -1
  134. package/src/internal/input/targets.ts +4 -9
  135. package/src/internal/page/bridge.ts +209 -113
  136. package/src/internal/page/context.ts +12 -59
  137. package/src/internal/page/dialogs.ts +58 -0
  138. package/src/internal/page/lane.ts +14 -15
  139. package/src/internal/page/navigation.ts +50 -42
  140. package/src/internal/page/openings.ts +11 -5
  141. package/src/internal/page/page.ts +44 -81
  142. package/src/internal/page/port.ts +125 -0
  143. package/src/internal/{input → page}/replies.ts +1 -1
  144. package/src/internal/page/request-owner.ts +269 -0
  145. package/src/internal/page/requests.ts +357 -0
  146. package/src/internal/page/scripts.ts +124 -0
  147. package/src/internal/pictures/capture.ts +255 -332
  148. package/src/internal/pictures/dimensions.ts +162 -0
  149. package/src/internal/pictures/frame.ts +68 -0
  150. package/src/internal/pictures/pictures.ts +57 -23
  151. package/src/internal/pictures/quiescence.ts +1 -1
  152. package/src/internal/pictures/state.ts +26 -0
  153. package/src/internal/pictures/transport.ts +21 -4
  154. package/src/internal/reading/controls.inpage.ts +5 -70
  155. package/src/internal/reading/expect.inpage.ts +13 -17
  156. package/src/internal/reading/names.inpage.ts +89 -45
  157. package/src/internal/reading/outline.inpage.ts +6 -7
  158. package/src/internal/reading/reading.ts +144 -176
  159. package/src/internal/reading/ready.inpage.ts +6 -17
  160. package/src/internal/reading/ready.ts +13 -135
  161. package/src/internal/reading/subjects.inpage.ts +83 -43
  162. package/src/internal/reading/walk.inpage.ts +33 -13
  163. package/src/internal/timeline/changes.ts +55 -6
  164. package/src/internal/timeline/events.ts +15 -5
  165. package/src/internal/timeline/history.inpage.ts +1 -1
  166. package/src/internal/timeline/record.inpage.ts +4 -1
  167. package/src/internal/timeline/window.ts +2 -1
  168. package/src/{Moment.ts → moment/Moment.ts} +13 -31
  169. package/src/{internal/timeline → moment}/words.ts +2 -2
  170. package/src/{internal/reading → plan}/choose.ts +3 -3
  171. package/src/plan/data.ts +117 -0
  172. package/src/plan/execute.ts +411 -0
  173. package/src/plan/index.ts +36 -0
  174. package/src/plan/record.ts +71 -0
  175. package/src/plan/targets.ts +71 -0
  176. package/src/{Policy.ts → policy/Policy.ts} +13 -10
  177. package/src/{Motion.ts → presentation/Motion.ts} +12 -46
  178. package/src/presentation/Presentation.ts +176 -0
  179. package/src/{Stage.ts → presentation/Stage.ts} +147 -112
  180. package/src/presentation/feed.ts +21 -0
  181. package/src/{Cdp.ts → providers/Cdp.ts} +2 -2
  182. package/src/{Chromium.ts → providers/Chromium.ts} +7 -10
  183. package/src/providers/Released.ts +13 -0
  184. package/src/{internal/cdp.ts → providers/connection.ts} +2 -2
  185. package/src/{internal/pictures/motion.ts → stillness/Pixels.ts} +1 -1
  186. package/src/stillness/Stillness.ts +166 -0
  187. package/src/supervisor/Supervisor.ts +170 -0
  188. package/src/supervisor/contract.ts +105 -0
  189. package/src/supervisor/lifecycle.ts +272 -0
  190. package/src/supervisor/runtime.ts +234 -0
  191. package/src/testing/TestBrowser.ts +115 -0
  192. package/src/testing/TestPage.ts +10 -0
  193. package/src/testing/evidence.ts +154 -0
  194. package/src/testing/index.ts +3 -0
  195. package/src/testing/model.ts +222 -0
  196. package/src/testing/page.ts +360 -0
  197. package/src/workerd/Cdp.ts +2 -2
  198. package/dist/Browser-CxXUjxOp.mjs +0 -619
  199. package/dist/Browser-CxXUjxOp.mjs.map +0 -1
  200. package/dist/Cdp-CzCRnMlM.d.mts +0 -10
  201. package/dist/Cdp.d.mts +0 -11
  202. package/dist/Cdp.mjs.map +0 -1
  203. package/dist/Change-DNvztNLu.mjs +0 -138
  204. package/dist/Change-DNvztNLu.mjs.map +0 -1
  205. package/dist/Chromium.mjs.map +0 -1
  206. package/dist/Expect-u3P_ENLX.d.mts +0 -62
  207. package/dist/Expect.d.mts +0 -2
  208. package/dist/Expect.mjs +0 -114
  209. package/dist/Expect.mjs.map +0 -1
  210. package/dist/Moment.d.mts +0 -2
  211. package/dist/Moment.mjs.map +0 -1
  212. package/dist/Motion.d.mts +0 -2
  213. package/dist/Motion.mjs.map +0 -1
  214. package/dist/Page-BP02bmNv.mjs +0 -264
  215. package/dist/Page-BP02bmNv.mjs.map +0 -1
  216. package/dist/Plan-BG1WifW2.mjs.map +0 -1
  217. package/dist/Plan.d.mts +0 -2
  218. package/dist/Plan.mjs +0 -2
  219. package/dist/Policy.d.mts +0 -2
  220. package/dist/Policy.mjs.map +0 -1
  221. package/dist/Presentation-DxVikn9_.d.mts +0 -48
  222. package/dist/Presentation.d.mts +0 -2
  223. package/dist/Presentation.mjs +0 -118
  224. package/dist/Presentation.mjs.map +0 -1
  225. package/dist/Stage.d.mts +0 -2
  226. package/dist/Stage.mjs.map +0 -1
  227. package/dist/Supervisor-2mPjIYYs.mjs +0 -676
  228. package/dist/Supervisor-2mPjIYYs.mjs.map +0 -1
  229. package/dist/Supervisor-CfRxbaso.d.mts +0 -317
  230. package/dist/Supervisor.d.mts +0 -2
  231. package/dist/Supervisor.mjs +0 -2
  232. package/dist/cdp-BMpYMWYe.mjs.map +0 -1
  233. package/dist/context-EGhOqjNN.mjs +0 -335
  234. package/dist/context-EGhOqjNN.mjs.map +0 -1
  235. package/dist/page-PTM8m5wC.mjs +0 -4057
  236. package/dist/page-PTM8m5wC.mjs.map +0 -1
  237. package/dist/url-Bsp2y55M.mjs +0 -264
  238. package/dist/url-Bsp2y55M.mjs.map +0 -1
  239. package/dist/workerd/index.d.mts +0 -16
  240. package/dist/workerd/index.mjs +0 -17
  241. package/src/Expect.ts +0 -180
  242. package/src/Plan.ts +0 -674
  243. package/src/Presentation.ts +0 -196
  244. package/src/Supervisor.ts +0 -632
  245. package/src/internal/supervisor/lifecycle.ts +0 -484
  246. package/src/workerd/index.ts +0 -17
  247. /package/src/{internal/page/url.ts → Url.ts} +0 -0
  248. /package/src/{internal/pictures → stillness}/utilsBundle.d.ts +0 -0
@@ -4,46 +4,18 @@
4
4
  *
5
5
  * @since 0.3.0
6
6
  */
7
- import { Effect, Random, Schema } from "effect";
7
+ import { Effect, Random } from "effect";
8
8
 
9
- export const maximumSamples = 2048;
10
- export const maximumDurationMillis = 5000;
9
+ import {
10
+ normal,
11
+ type Planner,
12
+ type Point,
13
+ type Sample,
14
+ maximumDurationMillis,
15
+ } from "../Geometry.ts";
11
16
 
12
- export const Point = Schema.Struct({ x: Schema.Finite, y: Schema.Finite });
13
- export type Point = typeof Point.Type;
14
-
15
- export const Sample = Schema.Struct({
16
- ...Point.fields,
17
- afterMillis: Schema.Finite.check(
18
- Schema.isGreaterThanOrEqualTo(0),
19
- Schema.isLessThanOrEqualTo(maximumDurationMillis),
20
- ),
21
- });
22
-
23
- export type Sample = typeof Sample.Type;
24
-
25
- /** A complete glide: 1 to 2,048 samples whose offsets never decrease. */
26
- export const Plan = Schema.Array(Sample).check(
27
- Schema.isBetweenLength(1, maximumSamples),
28
- Schema.makeFilter(
29
- (samples) =>
30
- samples.every(
31
- (sample, index) => sample.afterMillis >= (samples[index - 1]?.afterMillis ?? 0),
32
- ),
33
- { expected: "nondecreasing sample offsets" },
34
- ),
35
- );
36
-
37
- export type Plan = typeof Plan.Type;
38
-
39
- export interface Service {
40
- /**
41
- * Offsets start when the performer publishes the plan, before its first input. The browser
42
- * decodes the result as a `Plan` whose last sample must be exactly `to`. Glide time has a budget
43
- * of its own, outside the browser's `actionTimeout`, so a drag may glide twice for 5 seconds.
44
- */
45
- readonly plan: (from: Point, to: Point) => Effect.Effect<ReadonlyArray<Sample>>;
46
- }
17
+ export { Point, Sample, Plan, maximumSamples, maximumDurationMillis } from "../Geometry.ts";
18
+ export type { Planner } from "../Geometry.ts";
47
19
 
48
20
  // These are the research's tuned two-stroke parameters. Width scaling was not evaluated, so the
49
21
  // model keeps its 40px reference width rather than implying measured target-size behavior.
@@ -63,12 +35,6 @@ const tuning = {
63
35
 
64
36
  const sampleMillis = 16.7;
65
37
 
66
- const normal = Effect.gen(function* () {
67
- const radius = Math.sqrt(-2 * Math.log(1 - (yield* Random.next)));
68
-
69
- return radius * Math.cos(2 * Math.PI * (yield* Random.next));
70
- });
71
-
72
38
  // Abramowitz-Stegun 7.1.26 supplies erf without another runtime dependency (error < 1.5e-7).
73
39
  const erf = (value: number) => {
74
40
  const x = Math.abs(value);
@@ -83,7 +49,7 @@ const erf = (value: number) => {
83
49
  const cumulative = (time: number, onset: number, mu: number, sigma: number) =>
84
50
  time <= onset ? 0 : (1 + erf((Math.log(time - onset) - mu) / (sigma * Math.SQRT2))) / 2;
85
51
 
86
- const plan: Service["plan"] = Effect.fnUntraced(function* (from, to) {
52
+ const plan: Planner["plan"] = Effect.fnUntraced(function* (from, to) {
87
53
  // Work in normalized coordinates so finite extreme inputs cannot overflow the displacement.
88
54
  const scale = Math.max(1, Math.abs(from.x), Math.abs(from.y), Math.abs(to.x), Math.abs(to.y));
89
55
  const startX = from.x / scale;
@@ -162,4 +128,4 @@ const plan: Service["plan"] = Effect.fnUntraced(function* (from, to) {
162
128
  * The research's tuned two-stroke sigma-lognormal model: a fast stroke toward the target and a
163
129
  * slower one that homes in on it, with a little bend, drawn from `Random`.
164
130
  */
165
- export const lognormal: Service = { plan };
131
+ export const lognormal: Planner = { plan };
@@ -0,0 +1,176 @@
1
+ /**
2
+ * Input performed for viewers. A presenter owns one pointer and pace. Its public page view applies
3
+ * a style to input after approval, so policy holds leave the hand free. Hand acquisition is bounded
4
+ * by the page's action wait and honours failFast; reactions and input use the page owner's clock.
5
+ * @since 0.3.0
6
+ */
7
+ import { type Clock, Deferred, Duration, Effect, MutableRef, Option, Semaphore } from "effect";
8
+
9
+ import { methods } from "../ActionPort.ts";
10
+ import { Busy, refuse, undispatched } from "../BrowserError.ts";
11
+ import { monotonicMillis } from "../Clock.ts";
12
+ import type { Page, Point } from "../Page.ts";
13
+ import * as Style from "../Style.ts";
14
+ import * as Motion from "./Motion.ts";
15
+
16
+ /** How long a person waits before acting, and how fast they type. */
17
+ export interface Pacing {
18
+ readonly expected: Duration.Input;
19
+ readonly surprise: Duration.Input;
20
+ readonly unrelated: Duration.Input;
21
+ /** A word is five characters. */
22
+ readonly wordsPerMinute: number;
23
+ }
24
+
25
+ export const human: Pacing = {
26
+ expected: "280 millis",
27
+ surprise: "600 millis",
28
+ unrelated: "1 second",
29
+ wordsPerMinute: 70,
30
+ };
31
+
32
+ export interface Options {
33
+ readonly pacing?: Pacing | undefined;
34
+ readonly motion?: Motion.Planner | undefined;
35
+ }
36
+
37
+ export type View = Page;
38
+
39
+ export interface Presenter {
40
+ /** The same public page view for the same page. */
41
+ readonly view: (page: Page) => View;
42
+ }
43
+
44
+ const spread = { expected: 0.3, surprise: 0.4, unrelated: 0.4 };
45
+
46
+ /** One hand for the scope; input effects keep their page's authority and failure evidence. */
47
+ export const make = Effect.fn("Presentation.make")(function* (options: Options = {}) {
48
+ const pacing = options.pacing ?? human;
49
+ const medians = { expected: 0, surprise: 0, unrelated: 0 };
50
+
51
+ for (const key of ["expected", "surprise", "unrelated"] as const) {
52
+ const duration = Duration.fromInput(pacing[key]);
53
+
54
+ if (
55
+ Option.isNone(duration) ||
56
+ !Duration.isFinite(duration.value) ||
57
+ Duration.isNegative(duration.value)
58
+ )
59
+ return yield* refuse("presentation", "reaction durations must be finite and nonnegative");
60
+ medians[key] = Duration.toMillis(duration.value);
61
+ }
62
+ if (
63
+ !Number.isFinite(pacing.wordsPerMinute) ||
64
+ pacing.wordsPerMinute <= 0 ||
65
+ Style.keyGap(pacing.wordsPerMinute) < 0
66
+ )
67
+ return yield* refuse("presentation", "wordsPerMinute must be positive and at most 1200");
68
+
69
+ const pointer = MutableRef.make(Option.none<Point>());
70
+ const hand = yield* Semaphore.make(1);
71
+
72
+ const planned = Style.performed({
73
+ pointer,
74
+ motion: options.motion ?? Motion.lognormal,
75
+ wordsPerMinute: pacing.wordsPerMinute,
76
+ });
77
+
78
+ const views = new WeakMap<Page, View>();
79
+
80
+ let last:
81
+ | {
82
+ readonly page: string;
83
+ readonly document: number;
84
+ readonly clock: Clock.Clock;
85
+ readonly at: number;
86
+ }
87
+ | undefined;
88
+
89
+ const view = (page: Page): View => {
90
+ const known = views.get(page);
91
+
92
+ if (known !== undefined) return known;
93
+ const now = () => monotonicMillis(page.clock);
94
+
95
+ const style: Style.Style = {
96
+ ...planned,
97
+ withInput:
98
+ ({ operation, wait, failFast }) =>
99
+ (effect) =>
100
+ Effect.gen(function* () {
101
+ const asked = now();
102
+ const entered = yield* Deferred.make<void>();
103
+
104
+ const busy = () =>
105
+ undispatched(
106
+ operation,
107
+ new Busy({ waitedMillis: Math.round(now() - asked), ahead: 1 }),
108
+ );
109
+
110
+ const use = Effect.gen(function* () {
111
+ yield* Deferred.succeed(entered, undefined);
112
+ const document = (yield* page.state).document;
113
+
114
+ const kind =
115
+ last === undefined || last.page !== page.id || last.clock !== page.clock
116
+ ? "unrelated"
117
+ : last.document === document
118
+ ? "expected"
119
+ : "surprise";
120
+
121
+ const left =
122
+ (last?.clock === page.clock ? last.at : now()) +
123
+ (yield* Style.pause(medians[kind], spread[kind])) -
124
+ now();
125
+
126
+ if (left > 0)
127
+ yield* Effect.sleep(Duration.millis(left)).pipe(
128
+ Effect.withSpan("Presentation.react"),
129
+ );
130
+
131
+ return yield* effect.pipe(
132
+ Effect.ensuring(
133
+ Effect.sync(() => {
134
+ last = { page: page.id, document, clock: page.clock, at: now() };
135
+ }),
136
+ ),
137
+ );
138
+ });
139
+
140
+ if (failFast)
141
+ return yield* hand
142
+ .withPermitsIfAvailable(1)(use)
143
+ .pipe(
144
+ Effect.flatMap((result) => Effect.fromOption(result).pipe(Effect.mapError(busy))),
145
+ );
146
+
147
+ // Only admission has a wait budget. Once admitted, the page owns input's deadline.
148
+ return yield* Effect.raceFirst(
149
+ hand.withPermits(1)(use),
150
+ Deferred.await(entered).pipe(
151
+ Effect.timeoutOrElse({ duration: wait, orElse: () => Effect.fail(busy()) }),
152
+ Effect.andThen(Effect.never),
153
+ ),
154
+ );
155
+ }).pipe(Effect.withSpan("Presentation.hand")),
156
+ };
157
+
158
+ const styled = Style.provide(style);
159
+ const perform: Page["perform"] = (action) => styled(page.perform(action));
160
+
161
+ const performed: View = {
162
+ ...page,
163
+ ...methods(perform),
164
+ perform,
165
+ goto: page.goto,
166
+ back: page.back,
167
+ reload: page.reload,
168
+ };
169
+
170
+ views.set(page, performed);
171
+
172
+ return performed;
173
+ };
174
+
175
+ return { view } satisfies Presenter;
176
+ });
@@ -3,6 +3,7 @@
3
3
  * happens. A prepared cue belongs to its caller's scope, and a committed one to the stage.
4
4
  * Commits serialize, drain old-page input and refuse stale first frames. `shown` distinguishes
5
5
  * confirmed silence, uncertain silence and loss without treating capture silence as evidence.
6
+ * All pages of a stage must share one owning clock; timestamps from unrelated clocks cannot mix.
6
7
  * @since 0.3.0
7
8
  */
8
9
  import {
@@ -22,12 +23,19 @@ import {
22
23
  Stream,
23
24
  } from "effect";
24
25
 
25
- import { type BrowserError, Closed, consequence, InvalidRequest, Timeout } from "./BrowserError.ts";
26
- import type { CaptureStats, Frame, ScreencastOptions } from "./Frame.ts";
27
- import { failWith, refuse, undispatched } from "./internal/page/context.ts";
28
- import { internalsOf } from "./internal/page/page.ts";
29
- import { monotonicMillis } from "./internal/pictures/clock.ts";
30
- import type { Page } from "./Page.ts";
26
+ import {
27
+ type BrowserError,
28
+ consequence,
29
+ failWith,
30
+ InvalidRequest,
31
+ refuse,
32
+ Timeout,
33
+ undispatched,
34
+ } from "../BrowserError.ts";
35
+ import { monotonicMillis } from "../Clock.ts";
36
+ import type { CaptureStats, Frame, ScreencastOptions } from "../Frame.ts";
37
+ import type { Capture, Page } from "../Page.ts";
38
+ import * as Lifecycle from "./feed.ts";
31
39
 
32
40
  export interface Options extends ScreencastOptions {
33
41
  /** Bound for each first-frame wait. Defaults to 5 seconds. */
@@ -65,7 +73,9 @@ export interface Stage {
65
73
  ) => Effect.Effect<Presented, BrowserError>;
66
74
  readonly cue: (page: Page) => Effect.Effect<Cue, BrowserError, Scope.Scope>;
67
75
  /** Paint and, after every finite positive hold without paint, current evidence or loss. */
68
- readonly shown: (options: { readonly hold: Duration.Input }) => Stream.Stream<Shown>;
76
+ readonly shown: (options: {
77
+ readonly hold: Duration.Input;
78
+ }) => Stream.Stream<Shown, BrowserError>;
69
79
  /** Capture-timed current-page paint; use `shown` for output time. Readers retain 16 frames. */
70
80
  readonly frames: Stream.Stream<Frame>;
71
81
  readonly current: Effect.Effect<Option.Option<Page>>;
@@ -74,15 +84,12 @@ export interface Stage {
74
84
  }) => Effect.Effect<Option.Option<CaptureStats>, BrowserError>;
75
85
  }
76
86
 
77
- type FeedState =
78
- | { readonly _tag: "Warming" }
79
- | { readonly _tag: "Ready"; readonly latest: Frame | undefined }
80
- | { readonly _tag: "Stopped" };
81
87
  interface Feed {
82
88
  readonly page: Page;
89
+ capture: Capture | undefined;
83
90
  readonly scope: Scope.Closeable;
84
91
  readonly changed: PubSub.PubSub<Frame>;
85
- state: FeedState;
92
+ state: Lifecycle.State;
86
93
  fiber: Fiber.Fiber<void, BrowserError> | undefined;
87
94
  }
88
95
  interface Paint {
@@ -101,14 +108,8 @@ const unavailable = () =>
101
108
  undispatched("cue", new InvalidRequest({ detail: "the cue's capture is no longer available" }));
102
109
 
103
110
  /** The sole writer of a cue's capture lifecycle; presentation ownership belongs to the stage. */
104
- const update = (
105
- feed: Feed,
106
- input: { readonly _tag: "Paint"; readonly frame: Frame } | { readonly _tag: "Reset" | "Stop" },
107
- ) => {
108
- if (feed.state._tag === "Stopped") return;
109
- if (input._tag === "Stop") feed.state = { _tag: "Stopped" };
110
- else if (input._tag === "Paint") feed.state = { _tag: "Ready", latest: input.frame };
111
- else if (feed.state._tag === "Ready") feed.state = { _tag: "Ready", latest: undefined };
111
+ const update = (feed: Feed, input: Lifecycle.Input) => {
112
+ feed.state = Lifecycle.step(feed.state, input);
112
113
  };
113
114
 
114
115
  const restartable = (error: BrowserError) => {
@@ -126,11 +127,8 @@ const stop = (feed: Feed) =>
126
127
 
127
128
  const frameOf = (feed: Feed) => {
128
129
  const frame = feed.state._tag === "Ready" ? feed.state.latest : undefined;
129
- const internals = internalsOf(feed.page);
130
130
 
131
- return frame !== undefined && (internals === undefined || internals.current(frame))
132
- ? frame
133
- : undefined;
131
+ return frame !== undefined && feed.capture?.isCurrent(frame) ? frame : undefined;
134
132
  };
135
133
 
136
134
  const ended = (feed: Feed): Effect.Effect<never, BrowserError> =>
@@ -147,7 +145,6 @@ const ended = (feed: Feed): Effect.Effect<never, BrowserError> =>
147
145
  const firstOf = (feed: Feed, wait: Duration.Duration) =>
148
146
  Effect.suspend(() => {
149
147
  if (feed.state._tag === "Stopped") return Effect.fail(unavailable());
150
- if (feed.page.playwright.isClosed()) return failWith("cue", new Closed({ cause: "page" }));
151
148
  if (frameOf(feed) !== undefined) return Effect.void;
152
149
 
153
150
  return Effect.raceFirst(
@@ -208,9 +205,7 @@ const confirm = (feed: Feed, frame: Frame) => {
208
205
  if (Option.isSome(failure) && consequence(failure.value).lost !== "nothing")
209
206
  return Effect.fail(failure.value);
210
207
 
211
- return feed.page.playwright.isClosed()
212
- ? failWith("shown", new Closed({ cause: "page" }))
213
- : (internalsOf(feed.page)?.confirm(frame) ?? Effect.succeed(false));
208
+ return feed.capture?.confirm(frame) ?? Effect.succeed(false);
214
209
  };
215
210
 
216
211
  /** Each subscriber confirms silence; a new frame or commit invalidates a probe. */
@@ -220,19 +215,11 @@ const evidence = (
220
215
  | { readonly feed: Feed; readonly frame: Frame; readonly changedAt: number }
221
216
  | undefined,
222
217
  now: () => number,
223
- hold: Duration.Input,
224
- ): Stream.Stream<Shown> =>
218
+ hold: Duration.Duration,
219
+ ): Stream.Stream<Shown, BrowserError> =>
225
220
  Stream.unwrap(
226
221
  Effect.sync(() => {
227
- const parsed = Duration.fromInput(hold);
228
-
229
- if (
230
- Option.isNone(parsed) ||
231
- !Duration.isFinite(parsed.value) ||
232
- !Duration.isPositive(parsed.value)
233
- )
234
- throw new RangeError("shown hold must be a finite positive duration");
235
- const millis = Duration.toMillis(parsed.value);
222
+ const millis = Duration.toMillis(hold);
236
223
  let checkedAt = now();
237
224
  let shownAt = Number.NEGATIVE_INFINITY;
238
225
  let seen: Pick<Paint, "feed" | "frame"> | undefined = current();
@@ -316,7 +303,6 @@ const evidence = (
316
303
  const openFeed = (
317
304
  page: Page,
318
305
  owner: Scope.Scope,
319
- clock: Clock.Clock,
320
306
  settings: ScreencastOptions,
321
307
  current: () => Feed | undefined,
322
308
  publish: (feed: Feed, frame: Frame) => Effect.Effect<unknown>,
@@ -327,9 +313,10 @@ const openFeed = (
327
313
 
328
314
  const feed: Feed = {
329
315
  page,
316
+ capture: undefined,
330
317
  scope,
331
318
  changed: yield* PubSub.sliding<Frame>({ capacity: 1, replay: 1 }),
332
- state: { _tag: "Warming" },
319
+ state: Lifecycle.initial,
333
320
  fiber: undefined,
334
321
  };
335
322
 
@@ -342,26 +329,31 @@ const openFeed = (
342
329
  yield* Effect.addFinalizer(() => (current() === feed ? Effect.void : stop(feed)));
343
330
 
344
331
  const accept = (frame: Frame) =>
345
- Effect.suspend(() => {
332
+ Effect.gen(function* () {
333
+ const state = yield* page.state;
334
+
335
+ if (
336
+ feed.state._tag === "Stopped" ||
337
+ state.page !== frame.page ||
338
+ state.connection !== frame.connection ||
339
+ state.document !== frame.document
340
+ )
341
+ return;
346
342
  update(feed, { _tag: "Paint", frame });
347
-
348
- return (current() === feed ? publish(feed, frame) : Effect.void).pipe(
349
- Effect.andThen(PubSub.publish(feed.changed, frame)),
350
- );
343
+ if (current() === feed) yield* publish(feed, frame);
344
+ yield* PubSub.publish(feed.changed, frame);
351
345
  });
352
346
 
353
347
  const capture = Effect.scoped(
354
348
  Effect.gen(function* () {
355
349
  update(feed, { _tag: "Reset" });
356
350
 
357
- const internals = internalsOf(page);
351
+ const capture = yield* page.capture(settings);
358
352
 
359
- const { stream, current: frame } = yield* (
360
- internals?.capture(settings) ??
361
- Effect.succeed({ stream: page.screencast(settings), current: undefined })
362
- );
353
+ feed.capture = capture;
354
+ const { stream, current: frame } = capture;
363
355
 
364
- if (frame !== undefined && internals?.current(frame)) yield* accept(frame);
356
+ if (frame !== undefined && capture.isCurrent(frame)) yield* accept(frame);
365
357
  yield* Deferred.succeed(started, undefined);
366
358
  yield* Stream.runForEach(stream, accept);
367
359
  }),
@@ -374,7 +366,7 @@ const openFeed = (
374
366
  );
375
367
 
376
368
  feed.fiber = yield* capture.pipe(
377
- Effect.provideService(Clock.Clock, clock),
369
+ Effect.provideService(Clock.Clock, feed.page.clock),
378
370
  Effect.forkIn(scope),
379
371
  );
380
372
  yield* Effect.raceFirst(Deferred.await(started), ended(feed)).pipe(
@@ -387,9 +379,21 @@ const openFeed = (
387
379
  export const make = Effect.fn("Stage.make")(function* (
388
380
  options: Options = {},
389
381
  ): Effect.fn.Return<Stage, BrowserError, Scope.Scope> {
390
- const clock = yield* Clock.Clock;
382
+ const chosen = yield* Deferred.make<Clock.Clock>();
383
+ let clock: Clock.Clock | undefined;
384
+
385
+ const choose = (page: Page) =>
386
+ Effect.suspend(() => {
387
+ if (clock !== undefined && clock !== page.clock)
388
+ return refuse("stage", "pages presented by one stage must share an owning clock");
389
+ clock ??= page.clock;
390
+ Deferred.doneUnsafe(chosen, Exit.succeed(clock));
391
+
392
+ return Effect.void;
393
+ });
394
+
391
395
  const owner = yield* Effect.scope;
392
- const now = () => monotonicMillis(clock);
396
+ const now = () => (clock === undefined ? 0 : monotonicMillis(clock));
393
397
  const waited = Duration.fromInput(options.firstFrame ?? Duration.seconds(5));
394
398
 
395
399
  if (
@@ -422,81 +426,112 @@ export const make = Effect.fn("Stage.make")(function* (
422
426
  return PubSub.publish(output, { feed, frame, at: painted });
423
427
  });
424
428
 
425
- const commit = (feed: Feed, options: { readonly at?: number | undefined } = {}) =>
426
- Effect.suspend(() => {
427
- const asked = options.at ?? now();
428
-
429
- return switching(
430
- Effect.gen(function* () {
431
- if (!Number.isFinite(asked))
432
- return yield* refuse("cue", "at must be finite host milliseconds");
433
- yield* firstOf(feed, firstFrame);
434
- const old = showing;
435
-
436
- if (old?.feed === feed) return old.presented;
429
+ // Both public entry points reserve a FIFO turn before reading the current feed.
430
+ const commit = (feed: Feed, asked: number) =>
431
+ Effect.gen(function* () {
432
+ if (!Number.isFinite(asked))
433
+ return yield* refuse("cue", "at must be finite host milliseconds");
434
+ yield* firstOf(feed, firstFrame);
435
+ const old = showing;
437
436
 
438
- yield* Effect.raceFirst(until(asked, now), ended(feed));
437
+ if (old?.feed === feed) return old.presented;
439
438
 
440
- const switchTo = Effect.suspend(() => {
441
- const frame = frameOf(feed);
439
+ yield* Effect.raceFirst(until(asked, now), ended(feed));
442
440
 
443
- if (frame === undefined) return Effect.fail(unavailable());
444
- const at = Math.max(now(), asked, frame.hostTime, painted);
441
+ const switchTo = Effect.suspend(() => {
442
+ const frame = frameOf(feed);
445
443
 
446
- showing = {
447
- feed,
448
- frame,
449
- presented: new Presented({ page: feed.page.id, at, latency: at - asked }),
450
- changedAt: at,
451
- };
444
+ if (frame === undefined) return Effect.fail(unavailable());
445
+ const at = Math.max(now(), asked, frame.hostTime, painted);
452
446
 
453
- return publish(feed, frame, at).pipe(Effect.as(showing.presented));
454
- }).pipe(Effect.uninterruptible);
447
+ showing = {
448
+ feed,
449
+ frame,
450
+ presented: new Presented({ page: feed.page.id, at, latency: at - asked }),
451
+ changedAt: at,
452
+ };
455
453
 
456
- const readyToSwitch = firstOf(feed, firstFrame).pipe(
457
- Effect.andThen(switchTo),
458
- Effect.provideService(Clock.Clock, clock),
459
- );
454
+ return publish(feed, frame, at).pipe(Effect.as(showing.presented));
455
+ }).pipe(Effect.uninterruptible);
460
456
 
461
- const before = old === undefined ? undefined : internalsOf(old.feed.page);
457
+ const readyToSwitch = firstOf(feed, firstFrame).pipe(Effect.andThen(switchTo));
462
458
 
463
- return yield* (
464
- before === undefined ? readyToSwitch : before.afterInput(readyToSwitch)
465
- ).pipe(
466
- // The cut owns old-page input through publication. Capture cleanup has its own bound
467
- // and must finish even if the caller leaves, without turning a completed cut into Timeout.
468
- Effect.ensuring(
469
- Effect.suspend(() =>
470
- showing?.feed === feed && old !== undefined ? stop(old.feed) : Effect.void,
471
- ),
472
- ),
473
- );
474
- }).pipe(Effect.provideService(Clock.Clock, clock)),
459
+ return yield* (
460
+ old === undefined ? readyToSwitch : old.feed.page.afterInput(readyToSwitch)
461
+ ).pipe(
462
+ // The cut owns old-page input through publication. Capture cleanup has its own bound
463
+ // and must finish even if the caller leaves, without turning a completed cut into Timeout.
464
+ Effect.ensuring(
465
+ Effect.suspend(() =>
466
+ showing?.feed === feed && old !== undefined ? stop(old.feed) : Effect.void,
467
+ ),
468
+ ),
475
469
  );
476
470
  });
477
471
 
478
472
  const cue: Stage["cue"] = (page) =>
479
- Effect.map(
480
- openFeed(page, owner, clock, settings, () => showing?.feed, publish),
481
- (feed) => ({
482
- page,
483
- ready: firstOf(feed, firstFrame).pipe(Effect.provideService(Clock.Clock, clock)),
484
- commit: (options) => commit(feed, options),
485
- }),
473
+ choose(page).pipe(
474
+ Effect.andThen(
475
+ Effect.map(
476
+ openFeed(page, owner, settings, () => showing?.feed, publish),
477
+ (feed) => ({
478
+ page,
479
+ ready: firstOf(feed, firstFrame).pipe(
480
+ Effect.provideService(Clock.Clock, feed.page.clock),
481
+ ),
482
+ commit: (options?: { readonly at?: number | undefined }) =>
483
+ Effect.suspend(() => switching(commit(feed, options?.at ?? now()))).pipe(
484
+ Effect.provideService(Clock.Clock, feed.page.clock),
485
+ ),
486
+ }),
487
+ ),
488
+ ),
486
489
  );
487
490
 
488
491
  return {
489
492
  cue,
490
493
  present: (page, options) =>
491
- Effect.suspend(() => {
492
- const asked = options?.at ?? now();
494
+ choose(page).pipe(
495
+ Effect.andThen(
496
+ Effect.suspend(() => {
497
+ const asked = options?.at ?? now();
493
498
 
494
- return showing?.feed.page === page
495
- ? Effect.as(firstOf(showing.feed, firstFrame), showing.presented)
496
- : Effect.scoped(Effect.flatMap(cue(page), (prepared) => prepared.commit({ at: asked })));
497
- }),
498
- shown: ({ hold }) =>
499
- evidence(output, () => showing, now, hold).pipe(Stream.provideService(Clock.Clock, clock)),
499
+ return switching(
500
+ Effect.suspend(() =>
501
+ showing?.feed.page === page
502
+ ? commit(showing.feed, asked)
503
+ : Effect.scoped(
504
+ Effect.flatMap(
505
+ openFeed(page, owner, settings, () => showing?.feed, publish),
506
+ (feed) => commit(feed, asked),
507
+ ),
508
+ ),
509
+ ),
510
+ );
511
+ }),
512
+ ),
513
+ Effect.provideService(Clock.Clock, page.clock),
514
+ ),
515
+ shown: ({ hold }) => {
516
+ const parsed = Duration.fromInput(hold);
517
+
518
+ if (
519
+ Option.isNone(parsed) ||
520
+ !Duration.isFinite(parsed.value) ||
521
+ !Duration.isPositive(parsed.value)
522
+ )
523
+ return Stream.fromEffect(refuse("shown", "hold must be a finite positive duration"));
524
+
525
+ return Stream.unwrap(
526
+ Deferred.await(chosen).pipe(
527
+ Effect.map((ownerClock) =>
528
+ evidence(output, () => showing, now, parsed.value).pipe(
529
+ Stream.provideService(Clock.Clock, ownerClock),
530
+ ),
531
+ ),
532
+ ),
533
+ );
534
+ },
500
535
  frames: Stream.fromPubSub(output).pipe(Stream.map(({ frame }) => frame)),
501
536
  current: Effect.sync(() => Option.fromNullishOr(showing?.feed.page)),
502
537
  stats: (options = {}) =>
@@ -0,0 +1,21 @@
1
+ /** The capture feed's lifecycle: reset drops evidence without undoing readiness or retirement. */
2
+ import type { Frame } from "../Frame.ts";
3
+
4
+ export type State =
5
+ | { readonly _tag: "Warming" }
6
+ | { readonly _tag: "Ready"; readonly latest: Frame | undefined }
7
+ | { readonly _tag: "Stopped" };
8
+
9
+ export type Input =
10
+ | { readonly _tag: "Paint"; readonly frame: Frame }
11
+ | { readonly _tag: "Reset" | "Stop" };
12
+
13
+ export const initial: State = { _tag: "Warming" };
14
+
15
+ export const step = (state: State, input: Input): State => {
16
+ if (state._tag === "Stopped") return state;
17
+ if (input._tag === "Stop") return { _tag: "Stopped" };
18
+ if (input._tag === "Paint") return { _tag: "Ready", latest: input.frame };
19
+
20
+ return state._tag === "Ready" ? { _tag: "Ready", latest: undefined } : state;
21
+ };