@torpor/view 1.2.1 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/ssr.cjs CHANGED
@@ -22,10 +22,46 @@ function formatServerText(value) {
22
22
  return require_fromWebSocket.formatText(value).replace(/[&<>]/g, (c) => ESCAPABLE[c]);
23
23
  }
24
24
  //#endregion
25
+ //#region src/ssr/$serverWatch.ts
26
+ let currentSite = null;
27
+ /** The watched getter currently being read, or null outside a `$watch` get. */
28
+ function currentServerReadSite() {
29
+ return currentSite;
30
+ }
31
+ /**
32
+ * Server-render counterpart of `$watch`.
33
+ *
34
+ * State doesn't need to be reactive on the server, so values aren't wrapped
35
+ * for tracking. Plain objects and arrays are wrapped in a lightweight proxy
36
+ * that only records the property key while an accessor getter runs:
37
+ * `$serverAsync` uses that to identify the getter site across the collect and
38
+ * render passes. Other objects (Maps, class instances, …) are returned
39
+ * untouched — proxying them would break internal slots or private fields, and
40
+ * they rarely carry async getters.
41
+ */
42
+ function $serverWatch(object) {
43
+ if (object === null || object === void 0) return object;
44
+ const prototype = Object.getPrototypeOf(object);
45
+ if (!Array.isArray(object) && prototype !== Object.prototype && prototype !== null) return object;
46
+ return new Proxy(object, { get(target, key, receiver) {
47
+ if (Object.getOwnPropertyDescriptor(target, key)?.get === void 0) return Reflect.get(target, key, receiver);
48
+ const previous = currentSite;
49
+ currentSite = {
50
+ owner: target,
51
+ key
52
+ };
53
+ try {
54
+ return Reflect.get(target, key, receiver);
55
+ } finally {
56
+ currentSite = previous;
57
+ }
58
+ } });
59
+ }
60
+ //#endregion
25
61
  //#region src/ssr/serverAwaitState.ts
26
62
  /**
27
- * The sentinel thrown when the render pass hits a boundary state that the
28
- * collect pass didn't create (or a cursor that ran past the recorded reads).
63
+ * The sentinel thrown when the render pass reads a `source: "server"` getter
64
+ * that the collect pass never recorded (or one whose value hasn't settled).
29
65
  * Only this error degrades the boundary; real render errors propagate.
30
66
  */
31
67
  var ServerAwaitMisaligned = class extends Error {
@@ -58,8 +94,8 @@ function newServerAwaitState() {
58
94
  return {
59
95
  phase: "collect",
60
96
  promises: [],
61
- cursor: 0,
62
- renderReads: 0,
97
+ renderEntries: [],
98
+ renderSeen: /* @__PURE__ */ new Set(),
63
99
  clientReads: 0,
64
100
  children: /* @__PURE__ */ new Map(),
65
101
  counters: /* @__PURE__ */ new Map(),
@@ -94,13 +130,16 @@ function childKey(state, id) {
94
130
  *
95
131
  * - **Collect pass** — calls the thunk (starting the fetch) and records the
96
132
  * promise on the enclosing boundary, so sibling reads start in one wave.
97
- * Returns `undefined`; the speculative pass's output is discarded.
98
- * - **Render pass** — returns the settled value from the recorded promise at
99
- * the same position in read order, without calling the thunk again. A
100
- * rejection is re-thrown here so `@try`/`@error` handles it like any other
101
- * render error. Running past the recorded reads throws a sentinel that
102
- * degrades the boundary (the render pass no longer matches the collect
103
- * pass, so the shipped HTML can't be trusted).
133
+ * The read is keyed by getter identity (watched object + property key), so
134
+ * reading the same getter again reuses the recorded fetch. Returns
135
+ * `undefined`; the speculative pass's output is discarded.
136
+ * - **Render pass** — returns the settled value for the same getter identity,
137
+ * without calling the thunk again. The pass may read a getter any number of
138
+ * times, so markup may branch on the resolved value. A rejection is
139
+ * re-thrown here so `@try`/`@error` handles it like any other render error.
140
+ * Reading a getter the collect pass never touched throws a sentinel that
141
+ * degrades the boundary (its value can't be resolved without fetching, and
142
+ * the shipped HTML can't be trusted).
104
143
  *
105
144
  * Outside any `@await` boundary (including in a `with` branch) a
106
145
  * `source: "server"` read returns `undefined` without fetching — the same
@@ -112,34 +151,46 @@ function $serverAsync(fn, options) {
112
151
  if (state !== null && !state.isRoot && state.phase === "render") state.clientReads++;
113
152
  return;
114
153
  }
154
+ const site = currentServerReadSite();
155
+ const owner = site?.owner ?? null;
156
+ const key = site?.key ?? fn.toString();
115
157
  if (state.phase === "collect") {
116
- const entry = {
117
- promise: fn(),
118
- timeout: options.timeout ?? 5e3
119
- };
120
- entry.promise.then((value) => {
121
- entry.result = {
122
- ok: true,
123
- value
124
- };
125
- }, (error) => {
126
- entry.result = {
127
- ok: false,
128
- error
158
+ if (findEntry(state, owner, key) === void 0) {
159
+ const entry = {
160
+ owner,
161
+ key,
162
+ promise: fn(),
163
+ timeout: options.timeout ?? 5e3
129
164
  };
130
- });
131
- state.promises.push(entry);
165
+ entry.promise.then((value) => {
166
+ entry.result = {
167
+ ok: true,
168
+ value
169
+ };
170
+ }, (error) => {
171
+ entry.result = {
172
+ ok: false,
173
+ error
174
+ };
175
+ });
176
+ state.promises.push(entry);
177
+ }
132
178
  return;
133
179
  }
134
- const index = state.cursor;
135
- if (index >= state.promises.length) throw new ServerAwaitMisaligned();
136
- state.cursor++;
137
- state.renderReads++;
138
- const result = state.promises[index].result;
180
+ const entry = findEntry(state, owner, key);
181
+ if (entry === void 0) throw new ServerAwaitMisaligned();
182
+ if (!state.renderSeen.has(entry)) {
183
+ state.renderSeen.add(entry);
184
+ state.renderEntries.push(entry);
185
+ }
186
+ const result = entry.result;
139
187
  if (result === void 0) throw new ServerAwaitMisaligned();
140
188
  if (result.ok) return result.value;
141
189
  throw result.error;
142
190
  }
191
+ function findEntry(state, owner, key) {
192
+ for (const entry of state.promises) if (entry.owner === owner && entry.key === key) return entry;
193
+ }
143
194
  //#endregion
144
195
  //#region src/ssr/$serverBatch.ts
145
196
  function $serverBatch(fn) {
@@ -201,11 +252,6 @@ function $serverStream(_source, _handler, _options) {}
201
252
  function $serverUnwrap(object) {
202
253
  return object;
203
254
  }
204
- //#endregion
205
- //#region src/ssr/$serverWatch.ts
206
- function $serverWatch(object) {
207
- return object;
208
- }
209
255
  /**
210
256
  * Renders a component's (or a degraded boundary's `with` branch's) markup
211
257
  * with a fresh flush root as the enclosing await state, then substitutes any
@@ -292,12 +338,13 @@ function nextSentinel() {
292
338
  *
293
339
  * The lifecycle: render content once to start the `source: "server"` fetches
294
340
  * (discarding the output), wait for them with a per-getter timeout, then
295
- * render again with the settled values. The second pass ships wrapped in
296
- * hydration markers plus a `<!--t-await:...-->` comment carrying the values
297
- * for the client's `$async` hydration path. Any instability — timeout, a
298
- * render pass that reads a different set of getters, a client-fetch read —
299
- * degrades to the `with` branch + client fetch, exactly the pre-`source`
300
- * behavior.
341
+ * render again with the settled values. Reads are matched by getter identity,
342
+ * so the render pass may read a getter more than once (e.g. branch on it).
343
+ * The second pass ships wrapped in hydration markers plus a
344
+ * `<!--t-await:...-->` comment carrying the values for the client's `$async`
345
+ * hydration path. Any instability — timeout, a render pass that reads a getter
346
+ * the collect pass never touched, a client-fetch read — degrades to the `with`
347
+ * branch + client fetch, exactly the pre-`source` behavior.
301
348
  *
302
349
  * @param id The boundary's site id (unique per `@await` in the component)
303
350
  * @param renderContent Renders the content branch (may read `$async` getters)
@@ -334,13 +381,13 @@ async function renderBoundary(state, renderContent, renderWith) {
334
381
  let content;
335
382
  if (!degraded) {
336
383
  state.phase = "render";
337
- state.cursor = 0;
338
- state.renderReads = 0;
384
+ state.renderEntries.length = 0;
385
+ state.renderSeen.clear();
339
386
  state.clientReads = 0;
340
387
  state.counters.clear();
341
388
  try {
342
389
  content = await withServerAwait(state, renderContent);
343
- if (state.cursor !== state.promises.length || state.clientReads > 0) degraded = true;
390
+ if (state.clientReads > 0) degraded = true;
344
391
  } catch {
345
392
  degraded = true;
346
393
  }
@@ -357,8 +404,8 @@ async function renderBoundary(state, renderContent, renderWith) {
357
404
  }
358
405
  }
359
406
  const values = [];
360
- for (let i = 0; i < state.cursor; i++) {
361
- const result = state.promises[i].result;
407
+ for (const entry of state.renderEntries) {
408
+ const result = entry.result;
362
409
  if (result === void 0) {
363
410
  degraded = true;
364
411
  break;
@@ -376,8 +423,9 @@ async function renderBoundary(state, renderContent, renderWith) {
376
423
  }
377
424
  /**
378
425
  * The `<!--t-await:...-->` comment carrying the boundary's resolved values,
379
- * in read order, for the client's `$async` hydration path. `-->` inside the
380
- * JSON is escaped so the comment can't terminate early.
426
+ * in the order the render pass first read them, for the client's `$async`
427
+ * hydration path. `-->` inside the JSON is escaped so the comment can't
428
+ * terminate early.
381
429
  */
382
430
  function payloadComment(values) {
383
431
  return `<!--t-await:${JSON.stringify(values).replaceAll("-->", "--\\u003E")}-->`;
package/dist/ssr.d.cts CHANGED
@@ -30,13 +30,16 @@ declare function formatServerText(value: any): string;
30
30
  *
31
31
  * - **Collect pass** — calls the thunk (starting the fetch) and records the
32
32
  * promise on the enclosing boundary, so sibling reads start in one wave.
33
- * Returns `undefined`; the speculative pass's output is discarded.
34
- * - **Render pass** — returns the settled value from the recorded promise at
35
- * the same position in read order, without calling the thunk again. A
36
- * rejection is re-thrown here so `@try`/`@error` handles it like any other
37
- * render error. Running past the recorded reads throws a sentinel that
38
- * degrades the boundary (the render pass no longer matches the collect
39
- * pass, so the shipped HTML can't be trusted).
33
+ * The read is keyed by getter identity (watched object + property key), so
34
+ * reading the same getter again reuses the recorded fetch. Returns
35
+ * `undefined`; the speculative pass's output is discarded.
36
+ * - **Render pass** — returns the settled value for the same getter identity,
37
+ * without calling the thunk again. The pass may read a getter any number of
38
+ * times, so markup may branch on the resolved value. A rejection is
39
+ * re-thrown here so `@try`/`@error` handles it like any other render error.
40
+ * Reading a getter the collect pass never touched throws a sentinel that
41
+ * degrades the boundary (its value can't be resolved without fetching, and
42
+ * the shipped HTML can't be trusted).
40
43
  *
41
44
  * Outside any `@await` boundary (including in a `with` branch) a
42
45
  * `source: "server"` read returns `undefined` without fetching — the same
@@ -98,6 +101,17 @@ declare function $serverStream<T>(_source: StreamSource<T>, _handler: (value: T)
98
101
  declare function $serverUnwrap<T extends Record<PropertyKey, any>>(object: T): T;
99
102
  //#endregion
100
103
  //#region src/ssr/$serverWatch.d.ts
104
+ /**
105
+ * Server-render counterpart of `$watch`.
106
+ *
107
+ * State doesn't need to be reactive on the server, so values aren't wrapped
108
+ * for tracking. Plain objects and arrays are wrapped in a lightweight proxy
109
+ * that only records the property key while an accessor getter runs:
110
+ * `$serverAsync` uses that to identify the getter site across the collect and
111
+ * render passes. Other objects (Maps, class instances, …) are returned
112
+ * untouched — proxying them would break internal slots or private fields, and
113
+ * they rarely carry async getters.
114
+ */
101
115
  declare function $serverWatch<T extends Record<PropertyKey, any>>(object: T): T;
102
116
  //#endregion
103
117
  //#region src/ssr/runServerAwait.d.ts
@@ -122,12 +136,13 @@ declare function $serverWatch<T extends Record<PropertyKey, any>>(object: T): T;
122
136
  *
123
137
  * The lifecycle: render content once to start the `source: "server"` fetches
124
138
  * (discarding the output), wait for them with a per-getter timeout, then
125
- * render again with the settled values. The second pass ships wrapped in
126
- * hydration markers plus a `<!--t-await:...-->` comment carrying the values
127
- * for the client's `$async` hydration path. Any instability — timeout, a
128
- * render pass that reads a different set of getters, a client-fetch read —
129
- * degrades to the `with` branch + client fetch, exactly the pre-`source`
130
- * behavior.
139
+ * render again with the settled values. Reads are matched by getter identity,
140
+ * so the render pass may read a getter more than once (e.g. branch on it).
141
+ * The second pass ships wrapped in hydration markers plus a
142
+ * `<!--t-await:...-->` comment carrying the values for the client's `$async`
143
+ * hydration path. Any instability — timeout, a render pass that reads a getter
144
+ * the collect pass never touched, a client-fetch read — degrades to the `with`
145
+ * branch + client fetch, exactly the pre-`source` behavior.
131
146
  *
132
147
  * @param id The boundary's site id (unique per `@await` in the component)
133
148
  * @param renderContent Renders the content branch (may read `$async` getters)
@@ -1 +1 @@
1
- {"version":3,"file":"ssr.d.cts","names":[],"sources":["../src/render/formatAttributeText.ts","../src/ssr/formatText.ts","../src/ssr/$serverAsync.ts","../src/ssr/$serverBatch.ts","../src/ssr/$serverBind.ts","../src/ssr/$serverCache.ts","../src/ssr/$serverOnmount.ts","../src/ssr/$serverPeek.ts","../src/ssr/$serverPending.ts","../src/ssr/$serverRefresh.ts","../src/ssr/$serverRun.ts","../src/ssr/$serverStream.ts","../src/ssr/$serverUnwrap.ts","../src/ssr/$serverWatch.ts","../src/ssr/runServerAwait.ts","../src/ssr/serverSentinels.ts","../src/types/ServerSlotRender.ts","../src/types/ServerComponent.ts","../src/ssr/unavailable.ts"],"mappings":";;;iBAAwB,oBAAoB;;;;;;;;;;iBCepB,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBC2BjB,aAAa,GACpC,KAAK,cAAc,MAAM,QAAQ,IACjC,UAAU,eACR;;;iBC7CqB,aAAa,GAAG,UAAU,IAAI;;;;;;;;;iBCM9B,YACvB,QAAQ,OAAO,mBACf,QAAQ,OAAO,kCACZ;;;iBCToB,aAAa,GAAG,UAAU,IAAI;;;iBCE9B,eAAe,WAAW;;;iBCF1B,YAAY,GAAG,UAAU,IAAI;;;;;;;;iBCK7B,eAAe;;;;;;;;iBCAf,eAAe;;;iBCHf,WAAW,WAAW;;;;;;;;iBCKtB,cAAc,GACrC,SAAS,aAAa,IACtB,WAAW,OAAO,YAClB;EAAa;;;;iBCVU,cAAc,UAAU,OAAO,mBAAmB,QAAQ,IAAI;;;iBCA9D,aAAa,UAAU,OAAO,mBAAmB,QAAQ,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCgD7D,eACvB,YACA,qBAAqB;EAAU;EAAc;IAC7C,mBAAmB;EAAU;EAAc;;EACvC;EAAc;IAAiB;EAAU;EAAc;;;;;UC7C3C;EAChB;EACA;;;;;;;;;;;;;;;;iBAiBqB,YAAY,cAAc,QAAQ,cAAc,QAAQ;;;;;;;KCtBzE,oBACJ,QAAQ,OAAO,mBACf,WAAW,OAAO,sBACd;;;;;;;;;;KCEA,mBACJ,SAAS,OAAO,mBAChB,WAAW,OAAO,mBAClB,SAAS,eAAe,sBACpB;EAAU;EAAc;;;;;;;;;;;;;;;KCFxB,kBAAkB;qBAQV,OAAO;qBACP,SAAS;qBACT,SAAS;qBACT,gBAAgB;qBAChB,iBAAiB;qBAEjB,eAAe;qBACf,gBAAgB;qBAChB,UAAU;qBACV,WAAW;qBACX,eAAe;qBACf,aAAa;qBACb,SAAS;qBACT,WAAW;qBACX,SAAS;qBACT,gBAAgB;qBAChB,YAAY;qBACZ,eAAe;qBACf,gBAAgB;qBAChB,aAAa;qBACb,QAAQ;qBACR,cAAc;qBACd,eAAe;qBACf,UAAU;qBACV,wBAAwB;qBACxB,qBAAqB;qBACrB,QAAQ;qBACR,WAAW;qBACX,aAAa;qBACb,cAAc;qBACd,eAAe;qBACf,YAAY;qBACZ,WAAW;qBACX,kBAAkB;qBAClB,QAAQ"}
1
+ {"version":3,"file":"ssr.d.cts","names":[],"sources":["../src/render/formatAttributeText.ts","../src/ssr/formatText.ts","../src/ssr/$serverAsync.ts","../src/ssr/$serverBatch.ts","../src/ssr/$serverBind.ts","../src/ssr/$serverCache.ts","../src/ssr/$serverOnmount.ts","../src/ssr/$serverPeek.ts","../src/ssr/$serverPending.ts","../src/ssr/$serverRefresh.ts","../src/ssr/$serverRun.ts","../src/ssr/$serverStream.ts","../src/ssr/$serverUnwrap.ts","../src/ssr/$serverWatch.ts","../src/ssr/runServerAwait.ts","../src/ssr/serverSentinels.ts","../src/types/ServerSlotRender.ts","../src/types/ServerComponent.ts","../src/ssr/unavailable.ts"],"mappings":";;;iBAAwB,oBAAoB;;;;;;;;;;iBCepB,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCgCjB,aAAa,GACpC,KAAK,cAAc,MAAM,QAAQ,IACjC,UAAU,eACR;;;iBClDqB,aAAa,GAAG,UAAU,IAAI;;;;;;;;;iBCM9B,YACvB,QAAQ,OAAO,mBACf,QAAQ,OAAO,kCACZ;;;iBCToB,aAAa,GAAG,UAAU,IAAI;;;iBCE9B,eAAe,WAAW;;;iBCF1B,YAAY,GAAG,UAAU,IAAI;;;;;;;;iBCK7B,eAAe;;;;;;;;iBCAf,eAAe;;;iBCHf,WAAW,WAAW;;;;;;;;iBCKtB,cAAc,GACrC,SAAS,aAAa,IACtB,WAAW,OAAO,YAClB;EAAa;;;;iBCVU,cAAc,UAAU,OAAO,mBAAmB,QAAQ,IAAI;;;;;;;;;;;;;;iBC8B9D,aAAa,UAAU,OAAO,mBAAmB,QAAQ,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCmB7D,eACvB,YACA,qBAAqB;EAAU;EAAc;IAC7C,mBAAmB;EAAU;EAAc;;EACvC;EAAc;IAAiB;EAAU;EAAc;;;;;UC9C3C;EAChB;EACA;;;;;;;;;;;;;;;;iBAiBqB,YAAY,cAAc,QAAQ,cAAc,QAAQ;;;;;;;KCtBzE,oBACJ,QAAQ,OAAO,mBACf,WAAW,OAAO,sBACd;;;;;;;;;;KCEA,mBACJ,SAAS,OAAO,mBAChB,WAAW,OAAO,mBAClB,SAAS,eAAe,sBACpB;EAAU;EAAc;;;;;;;;;;;;;;;KCFxB,kBAAkB;qBAQV,OAAO;qBACP,SAAS;qBACT,SAAS;qBACT,gBAAgB;qBAChB,iBAAiB;qBAEjB,eAAe;qBACf,gBAAgB;qBAChB,UAAU;qBACV,WAAW;qBACX,eAAe;qBACf,aAAa;qBACb,SAAS;qBACT,WAAW;qBACX,SAAS;qBACT,gBAAgB;qBAChB,YAAY;qBACZ,eAAe;qBACf,gBAAgB;qBAChB,aAAa;qBACb,QAAQ;qBACR,cAAc;qBACd,eAAe;qBACf,UAAU;qBACV,wBAAwB;qBACxB,qBAAqB;qBACrB,QAAQ;qBACR,WAAW;qBACX,aAAa;qBACb,cAAc;qBACd,eAAe;qBACf,YAAY;qBACZ,WAAW;qBACX,kBAAkB;qBAClB,QAAQ"}
package/dist/ssr.d.mts CHANGED
@@ -30,13 +30,16 @@ declare function formatServerText(value: any): string;
30
30
  *
31
31
  * - **Collect pass** — calls the thunk (starting the fetch) and records the
32
32
  * promise on the enclosing boundary, so sibling reads start in one wave.
33
- * Returns `undefined`; the speculative pass's output is discarded.
34
- * - **Render pass** — returns the settled value from the recorded promise at
35
- * the same position in read order, without calling the thunk again. A
36
- * rejection is re-thrown here so `@try`/`@error` handles it like any other
37
- * render error. Running past the recorded reads throws a sentinel that
38
- * degrades the boundary (the render pass no longer matches the collect
39
- * pass, so the shipped HTML can't be trusted).
33
+ * The read is keyed by getter identity (watched object + property key), so
34
+ * reading the same getter again reuses the recorded fetch. Returns
35
+ * `undefined`; the speculative pass's output is discarded.
36
+ * - **Render pass** — returns the settled value for the same getter identity,
37
+ * without calling the thunk again. The pass may read a getter any number of
38
+ * times, so markup may branch on the resolved value. A rejection is
39
+ * re-thrown here so `@try`/`@error` handles it like any other render error.
40
+ * Reading a getter the collect pass never touched throws a sentinel that
41
+ * degrades the boundary (its value can't be resolved without fetching, and
42
+ * the shipped HTML can't be trusted).
40
43
  *
41
44
  * Outside any `@await` boundary (including in a `with` branch) a
42
45
  * `source: "server"` read returns `undefined` without fetching — the same
@@ -98,6 +101,17 @@ declare function $serverStream<T>(_source: StreamSource<T>, _handler: (value: T)
98
101
  declare function $serverUnwrap<T extends Record<PropertyKey, any>>(object: T): T;
99
102
  //#endregion
100
103
  //#region src/ssr/$serverWatch.d.ts
104
+ /**
105
+ * Server-render counterpart of `$watch`.
106
+ *
107
+ * State doesn't need to be reactive on the server, so values aren't wrapped
108
+ * for tracking. Plain objects and arrays are wrapped in a lightweight proxy
109
+ * that only records the property key while an accessor getter runs:
110
+ * `$serverAsync` uses that to identify the getter site across the collect and
111
+ * render passes. Other objects (Maps, class instances, …) are returned
112
+ * untouched — proxying them would break internal slots or private fields, and
113
+ * they rarely carry async getters.
114
+ */
101
115
  declare function $serverWatch<T extends Record<PropertyKey, any>>(object: T): T;
102
116
  //#endregion
103
117
  //#region src/ssr/runServerAwait.d.ts
@@ -122,12 +136,13 @@ declare function $serverWatch<T extends Record<PropertyKey, any>>(object: T): T;
122
136
  *
123
137
  * The lifecycle: render content once to start the `source: "server"` fetches
124
138
  * (discarding the output), wait for them with a per-getter timeout, then
125
- * render again with the settled values. The second pass ships wrapped in
126
- * hydration markers plus a `<!--t-await:...-->` comment carrying the values
127
- * for the client's `$async` hydration path. Any instability — timeout, a
128
- * render pass that reads a different set of getters, a client-fetch read —
129
- * degrades to the `with` branch + client fetch, exactly the pre-`source`
130
- * behavior.
139
+ * render again with the settled values. Reads are matched by getter identity,
140
+ * so the render pass may read a getter more than once (e.g. branch on it).
141
+ * The second pass ships wrapped in hydration markers plus a
142
+ * `<!--t-await:...-->` comment carrying the values for the client's `$async`
143
+ * hydration path. Any instability — timeout, a render pass that reads a getter
144
+ * the collect pass never touched, a client-fetch read — degrades to the `with`
145
+ * branch + client fetch, exactly the pre-`source` behavior.
131
146
  *
132
147
  * @param id The boundary's site id (unique per `@await` in the component)
133
148
  * @param renderContent Renders the content branch (may read `$async` getters)
@@ -1 +1 @@
1
- {"version":3,"file":"ssr.d.mts","names":[],"sources":["../src/render/formatAttributeText.ts","../src/ssr/formatText.ts","../src/ssr/$serverAsync.ts","../src/ssr/$serverBatch.ts","../src/ssr/$serverBind.ts","../src/ssr/$serverCache.ts","../src/ssr/$serverOnmount.ts","../src/ssr/$serverPeek.ts","../src/ssr/$serverPending.ts","../src/ssr/$serverRefresh.ts","../src/ssr/$serverRun.ts","../src/ssr/$serverStream.ts","../src/ssr/$serverUnwrap.ts","../src/ssr/$serverWatch.ts","../src/ssr/runServerAwait.ts","../src/ssr/serverSentinels.ts","../src/types/ServerSlotRender.ts","../src/types/ServerComponent.ts","../src/ssr/unavailable.ts"],"mappings":";;;iBAAwB,oBAAoB;;;;;;;;;;iBCepB,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBC2BjB,aAAa,GACpC,KAAK,cAAc,MAAM,QAAQ,IACjC,UAAU,eACR;;;iBC7CqB,aAAa,GAAG,UAAU,IAAI;;;;;;;;;iBCM9B,YACvB,QAAQ,OAAO,mBACf,QAAQ,OAAO,kCACZ;;;iBCToB,aAAa,GAAG,UAAU,IAAI;;;iBCE9B,eAAe,WAAW;;;iBCF1B,YAAY,GAAG,UAAU,IAAI;;;;;;;;iBCK7B,eAAe;;;;;;;;iBCAf,eAAe;;;iBCHf,WAAW,WAAW;;;;;;;;iBCKtB,cAAc,GACrC,SAAS,aAAa,IACtB,WAAW,OAAO,YAClB;EAAa;;;;iBCVU,cAAc,UAAU,OAAO,mBAAmB,QAAQ,IAAI;;;iBCA9D,aAAa,UAAU,OAAO,mBAAmB,QAAQ,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCgD7D,eACvB,YACA,qBAAqB;EAAU;EAAc;IAC7C,mBAAmB;EAAU;EAAc;;EACvC;EAAc;IAAiB;EAAU;EAAc;;;;;UC7C3C;EAChB;EACA;;;;;;;;;;;;;;;;iBAiBqB,YAAY,cAAc,QAAQ,cAAc,QAAQ;;;;;;;KCtBzE,oBACJ,QAAQ,OAAO,mBACf,WAAW,OAAO,sBACd;;;;;;;;;;KCEA,mBACJ,SAAS,OAAO,mBAChB,WAAW,OAAO,mBAClB,SAAS,eAAe,sBACpB;EAAU;EAAc;;;;;;;;;;;;;;;KCFxB,kBAAkB;qBAQV,OAAO;qBACP,SAAS;qBACT,SAAS;qBACT,gBAAgB;qBAChB,iBAAiB;qBAEjB,eAAe;qBACf,gBAAgB;qBAChB,UAAU;qBACV,WAAW;qBACX,eAAe;qBACf,aAAa;qBACb,SAAS;qBACT,WAAW;qBACX,SAAS;qBACT,gBAAgB;qBAChB,YAAY;qBACZ,eAAe;qBACf,gBAAgB;qBAChB,aAAa;qBACb,QAAQ;qBACR,cAAc;qBACd,eAAe;qBACf,UAAU;qBACV,wBAAwB;qBACxB,qBAAqB;qBACrB,QAAQ;qBACR,WAAW;qBACX,aAAa;qBACb,cAAc;qBACd,eAAe;qBACf,YAAY;qBACZ,WAAW;qBACX,kBAAkB;qBAClB,QAAQ"}
1
+ {"version":3,"file":"ssr.d.mts","names":[],"sources":["../src/render/formatAttributeText.ts","../src/ssr/formatText.ts","../src/ssr/$serverAsync.ts","../src/ssr/$serverBatch.ts","../src/ssr/$serverBind.ts","../src/ssr/$serverCache.ts","../src/ssr/$serverOnmount.ts","../src/ssr/$serverPeek.ts","../src/ssr/$serverPending.ts","../src/ssr/$serverRefresh.ts","../src/ssr/$serverRun.ts","../src/ssr/$serverStream.ts","../src/ssr/$serverUnwrap.ts","../src/ssr/$serverWatch.ts","../src/ssr/runServerAwait.ts","../src/ssr/serverSentinels.ts","../src/types/ServerSlotRender.ts","../src/types/ServerComponent.ts","../src/ssr/unavailable.ts"],"mappings":";;;iBAAwB,oBAAoB;;;;;;;;;;iBCepB,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCgCjB,aAAa,GACpC,KAAK,cAAc,MAAM,QAAQ,IACjC,UAAU,eACR;;;iBClDqB,aAAa,GAAG,UAAU,IAAI;;;;;;;;;iBCM9B,YACvB,QAAQ,OAAO,mBACf,QAAQ,OAAO,kCACZ;;;iBCToB,aAAa,GAAG,UAAU,IAAI;;;iBCE9B,eAAe,WAAW;;;iBCF1B,YAAY,GAAG,UAAU,IAAI;;;;;;;;iBCK7B,eAAe;;;;;;;;iBCAf,eAAe;;;iBCHf,WAAW,WAAW;;;;;;;;iBCKtB,cAAc,GACrC,SAAS,aAAa,IACtB,WAAW,OAAO,YAClB;EAAa;;;;iBCVU,cAAc,UAAU,OAAO,mBAAmB,QAAQ,IAAI;;;;;;;;;;;;;;iBC8B9D,aAAa,UAAU,OAAO,mBAAmB,QAAQ,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCmB7D,eACvB,YACA,qBAAqB;EAAU;EAAc;IAC7C,mBAAmB;EAAU;EAAc;;EACvC;EAAc;IAAiB;EAAU;EAAc;;;;;UC9C3C;EAChB;EACA;;;;;;;;;;;;;;;;iBAiBqB,YAAY,cAAc,QAAQ,cAAc,QAAQ;;;;;;;KCtBzE,oBACJ,QAAQ,OAAO,mBACf,WAAW,OAAO,sBACd;;;;;;;;;;KCEA,mBACJ,SAAS,OAAO,mBAChB,WAAW,OAAO,mBAClB,SAAS,eAAe,sBACpB;EAAU;EAAc;;;;;;;;;;;;;;;KCFxB,kBAAkB;qBAQV,OAAO;qBACP,SAAS;qBACT,SAAS;qBACT,gBAAgB;qBAChB,iBAAiB;qBAEjB,eAAe;qBACf,gBAAgB;qBAChB,UAAU;qBACV,WAAW;qBACX,eAAe;qBACf,aAAa;qBACb,SAAS;qBACT,WAAW;qBACX,SAAS;qBACT,gBAAgB;qBAChB,YAAY;qBACZ,eAAe;qBACf,gBAAgB;qBAChB,aAAa;qBACb,QAAQ;qBACR,cAAc;qBACd,eAAe;qBACf,UAAU;qBACV,wBAAwB;qBACxB,qBAAqB;qBACrB,QAAQ;qBACR,WAAW;qBACX,aAAa;qBACb,cAAc;qBACd,eAAe;qBACf,YAAY;qBACZ,WAAW;qBACX,kBAAkB;qBAClB,QAAQ"}
package/dist/ssr.mjs CHANGED
@@ -21,10 +21,46 @@ function formatServerText(value) {
21
21
  return formatText(value).replace(/[&<>]/g, (c) => ESCAPABLE[c]);
22
22
  }
23
23
  //#endregion
24
+ //#region src/ssr/$serverWatch.ts
25
+ let currentSite = null;
26
+ /** The watched getter currently being read, or null outside a `$watch` get. */
27
+ function currentServerReadSite() {
28
+ return currentSite;
29
+ }
30
+ /**
31
+ * Server-render counterpart of `$watch`.
32
+ *
33
+ * State doesn't need to be reactive on the server, so values aren't wrapped
34
+ * for tracking. Plain objects and arrays are wrapped in a lightweight proxy
35
+ * that only records the property key while an accessor getter runs:
36
+ * `$serverAsync` uses that to identify the getter site across the collect and
37
+ * render passes. Other objects (Maps, class instances, …) are returned
38
+ * untouched — proxying them would break internal slots or private fields, and
39
+ * they rarely carry async getters.
40
+ */
41
+ function $serverWatch(object) {
42
+ if (object === null || object === void 0) return object;
43
+ const prototype = Object.getPrototypeOf(object);
44
+ if (!Array.isArray(object) && prototype !== Object.prototype && prototype !== null) return object;
45
+ return new Proxy(object, { get(target, key, receiver) {
46
+ if (Object.getOwnPropertyDescriptor(target, key)?.get === void 0) return Reflect.get(target, key, receiver);
47
+ const previous = currentSite;
48
+ currentSite = {
49
+ owner: target,
50
+ key
51
+ };
52
+ try {
53
+ return Reflect.get(target, key, receiver);
54
+ } finally {
55
+ currentSite = previous;
56
+ }
57
+ } });
58
+ }
59
+ //#endregion
24
60
  //#region src/ssr/serverAwaitState.ts
25
61
  /**
26
- * The sentinel thrown when the render pass hits a boundary state that the
27
- * collect pass didn't create (or a cursor that ran past the recorded reads).
62
+ * The sentinel thrown when the render pass reads a `source: "server"` getter
63
+ * that the collect pass never recorded (or one whose value hasn't settled).
28
64
  * Only this error degrades the boundary; real render errors propagate.
29
65
  */
30
66
  var ServerAwaitMisaligned = class extends Error {
@@ -57,8 +93,8 @@ function newServerAwaitState() {
57
93
  return {
58
94
  phase: "collect",
59
95
  promises: [],
60
- cursor: 0,
61
- renderReads: 0,
96
+ renderEntries: [],
97
+ renderSeen: /* @__PURE__ */ new Set(),
62
98
  clientReads: 0,
63
99
  children: /* @__PURE__ */ new Map(),
64
100
  counters: /* @__PURE__ */ new Map(),
@@ -93,13 +129,16 @@ function childKey(state, id) {
93
129
  *
94
130
  * - **Collect pass** — calls the thunk (starting the fetch) and records the
95
131
  * promise on the enclosing boundary, so sibling reads start in one wave.
96
- * Returns `undefined`; the speculative pass's output is discarded.
97
- * - **Render pass** — returns the settled value from the recorded promise at
98
- * the same position in read order, without calling the thunk again. A
99
- * rejection is re-thrown here so `@try`/`@error` handles it like any other
100
- * render error. Running past the recorded reads throws a sentinel that
101
- * degrades the boundary (the render pass no longer matches the collect
102
- * pass, so the shipped HTML can't be trusted).
132
+ * The read is keyed by getter identity (watched object + property key), so
133
+ * reading the same getter again reuses the recorded fetch. Returns
134
+ * `undefined`; the speculative pass's output is discarded.
135
+ * - **Render pass** — returns the settled value for the same getter identity,
136
+ * without calling the thunk again. The pass may read a getter any number of
137
+ * times, so markup may branch on the resolved value. A rejection is
138
+ * re-thrown here so `@try`/`@error` handles it like any other render error.
139
+ * Reading a getter the collect pass never touched throws a sentinel that
140
+ * degrades the boundary (its value can't be resolved without fetching, and
141
+ * the shipped HTML can't be trusted).
103
142
  *
104
143
  * Outside any `@await` boundary (including in a `with` branch) a
105
144
  * `source: "server"` read returns `undefined` without fetching — the same
@@ -111,34 +150,46 @@ function $serverAsync(fn, options) {
111
150
  if (state !== null && !state.isRoot && state.phase === "render") state.clientReads++;
112
151
  return;
113
152
  }
153
+ const site = currentServerReadSite();
154
+ const owner = site?.owner ?? null;
155
+ const key = site?.key ?? fn.toString();
114
156
  if (state.phase === "collect") {
115
- const entry = {
116
- promise: fn(),
117
- timeout: options.timeout ?? 5e3
118
- };
119
- entry.promise.then((value) => {
120
- entry.result = {
121
- ok: true,
122
- value
123
- };
124
- }, (error) => {
125
- entry.result = {
126
- ok: false,
127
- error
157
+ if (findEntry(state, owner, key) === void 0) {
158
+ const entry = {
159
+ owner,
160
+ key,
161
+ promise: fn(),
162
+ timeout: options.timeout ?? 5e3
128
163
  };
129
- });
130
- state.promises.push(entry);
164
+ entry.promise.then((value) => {
165
+ entry.result = {
166
+ ok: true,
167
+ value
168
+ };
169
+ }, (error) => {
170
+ entry.result = {
171
+ ok: false,
172
+ error
173
+ };
174
+ });
175
+ state.promises.push(entry);
176
+ }
131
177
  return;
132
178
  }
133
- const index = state.cursor;
134
- if (index >= state.promises.length) throw new ServerAwaitMisaligned();
135
- state.cursor++;
136
- state.renderReads++;
137
- const result = state.promises[index].result;
179
+ const entry = findEntry(state, owner, key);
180
+ if (entry === void 0) throw new ServerAwaitMisaligned();
181
+ if (!state.renderSeen.has(entry)) {
182
+ state.renderSeen.add(entry);
183
+ state.renderEntries.push(entry);
184
+ }
185
+ const result = entry.result;
138
186
  if (result === void 0) throw new ServerAwaitMisaligned();
139
187
  if (result.ok) return result.value;
140
188
  throw result.error;
141
189
  }
190
+ function findEntry(state, owner, key) {
191
+ for (const entry of state.promises) if (entry.owner === owner && entry.key === key) return entry;
192
+ }
142
193
  //#endregion
143
194
  //#region src/ssr/$serverBatch.ts
144
195
  function $serverBatch(fn) {
@@ -200,11 +251,6 @@ function $serverStream(_source, _handler, _options) {}
200
251
  function $serverUnwrap(object) {
201
252
  return object;
202
253
  }
203
- //#endregion
204
- //#region src/ssr/$serverWatch.ts
205
- function $serverWatch(object) {
206
- return object;
207
- }
208
254
  /**
209
255
  * Renders a component's (or a degraded boundary's `with` branch's) markup
210
256
  * with a fresh flush root as the enclosing await state, then substitutes any
@@ -291,12 +337,13 @@ function nextSentinel() {
291
337
  *
292
338
  * The lifecycle: render content once to start the `source: "server"` fetches
293
339
  * (discarding the output), wait for them with a per-getter timeout, then
294
- * render again with the settled values. The second pass ships wrapped in
295
- * hydration markers plus a `<!--t-await:...-->` comment carrying the values
296
- * for the client's `$async` hydration path. Any instability — timeout, a
297
- * render pass that reads a different set of getters, a client-fetch read —
298
- * degrades to the `with` branch + client fetch, exactly the pre-`source`
299
- * behavior.
340
+ * render again with the settled values. Reads are matched by getter identity,
341
+ * so the render pass may read a getter more than once (e.g. branch on it).
342
+ * The second pass ships wrapped in hydration markers plus a
343
+ * `<!--t-await:...-->` comment carrying the values for the client's `$async`
344
+ * hydration path. Any instability — timeout, a render pass that reads a getter
345
+ * the collect pass never touched, a client-fetch read — degrades to the `with`
346
+ * branch + client fetch, exactly the pre-`source` behavior.
300
347
  *
301
348
  * @param id The boundary's site id (unique per `@await` in the component)
302
349
  * @param renderContent Renders the content branch (may read `$async` getters)
@@ -333,13 +380,13 @@ async function renderBoundary(state, renderContent, renderWith) {
333
380
  let content;
334
381
  if (!degraded) {
335
382
  state.phase = "render";
336
- state.cursor = 0;
337
- state.renderReads = 0;
383
+ state.renderEntries.length = 0;
384
+ state.renderSeen.clear();
338
385
  state.clientReads = 0;
339
386
  state.counters.clear();
340
387
  try {
341
388
  content = await withServerAwait(state, renderContent);
342
- if (state.cursor !== state.promises.length || state.clientReads > 0) degraded = true;
389
+ if (state.clientReads > 0) degraded = true;
343
390
  } catch {
344
391
  degraded = true;
345
392
  }
@@ -356,8 +403,8 @@ async function renderBoundary(state, renderContent, renderWith) {
356
403
  }
357
404
  }
358
405
  const values = [];
359
- for (let i = 0; i < state.cursor; i++) {
360
- const result = state.promises[i].result;
406
+ for (const entry of state.renderEntries) {
407
+ const result = entry.result;
361
408
  if (result === void 0) {
362
409
  degraded = true;
363
410
  break;
@@ -375,8 +422,9 @@ async function renderBoundary(state, renderContent, renderWith) {
375
422
  }
376
423
  /**
377
424
  * The `<!--t-await:...-->` comment carrying the boundary's resolved values,
378
- * in read order, for the client's `$async` hydration path. `-->` inside the
379
- * JSON is escaped so the comment can't terminate early.
425
+ * in the order the render pass first read them, for the client's `$async`
426
+ * hydration path. `-->` inside the JSON is escaped so the comment can't
427
+ * terminate early.
380
428
  */
381
429
  function payloadComment(values) {
382
430
  return `<!--t-await:${JSON.stringify(values).replaceAll("-->", "--\\u003E")}-->`;