@bgub/fig 0.1.0-alpha.5 → 0.1.0-alpha.7
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/CHANGELOG.md +61 -0
- package/dist/{element-BVg1bKOz.d.ts → element-Od8CmsBL.d.ts} +4 -3
- package/dist/{element-DmbzNH0T.js → element-wieeFL9f.js} +8 -4
- package/dist/{element-DmbzNH0T.js.map → element-wieeFL9f.js.map} +1 -1
- package/dist/{hooks-B7BTQWiu.d.ts → hooks-BSqKOOez.d.ts} +9 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -3
- package/dist/internal.d.ts +4 -4
- package/dist/internal.js +28 -5
- package/dist/internal.js.map +1 -1
- package/dist/jsx-runtime.d.ts +1 -1
- package/dist/jsx-runtime.js +1 -1
- package/dist/{payload-format-KTNaSI4h.js → payload-format-Dgap54q-.js} +4 -4
- package/dist/payload-format-Dgap54q-.js.map +1 -0
- package/dist/payload.d.ts +5 -3
- package/dist/payload.js +119 -111
- package/dist/payload.js.map +1 -1
- package/dist/{resource-DskjKeuM.js → resource-BfNHyXun.js} +47 -17
- package/dist/resource-BfNHyXun.js.map +1 -0
- package/dist/{transition-CC4kjjrU.js → transition-DBjQTorq.js} +7 -7
- package/dist/transition-DBjQTorq.js.map +1 -0
- package/dist-development/{element-B7mCQIMi.js → element-Bb5fLsPy.js} +8 -4
- package/dist-development/{element-B7mCQIMi.js.map → element-Bb5fLsPy.js.map} +1 -1
- package/dist-development/index.js +3 -3
- package/dist-development/internal.js +28 -5
- package/dist-development/internal.js.map +1 -1
- package/dist-development/jsx-runtime.js +1 -1
- package/dist-development/{payload-format-KTNaSI4h.js → payload-format-Dgap54q-.js} +4 -4
- package/dist-development/payload-format-Dgap54q-.js.map +1 -0
- package/dist-development/payload.js +119 -111
- package/dist-development/payload.js.map +1 -1
- package/dist-development/{resource-CP2OynG0.js → resource-Cp01PvqW.js} +47 -17
- package/dist-development/resource-Cp01PvqW.js.map +1 -0
- package/dist-development/{transition-B4XiOWAj.js → transition-CNVJyoT2.js} +7 -7
- package/dist-development/transition-CNVJyoT2.js.map +1 -0
- package/package.json +1 -1
- package/dist/payload-format-KTNaSI4h.js.map +0 -1
- package/dist/resource-DskjKeuM.js.map +0 -1
- package/dist/transition-CC4kjjrU.js.map +0 -1
- package/dist-development/payload-format-KTNaSI4h.js.map +0 -1
- package/dist-development/resource-CP2OynG0.js.map +0 -1
- package/dist-development/transition-B4XiOWAj.js.map +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { a as decodePayloadRecord, f as errorFromPayloadValue, h as jsonPayloadCodec, i as decodePayloadDataEntries, p as isPayloadSpecialModel, s as decodePayloadValueTag } from "./payload-format-
|
|
4
|
-
//#region src/payload.ts
|
|
1
|
+
import { L as readPromise, f as Fragment, g as createElement, m as ViewTransition, p as Suspense, s as FigElementSymbol } from "./element-Bb5fLsPy.js";
|
|
2
|
+
import { s as assets, t as assetResourceDestination, u as isFigAssetResource, x as trackThenable, y as isThenable } from "./resource-Cp01PvqW.js";
|
|
3
|
+
import { a as decodePayloadRecord, f as errorFromPayloadValue, h as jsonPayloadCodec, i as decodePayloadDataEntries, p as isPayloadSpecialModel, s as decodePayloadValueTag } from "./payload-format-Dgap54q-.js";
|
|
4
|
+
//#region src/payload-client-reference.ts
|
|
5
5
|
const resolverEntries = /* @__PURE__ */ new WeakMap();
|
|
6
6
|
function createPayloadClientReferenceResolver(resolve) {
|
|
7
7
|
const entries = /* @__PURE__ */ new Map();
|
|
@@ -12,8 +12,81 @@ function createPayloadClientReferenceResolver(resolve) {
|
|
|
12
12
|
resolverEntries.set(resolver, entries);
|
|
13
13
|
return resolver;
|
|
14
14
|
}
|
|
15
|
-
const
|
|
16
|
-
|
|
15
|
+
const elementDelivery = /* @__PURE__ */ new WeakMap();
|
|
16
|
+
/** Owns client-reference identity and per-decode delivery state. */
|
|
17
|
+
var PayloadClientReferences = class {
|
|
18
|
+
resolve;
|
|
19
|
+
rows = /* @__PURE__ */ new Map();
|
|
20
|
+
constructor(resolve) {
|
|
21
|
+
this.resolve = resolve;
|
|
22
|
+
}
|
|
23
|
+
register(rowId, reference, gate, assets) {
|
|
24
|
+
const hasDelivery = gate !== null || assets !== null;
|
|
25
|
+
if (hasDelivery) {
|
|
26
|
+
const delivery = {};
|
|
27
|
+
if (gate !== null) delivery.gate = gate;
|
|
28
|
+
if (assets !== null) delivery.assets = assets;
|
|
29
|
+
this.rows.set(rowId, delivery);
|
|
30
|
+
if (gate !== null) gate.then(() => {
|
|
31
|
+
delete delivery.gate;
|
|
32
|
+
if (delivery.assets === void 0) this.rows.delete(rowId);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return this.resolveComponent(reference, hasDelivery);
|
|
36
|
+
}
|
|
37
|
+
attach(rowId, props) {
|
|
38
|
+
const delivery = this.rows.get(rowId);
|
|
39
|
+
if (delivery !== void 0) elementDelivery.set(props, delivery);
|
|
40
|
+
}
|
|
41
|
+
resolveComponent(reference, hasDelivery) {
|
|
42
|
+
const entries = this.resolve === void 0 ? void 0 : resolverEntries.get(this.resolve);
|
|
43
|
+
const existing = entries?.get(reference.id);
|
|
44
|
+
if (existing !== void 0) return existing;
|
|
45
|
+
let resolved;
|
|
46
|
+
try {
|
|
47
|
+
resolved = this.resolve?.(reference);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
resolved = Promise.reject(error);
|
|
50
|
+
}
|
|
51
|
+
if (resolved === void 0) return function PayloadUnresolvedClientComponent() {
|
|
52
|
+
throw new Error(`Cannot render client reference "${reference.id}" because decodePayloadStream was not configured with a matching resolveClientReference.`);
|
|
53
|
+
};
|
|
54
|
+
if (entries === void 0 && !isThenable(resolved) && !hasDelivery) return resolved;
|
|
55
|
+
const component = clientReferenceWrapper(resolved, reference.id);
|
|
56
|
+
entries?.set(reference.id, component);
|
|
57
|
+
return component;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
function clientReferenceWrapper(resolved, referenceId) {
|
|
61
|
+
let render;
|
|
62
|
+
if (isThenable(resolved)) {
|
|
63
|
+
const pending = Promise.resolve(resolved);
|
|
64
|
+
trackThenable(pending);
|
|
65
|
+
let type = null;
|
|
66
|
+
render = (props) => {
|
|
67
|
+
type ??= clientReferenceType(readPromise(pending), referenceId);
|
|
68
|
+
return createElement(type, props);
|
|
69
|
+
};
|
|
70
|
+
} else render = (props) => createElement(resolved, props);
|
|
71
|
+
function PayloadClientContent(content) {
|
|
72
|
+
if (content.gate !== void 0) readPromise(content.gate);
|
|
73
|
+
return render(content.props);
|
|
74
|
+
}
|
|
75
|
+
return function PayloadClientComponent(props) {
|
|
76
|
+
const delivery = elementDelivery.get(props);
|
|
77
|
+
const content = createElement(PayloadClientContent, {
|
|
78
|
+
gate: delivery?.gate,
|
|
79
|
+
props
|
|
80
|
+
});
|
|
81
|
+
return delivery?.assets === void 0 ? content : assets(delivery.assets, content);
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function clientReferenceType(value, id) {
|
|
85
|
+
if (typeof value === "function") return value;
|
|
86
|
+
throw new Error(`Client reference "${id}" did not resolve to a component.`);
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/payload.ts
|
|
17
90
|
var PayloadDecodeAbortedError = class extends Error {
|
|
18
91
|
constructor(reason) {
|
|
19
92
|
super("Payload decode aborted.", reason === void 0 ? void 0 : { cause: reason });
|
|
@@ -31,9 +104,15 @@ var PayloadDecodeAbortedError = class extends Error {
|
|
|
31
104
|
* cancellation reason.
|
|
32
105
|
*/
|
|
33
106
|
function decodePayloadStream(stream, options = {}) {
|
|
34
|
-
return new
|
|
107
|
+
return new PayloadDecoder(stream, options).value;
|
|
35
108
|
}
|
|
36
109
|
const noop = () => void 0;
|
|
110
|
+
function notifyObserver(observer, value) {
|
|
111
|
+
try {
|
|
112
|
+
const result = observer?.(value);
|
|
113
|
+
if (isThenable(result)) Promise.resolve(result).then(noop, noop);
|
|
114
|
+
} catch {}
|
|
115
|
+
}
|
|
37
116
|
function deferred() {
|
|
38
117
|
let resolve = noop;
|
|
39
118
|
let reject = noop;
|
|
@@ -51,16 +130,14 @@ function decodeAssetResources(serialized) {
|
|
|
51
130
|
const assets = serialized.filter(isFigAssetResource);
|
|
52
131
|
return assets.length === 0 ? null : assets;
|
|
53
132
|
}
|
|
54
|
-
var
|
|
133
|
+
var PayloadDecoder = class {
|
|
55
134
|
options;
|
|
56
135
|
value;
|
|
57
136
|
chunks = /* @__PURE__ */ new Map();
|
|
58
137
|
objectRefs = /* @__PURE__ */ new Map();
|
|
59
138
|
rowGates = /* @__PURE__ */ new Map();
|
|
60
139
|
rowAssets = /* @__PURE__ */ new Map();
|
|
61
|
-
|
|
62
|
-
clientRowAssets = /* @__PURE__ */ new Map();
|
|
63
|
-
rowDecoder;
|
|
140
|
+
clientReferences;
|
|
64
141
|
reader = null;
|
|
65
142
|
done = false;
|
|
66
143
|
gateRelease = deferred();
|
|
@@ -75,9 +152,10 @@ var PayloadStreamDecode = class {
|
|
|
75
152
|
};
|
|
76
153
|
constructor(stream, options) {
|
|
77
154
|
this.options = options;
|
|
78
|
-
this.
|
|
155
|
+
this.clientReferences = new PayloadClientReferences(options.resolveClientReference);
|
|
156
|
+
const rowDecoder = jsonPayloadCodec.createDecoder((row) => this.handleRow(row));
|
|
79
157
|
this.value = this.chunkPromise(this.getChunk(0));
|
|
80
|
-
this.ingest(stream);
|
|
158
|
+
this.ingest(stream, rowDecoder);
|
|
81
159
|
const signal = options.signal;
|
|
82
160
|
if (signal !== void 0) if (signal.aborted) this.abort(signal.reason);
|
|
83
161
|
else {
|
|
@@ -94,7 +172,7 @@ var PayloadStreamDecode = class {
|
|
|
94
172
|
this.rejectUnresolved(error);
|
|
95
173
|
this.settle({ status: "aborted" });
|
|
96
174
|
}
|
|
97
|
-
async ingest(stream) {
|
|
175
|
+
async ingest(stream, rowDecoder) {
|
|
98
176
|
const reader = stream.getReader();
|
|
99
177
|
this.reader = reader;
|
|
100
178
|
try {
|
|
@@ -102,9 +180,9 @@ var PayloadStreamDecode = class {
|
|
|
102
180
|
const { done, value } = await reader.read();
|
|
103
181
|
if (this.done) return;
|
|
104
182
|
if (done) break;
|
|
105
|
-
|
|
183
|
+
rowDecoder.decode(value);
|
|
106
184
|
}
|
|
107
|
-
|
|
185
|
+
rowDecoder.flush();
|
|
108
186
|
this.finishIngestion();
|
|
109
187
|
} catch (error) {
|
|
110
188
|
if (this.done) return;
|
|
@@ -113,12 +191,7 @@ var PayloadStreamDecode = class {
|
|
|
113
191
|
}
|
|
114
192
|
}
|
|
115
193
|
finishIngestion() {
|
|
116
|
-
let truncated = false;
|
|
117
194
|
for (const chunk of this.chunks.values()) if (!chunk.arrived) {
|
|
118
|
-
truncated = true;
|
|
119
|
-
break;
|
|
120
|
-
}
|
|
121
|
-
if (truncated) {
|
|
122
195
|
this.failIngestion(/* @__PURE__ */ new Error("Payload stream ended before all referenced rows arrived."));
|
|
123
196
|
return;
|
|
124
197
|
}
|
|
@@ -133,20 +206,17 @@ var PayloadStreamDecode = class {
|
|
|
133
206
|
});
|
|
134
207
|
}
|
|
135
208
|
rejectUnresolved(error) {
|
|
136
|
-
for (const chunk of this.chunks
|
|
209
|
+
for (const [id, chunk] of this.chunks) {
|
|
137
210
|
if (chunk.arrived) continue;
|
|
138
211
|
chunk.arrived = true;
|
|
139
|
-
this.rejectChunk(chunk, error);
|
|
212
|
+
this.rejectChunk(id, chunk, error);
|
|
140
213
|
}
|
|
141
214
|
}
|
|
142
215
|
settle(result) {
|
|
143
216
|
if (this.done) return;
|
|
144
217
|
this.done = true;
|
|
145
218
|
this.removeAbortListener();
|
|
146
|
-
|
|
147
|
-
const observed = this.options.onStreamDone?.(result);
|
|
148
|
-
if (isThenable(observed)) Promise.resolve(observed).then(noop, noop);
|
|
149
|
-
} catch {}
|
|
219
|
+
notifyObserver(this.options.onStreamDone, result);
|
|
150
220
|
}
|
|
151
221
|
handleRow(row) {
|
|
152
222
|
if (this.done) return;
|
|
@@ -177,16 +247,8 @@ var PayloadStreamDecode = class {
|
|
|
177
247
|
const assets = decodeAssetResources(row.value.assets);
|
|
178
248
|
if (assets !== null) reference.assets = assets;
|
|
179
249
|
const retainedAssets = this.retainedAssets(assets);
|
|
180
|
-
if (retainedAssets !== null) this.clientRowAssets.set(row.id, retainedAssets);
|
|
181
250
|
const gate = this.prepareAssets(assets);
|
|
182
|
-
|
|
183
|
-
trackThenable(gate);
|
|
184
|
-
this.clientRowGates.set(row.id, gate);
|
|
185
|
-
gate.then(() => {
|
|
186
|
-
this.clientRowGates.delete(row.id);
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
const component = this.clientReferenceComponent(reference, gate, retainedAssets !== null);
|
|
251
|
+
const component = this.clientReferences.register(row.id, reference, gate, retainedAssets);
|
|
190
252
|
chunk.arrived = true;
|
|
191
253
|
this.fulfillChunk(chunk, component);
|
|
192
254
|
return;
|
|
@@ -196,7 +258,7 @@ var PayloadStreamDecode = class {
|
|
|
196
258
|
chunk.arrived = true;
|
|
197
259
|
this.rowGates.delete(row.id);
|
|
198
260
|
this.rowAssets.delete(row.id);
|
|
199
|
-
this.rejectChunk(chunk, errorFromPayloadValue(row.value));
|
|
261
|
+
this.rejectChunk(row.id, chunk, errorFromPayloadValue(row.value));
|
|
200
262
|
return;
|
|
201
263
|
}
|
|
202
264
|
case "data": {
|
|
@@ -210,7 +272,8 @@ var PayloadStreamDecode = class {
|
|
|
210
272
|
const retainedAssets = this.retainedAssets(decodedAssets);
|
|
211
273
|
if (retainedAssets !== null && row.for !== void 0) {
|
|
212
274
|
const retained = this.rowAssets.get(row.for);
|
|
213
|
-
this.rowAssets.set(row.for,
|
|
275
|
+
if (retained === void 0) this.rowAssets.set(row.for, retainedAssets);
|
|
276
|
+
else retained.push(...retainedAssets);
|
|
214
277
|
}
|
|
215
278
|
const gate = this.prepareAssets(decodedAssets);
|
|
216
279
|
if (gate === null || row.for === void 0) return;
|
|
@@ -242,33 +305,14 @@ var PayloadStreamDecode = class {
|
|
|
242
305
|
const retained = this.options.retainAssets === true ? assets : assets.filter((resource) => assetResourceDestination(resource) === "head");
|
|
243
306
|
return retained.length === 0 ? null : retained;
|
|
244
307
|
}
|
|
245
|
-
clientReferenceComponent(reference, gate, retainsAssets) {
|
|
246
|
-
const resolve = this.options.resolveClientReference;
|
|
247
|
-
const entries = resolve === void 0 ? void 0 : resolverEntries.get(resolve);
|
|
248
|
-
const existing = entries?.get(reference.id);
|
|
249
|
-
if (existing !== void 0) return existing;
|
|
250
|
-
let resolved;
|
|
251
|
-
try {
|
|
252
|
-
resolved = resolve?.(reference);
|
|
253
|
-
} catch (error) {
|
|
254
|
-
resolved = Promise.reject(error);
|
|
255
|
-
}
|
|
256
|
-
if (resolved === void 0) return function PayloadUnresolvedClientComponent() {
|
|
257
|
-
throw new Error(`Cannot render client reference "${reference.id}" because decodePayloadStream was not configured with a matching resolveClientReference.`);
|
|
258
|
-
};
|
|
259
|
-
if (entries === void 0 && gate === null && !isThenable(resolved) && !retainsAssets) return resolved;
|
|
260
|
-
const component = clientReferenceWrapper(resolved, reference.id);
|
|
261
|
-
entries?.set(reference.id, component);
|
|
262
|
-
return component;
|
|
263
|
-
}
|
|
264
308
|
getChunk(id) {
|
|
265
309
|
const existing = this.chunks.get(id);
|
|
266
310
|
if (existing !== void 0) return existing;
|
|
267
311
|
const chunk = {
|
|
268
312
|
arrived: false,
|
|
269
|
-
deferred: null,
|
|
270
|
-
id,
|
|
271
313
|
promise: null,
|
|
314
|
+
reject: null,
|
|
315
|
+
resolve: null,
|
|
272
316
|
result: { status: "pending" }
|
|
273
317
|
};
|
|
274
318
|
this.chunks.set(id, chunk);
|
|
@@ -280,10 +324,10 @@ var PayloadStreamDecode = class {
|
|
|
280
324
|
else if (chunk.result.status === "rejected") {
|
|
281
325
|
chunk.promise = Promise.reject(chunk.result.error);
|
|
282
326
|
chunk.promise.catch(noop);
|
|
283
|
-
} else {
|
|
284
|
-
chunk.
|
|
285
|
-
chunk.
|
|
286
|
-
}
|
|
327
|
+
} else chunk.promise = new Promise((resolve, reject) => {
|
|
328
|
+
chunk.resolve = resolve;
|
|
329
|
+
chunk.reject = reject;
|
|
330
|
+
});
|
|
287
331
|
trackThenable(chunk.promise);
|
|
288
332
|
return chunk.promise;
|
|
289
333
|
}
|
|
@@ -293,25 +337,23 @@ var PayloadStreamDecode = class {
|
|
|
293
337
|
status: "fulfilled",
|
|
294
338
|
value
|
|
295
339
|
};
|
|
296
|
-
chunk.
|
|
340
|
+
chunk.resolve?.(value);
|
|
341
|
+
chunk.resolve = null;
|
|
342
|
+
chunk.reject = null;
|
|
297
343
|
}
|
|
298
|
-
rejectChunk(chunk, error) {
|
|
344
|
+
rejectChunk(id, chunk, error) {
|
|
299
345
|
if (chunk.result.status !== "pending") return;
|
|
300
346
|
chunk.result = {
|
|
301
347
|
error,
|
|
302
348
|
status: "rejected"
|
|
303
349
|
};
|
|
304
|
-
if (chunk.
|
|
305
|
-
chunk.
|
|
350
|
+
if (chunk.reject !== null) {
|
|
351
|
+
chunk.reject(error);
|
|
306
352
|
chunk.promise?.catch(noop);
|
|
307
353
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
try {
|
|
312
|
-
const observed = this.options.onHoleError?.(error);
|
|
313
|
-
if (isThenable(observed)) Promise.resolve(observed).then(noop, noop);
|
|
314
|
-
} catch {}
|
|
354
|
+
chunk.resolve = null;
|
|
355
|
+
chunk.reject = null;
|
|
356
|
+
if (id !== 0 && !(error instanceof PayloadDecodeAbortedError)) notifyObserver(this.options.onHoleError, error);
|
|
315
357
|
}
|
|
316
358
|
readChunkForRender(id) {
|
|
317
359
|
const chunk = this.getChunk(id);
|
|
@@ -355,7 +397,7 @@ var PayloadStreamDecode = class {
|
|
|
355
397
|
case "fragment": return Fragment;
|
|
356
398
|
case "lazy":
|
|
357
399
|
this.getChunk(model.id);
|
|
358
|
-
return createElement(
|
|
400
|
+
return createElement(PayloadHole, {
|
|
359
401
|
decode: this,
|
|
360
402
|
id: model.id
|
|
361
403
|
});
|
|
@@ -371,10 +413,7 @@ var PayloadStreamDecode = class {
|
|
|
371
413
|
}
|
|
372
414
|
attachElementDelivery(typeModel, props) {
|
|
373
415
|
if (typeof typeModel === "string" || typeModel.$fig !== "client") return;
|
|
374
|
-
|
|
375
|
-
if (gate !== void 0) elementGates.set(props, gate);
|
|
376
|
-
const assets = this.clientRowAssets.get(typeModel.id);
|
|
377
|
-
if (assets !== void 0) elementAssets.set(props, assets);
|
|
416
|
+
this.clientReferences.attach(typeModel.id, props);
|
|
378
417
|
}
|
|
379
418
|
defineObjectRef(id, create, fill) {
|
|
380
419
|
if (this.objectRefs.has(id)) return this.objectRefs.get(id);
|
|
@@ -389,40 +428,9 @@ var PayloadStreamDecode = class {
|
|
|
389
428
|
}
|
|
390
429
|
}
|
|
391
430
|
};
|
|
392
|
-
function
|
|
431
|
+
function PayloadHole(props) {
|
|
393
432
|
return props.decode.readChunkForRender(props.id);
|
|
394
433
|
}
|
|
395
|
-
function clientReferenceWrapper(resolved, referenceId) {
|
|
396
|
-
let render;
|
|
397
|
-
if (!isThenable(resolved)) render = (props) => createElement(resolved, props);
|
|
398
|
-
else {
|
|
399
|
-
const pending = Promise.resolve(resolved);
|
|
400
|
-
trackThenable(pending);
|
|
401
|
-
let type = null;
|
|
402
|
-
render = (props) => {
|
|
403
|
-
if (type === null) type = clientReferenceType(readPromise(pending), referenceId);
|
|
404
|
-
return createElement(type, props);
|
|
405
|
-
};
|
|
406
|
-
}
|
|
407
|
-
function PayloadClientContent(content) {
|
|
408
|
-
if (content.gate !== void 0) readPromise(content.gate);
|
|
409
|
-
return render(content.props);
|
|
410
|
-
}
|
|
411
|
-
return function PayloadClientComponent(props) {
|
|
412
|
-
return attachElementAssets(props, createElement(PayloadClientContent, {
|
|
413
|
-
gate: elementGates.get(props),
|
|
414
|
-
props
|
|
415
|
-
}));
|
|
416
|
-
};
|
|
417
|
-
}
|
|
418
|
-
function attachElementAssets(props, node) {
|
|
419
|
-
const resources = elementAssets.get(props);
|
|
420
|
-
return resources === void 0 ? node : assets(resources, node);
|
|
421
|
-
}
|
|
422
|
-
function clientReferenceType(value, id) {
|
|
423
|
-
if (typeof value === "function") return value;
|
|
424
|
-
throw new Error(`Client reference "${id}" did not resolve to a component.`);
|
|
425
|
-
}
|
|
426
434
|
//#endregion
|
|
427
435
|
export { createPayloadClientReferenceResolver, decodePayloadStream };
|
|
428
436
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"payload.js","names":["attachAssets"],"sources":["../src/payload.ts"],"sourcesContent":["import type { FigDataHydrationEntry } from \"./data.ts\";\nimport {\n type AwaitedFigNode,\n createElement,\n type ElementType,\n FigElementSymbol,\n type FigNode,\n Fragment,\n type Key,\n type Props,\n Suspense,\n ViewTransition,\n} from \"./element.ts\";\nimport { readPromise } from \"./hooks.ts\";\nimport {\n decodePayloadDataEntries,\n decodePayloadRecord,\n decodePayloadValueTag,\n errorFromPayloadValue,\n isPayloadSpecialModel,\n jsonPayloadCodec,\n type PayloadDecodeRefs,\n type PayloadElementModel,\n type PayloadModel,\n type PayloadRow,\n type PayloadRowDecoder,\n type PayloadSpecialModel,\n type SerializedAssetResource,\n} from \"./payload-format.ts\";\nimport {\n assetResourceDestination,\n assets as attachAssets,\n type FigAssetResource,\n isFigAssetResource,\n} from \"./resource.ts\";\nimport { isThenable, trackThenable } from \"./thenables.ts\";\n\nexport interface PayloadClientReference {\n assets?: readonly FigAssetResource[];\n exportName?: string;\n id: string;\n ssr?: boolean;\n}\n\nexport type ResolveClientReference = (\n reference: PayloadClientReference,\n) => ElementType<any> | PromiseLike<ElementType<any>> | undefined;\n\n/**\n * A caller-owned stateful resolver: a `ResolveClientReference` that also\n * owns component identity. Decodes given one (as `resolveClientReference`)\n * resolve every client reference to a single resolver-owned wrapper per\n * reference id, so re-decoding a payload updates islands in place instead\n * of remounting them. The caller owns the lifetime: drop entries when their\n * modules change (HMR) or the manifest swaps.\n */\nexport interface PayloadClientReferenceResolver {\n (\n reference: PayloadClientReference,\n ): ElementType<any> | PromiseLike<ElementType<any>> | undefined;\n clear(): void;\n delete(id: string): boolean;\n}\n\nconst resolverEntries = new WeakMap<\n ResolveClientReference,\n Map<string, ElementType<any>>\n>();\n\nexport function createPayloadClientReferenceResolver(\n resolve: ResolveClientReference,\n): PayloadClientReferenceResolver {\n const entries = new Map<string, ElementType<any>>();\n const resolver = Object.assign(\n (reference: PayloadClientReference) => resolve(reference),\n {\n clear: (): void => entries.clear(),\n delete: (id: string): boolean => entries.delete(id),\n },\n );\n resolverEntries.set(resolver, entries);\n return resolver;\n}\n\n// Reveal gates ride the decoded element instances, not the reference\n// wrapper: each decode attaches its own gate to the elements it\n// materializes. Identity lives on the component type; the asset dependency\n// lives on the element — so a mounted island (a previous decode's elements)\n// can never be re-suspended by a newer decode's pending assets, while the\n// newer decode's elements gate on exactly the assets they declared.\n// Elements minted outside a decode from a cached component carry no gate\n// and render ungated: they declared no dependency.\nconst elementGates = new WeakMap<Props, Promise<void>>();\nconst elementAssets = new WeakMap<Props, readonly FigAssetResource[]>();\n\nexport type PayloadDecodeCompletion =\n | { status: \"aborted\" }\n | { status: \"complete\" }\n | { status: \"failed\"; error: unknown };\n\nexport interface PayloadDecodeOptions {\n /**\n * Receives decoded `data` rows for hydration into a data store. The\n * capability itself is expected to be generation-guarded and to ignore\n * entries after its caller loses authority.\n */\n hydrate?: (entries: readonly FigDataHydrationEntry[]) => void;\n /**\n * Observes every outlined hole rejection — an `error` row or a stream\n * failure stranding referenced rows, before or after the root fulfills.\n * Abort cancellation is excluded. Called once per rejected hole; the\n * observer is never awaited and cannot break decoding.\n */\n onHoleError?: (error: unknown) => unknown;\n /**\n * Observes the end of ingestion: called exactly once when the stream\n * settles as complete, failed, or aborted. Post-root failures reject the\n * holes they strand, but a failure with no pending slot is otherwise\n * invisible — this is the hook for reporting it. The callback is never\n * awaited, and its exceptions and rejections are swallowed, so an\n * observer cannot block or break decode teardown.\n */\n onStreamDone?: (result: PayloadDecodeCompletion) => unknown;\n /**\n * Called with delivery asset resources as soon as their rows arrive\n * (e.g. fig-dom's insertAssetResources). A returned promise gates the\n * reveal of only the content that declared a dependency on those assets;\n * gate settlement — fulfilled or rejected — releases the reveal, so a\n * failed asset never blocks content.\n */\n prepareAssets?: (\n assets: readonly FigAssetResource[],\n ) => void | PromiseLike<void>;\n /**\n * Retains delivery asset dependencies as `assets(...)` declarations on\n * their decoded owners. Document metadata is always retained because its\n * owner may mutate the document only when it commits. Server document\n * renderers enable this option to retain delivery assets too.\n */\n retainAssets?: boolean;\n /**\n * Resolves client-reference rows to components. A plain function keeps\n * identity per decode: gated and asynchronously resolved references decode\n * to per-decode wrappers and remount on re-decode. A stateful resolver\n * (created by `createPayloadClientReferenceResolver`) keeps every\n * resolvable reference's identity stable across the decodes sharing it.\n */\n resolveClientReference?: ResolveClientReference;\n signal?: AbortSignal;\n}\n\nclass PayloadDecodeAbortedError extends Error {\n constructor(reason?: unknown) {\n super(\n \"Payload decode aborted.\",\n reason === undefined ? undefined : { cause: reason },\n );\n this.name = \"PayloadDecodeAbortedError\";\n }\n}\n\n/**\n * Decode a payload row stream. The returned promise resolves with the\n * decoded root FigNode as soon as the root row decodes (and rejects only\n * when the stream fails before producing a root value, or with the root\n * row's own error); decoding continues in the background, filling outlined\n * holes as their rows arrive. Post-root failures reject the holes they\n * strand and report through `onStreamDone`. Aborting `options.signal`\n * ignores remaining rows and rejects unresolved holes with an internal\n * cancellation reason.\n */\nexport function decodePayloadStream(\n stream: ReadableStream<Uint8Array>,\n options: PayloadDecodeOptions = {},\n): Promise<AwaitedFigNode> {\n return new PayloadStreamDecode(stream, options).value;\n}\n\ntype DecodeChunk = {\n // The chunk's row has been ingested (decoded or rejected); reveal may still\n // be waiting on an asset gate. Truncation and abort reject only chunks\n // whose rows never arrived.\n arrived: boolean;\n // Materialized lazily: most rows settle synchronously at arrival and are\n // only ever read through result, so eagerly allocating a promise and\n // its controls per row would be waste.\n deferred: Deferred<unknown> | null;\n id: number;\n promise: Promise<unknown> | null;\n result:\n | { status: \"pending\" }\n | { status: \"fulfilled\"; value: unknown }\n | { status: \"rejected\"; error: unknown };\n};\n\ntype DecodingElement = {\n $$typeof: symbol;\n key: Key | null;\n props: Props;\n type: ElementType<any>;\n};\n\nconst noop = (): void => undefined;\n\ninterface Deferred<T> {\n promise: Promise<T>;\n reject: (reason: unknown) => void;\n resolve: (value: T) => void;\n}\n\nfunction deferred<T>(): Deferred<T> {\n let resolve: Deferred<T>[\"resolve\"] = noop;\n let reject: Deferred<T>[\"reject\"] = noop;\n const promise = new Promise<T>((innerResolve, innerReject) => {\n resolve = innerResolve;\n reject = innerReject;\n });\n return { promise, reject, resolve };\n}\n\nfunction decodeAssetResources(\n serialized: readonly SerializedAssetResource[] | undefined,\n): FigAssetResource[] | null {\n if (serialized === undefined) return null;\n const assets = serialized.filter(isFigAssetResource);\n return assets.length === 0 ? null : assets;\n}\n\nclass PayloadStreamDecode {\n readonly value: Promise<AwaitedFigNode>;\n\n private readonly chunks = new Map<number, DecodeChunk>();\n private readonly objectRefs = new Map<number, unknown>();\n // Asset gates registered for a row id (assets rows carry `for`); consumed\n // when that row arrives.\n private readonly rowGates = new Map<number, Array<PromiseLike<void>>>();\n private readonly rowAssets = new Map<number, readonly FigAssetResource[]>();\n // Unsettled reveal gates for arrived client rows, attached per element as\n // models referencing the row materialize (see elementGates).\n private readonly clientRowGates = new Map<number, Promise<void>>();\n private readonly clientRowAssets = new Map<\n number,\n readonly FigAssetResource[]\n >();\n private readonly rowDecoder: PayloadRowDecoder;\n private reader: ReadableStreamDefaultReader<Uint8Array> | null = null;\n private done = false;\n // Resolved on abort so arrived-but-gated chunks reveal instead of waiting\n // for asset gates that may never settle.\n private readonly gateRelease = deferred<void>();\n private removeAbortListener: () => void = noop;\n // One closure and one refs adapter reused across every decoded model, so\n // the per-node decode loop allocates only the decoded values themselves.\n private readonly decodeChild = (model: PayloadModel): unknown =>\n this.decodeModel(model);\n private readonly valueRefs: PayloadDecodeRefs = {\n define: (id, create, fill) => this.defineObjectRef(id, create, fill),\n read: (id) => {\n if (!this.objectRefs.has(id)) {\n throw new Error(`Payload referenced unknown object id ${id}.`);\n }\n return this.objectRefs.get(id);\n },\n };\n\n constructor(\n stream: ReadableStream<Uint8Array>,\n private readonly options: PayloadDecodeOptions,\n ) {\n this.rowDecoder = jsonPayloadCodec.createDecoder((row) =>\n this.handleRow(row),\n );\n this.value = this.chunkPromise(this.getChunk(0)) as Promise<AwaitedFigNode>;\n\n void this.ingest(stream);\n\n const signal = options.signal;\n if (signal !== undefined) {\n if (signal.aborted) {\n this.abort(signal.reason);\n } else {\n const onAbort = () => this.abort(signal.reason);\n signal.addEventListener(\"abort\", onAbort, { once: true });\n this.removeAbortListener = () =>\n signal.removeEventListener(\"abort\", onAbort);\n }\n }\n }\n\n abort(reason?: unknown): void {\n if (this.done) return;\n const error = new PayloadDecodeAbortedError(reason);\n this.gateRelease.resolve(undefined);\n void this.reader?.cancel(error).catch(noop);\n this.rejectUnresolved(error);\n this.settle({ status: \"aborted\" });\n }\n\n private async ingest(stream: ReadableStream<Uint8Array>): Promise<void> {\n const reader = stream.getReader();\n this.reader = reader;\n\n try {\n for (;;) {\n const { done, value } = await reader.read();\n if (this.done) return;\n if (done) break;\n this.rowDecoder.decode(value);\n }\n this.rowDecoder.flush();\n this.finishIngestion();\n } catch (error) {\n if (this.done) return;\n void reader.cancel(error).catch(noop);\n this.failIngestion(error);\n }\n }\n\n private finishIngestion(): void {\n let truncated = false;\n for (const chunk of this.chunks.values()) {\n if (!chunk.arrived) {\n truncated = true;\n break;\n }\n }\n if (truncated) {\n // The server closes the stream only after every outlined row has been\n // written, so an unresolved reference at end-of-stream is truncation.\n this.failIngestion(\n new Error(\"Payload stream ended before all referenced rows arrived.\"),\n );\n return;\n }\n this.settle({ status: \"complete\" });\n }\n\n private failIngestion(error: unknown): void {\n if (this.done) return;\n this.rejectUnresolved(error);\n this.settle({ status: \"failed\", error });\n }\n\n private rejectUnresolved(error: unknown): void {\n for (const chunk of this.chunks.values()) {\n if (chunk.arrived) continue;\n chunk.arrived = true;\n this.rejectChunk(chunk, error);\n }\n }\n\n private settle(result: PayloadDecodeCompletion): void {\n if (this.done) return;\n this.done = true;\n this.removeAbortListener();\n try {\n const observed = this.options.onStreamDone?.(result);\n // An async observer's rejection must not surface as an unhandled\n // rejection any more than a sync throw may break teardown.\n if (isThenable(observed)) void Promise.resolve(observed).then(noop, noop);\n } catch {\n // An observer must not be able to break ingestion teardown.\n }\n }\n\n private handleRow(row: PayloadRow): void {\n if (this.done) return;\n\n switch (row.tag) {\n case \"model\": {\n const chunk = this.getChunk(row.id);\n let decoded = this.decodeModel(row.value);\n const retainedAssets = this.rowAssets.get(row.id);\n if (retainedAssets !== undefined) {\n this.rowAssets.delete(row.id);\n decoded = attachAssets(retainedAssets, decoded as FigNode);\n }\n chunk.arrived = true;\n const gates = this.rowGates.get(row.id);\n if (gates === undefined) {\n this.fulfillChunk(chunk, decoded);\n return;\n }\n this.rowGates.delete(row.id);\n void Promise.race([Promise.all(gates), this.gateRelease.promise]).then(\n () => this.fulfillChunk(chunk, decoded),\n );\n return;\n }\n case \"client\": {\n const chunk = this.getChunk(row.id);\n const reference: PayloadClientReference = { id: row.value.id };\n if (row.value.exportName !== undefined) {\n reference.exportName = row.value.exportName;\n }\n if (row.value.ssr === true) reference.ssr = true;\n const assets = decodeAssetResources(row.value.assets);\n if (assets !== null) {\n reference.assets = assets;\n }\n const retainedAssets = this.retainedAssets(assets);\n if (retainedAssets !== null) {\n this.clientRowAssets.set(row.id, retainedAssets);\n }\n const gate = this.prepareAssets(assets);\n if (gate !== null) {\n // Elements referencing this row inherit the gate as they\n // materialize; once it settles there is nothing left to gate.\n // Track it now so a gate that settles before its first render\n // read (e.g. awaited by a router's pre-commit prepare) resolves\n // synchronously instead of suspending for a retry beat.\n trackThenable(gate);\n this.clientRowGates.set(row.id, gate);\n void gate.then(() => {\n this.clientRowGates.delete(row.id);\n });\n }\n const component = this.clientReferenceComponent(\n reference,\n gate,\n retainedAssets !== null,\n );\n chunk.arrived = true;\n this.fulfillChunk(chunk, component);\n return;\n }\n case \"error\": {\n const chunk = this.getChunk(row.id);\n chunk.arrived = true;\n // Reveal-gating a failure is pointless; drop any gates aimed here.\n this.rowGates.delete(row.id);\n this.rowAssets.delete(row.id);\n this.rejectChunk(chunk, errorFromPayloadValue(row.value));\n return;\n }\n case \"data\": {\n const hydrate = this.options.hydrate;\n if (hydrate === undefined) return;\n hydrate(decodePayloadDataEntries(row.value));\n return;\n }\n case \"assets\": {\n const decodedAssets = decodeAssetResources(row.value);\n const retainedAssets = this.retainedAssets(decodedAssets);\n if (retainedAssets !== null && row.for !== undefined) {\n const retained = this.rowAssets.get(row.for);\n this.rowAssets.set(\n row.for,\n retained === undefined\n ? retainedAssets\n : [...retained, ...retainedAssets],\n );\n }\n const gate = this.prepareAssets(decodedAssets);\n if (gate === null || row.for === undefined) return;\n const gates = this.rowGates.get(row.for);\n if (gates === undefined) this.rowGates.set(row.for, [gate]);\n else gates.push(gate);\n return;\n }\n }\n }\n\n // Never rejects and never blocks content on a failed asset: a rejected\n // prepareAssets result (or synchronous throw) settles the gate.\n private prepareAssets(\n assets: readonly FigAssetResource[] | null,\n ): Promise<void> | null {\n const prepare = this.options.prepareAssets;\n if (prepare === undefined || assets === null) return null;\n const delivery = assets.filter(\n (resource) => assetResourceDestination(resource) === \"stream\",\n );\n if (delivery.length === 0) return null;\n\n let result: void | PromiseLike<void>;\n try {\n result = prepare(delivery);\n } catch {\n return null;\n }\n if (!isThenable(result)) return null;\n const gate = Promise.resolve(result).then(noop, noop);\n trackThenable(gate);\n return gate;\n }\n\n private retainedAssets(\n assets: readonly FigAssetResource[] | null,\n ): readonly FigAssetResource[] | null {\n if (assets === null) return null;\n const retained =\n this.options.retainAssets === true\n ? assets\n : assets.filter(\n (resource) => assetResourceDestination(resource) === \"head\",\n );\n return retained.length === 0 ? null : retained;\n }\n\n private clientReferenceComponent(\n reference: PayloadClientReference,\n gate: Promise<void> | null,\n retainsAssets: boolean,\n ): ElementType<any> {\n const resolve = this.options.resolveClientReference;\n const entries =\n resolve === undefined ? undefined : resolverEntries.get(resolve);\n const existing = entries?.get(reference.id);\n if (existing !== undefined) return existing;\n\n let resolved: ReturnType<ResolveClientReference>;\n try {\n resolved = resolve?.(reference);\n } catch (error) {\n resolved = Promise.reject(error);\n }\n\n if (resolved === undefined) {\n // Never cached: a stateful resolver that cannot resolve this\n // reference must not latch the error component for later decodes\n // that can.\n return function PayloadUnresolvedClientComponent(): never {\n throw new Error(\n `Cannot render client reference \"${reference.id}\" because decodePayloadStream was not configured with a matching resolveClientReference.`,\n );\n };\n }\n\n // Without a stateful resolver, an ungated synchronously resolved\n // reference decodes to the component itself, so its element type is\n // stable across decodes whenever the resolver's answer is — re-decoding\n // updates the client component in place and its state survives.\n if (\n entries === undefined &&\n gate === null &&\n !isThenable(resolved) &&\n !retainsAssets\n ) {\n return resolved;\n }\n\n const component = clientReferenceWrapper(resolved, reference.id);\n entries?.set(reference.id, component);\n return component;\n }\n\n private getChunk(id: number): DecodeChunk {\n const existing = this.chunks.get(id);\n if (existing !== undefined) return existing;\n\n const chunk: DecodeChunk = {\n arrived: false,\n deferred: null,\n id,\n promise: null,\n result: { status: \"pending\" },\n };\n this.chunks.set(id, chunk);\n return chunk;\n }\n\n // Materializes (and registers with the thenable registry) on first access:\n // a settled chunk becomes an already-settled promise, a pending one gets\n // live resolvers that fulfillChunk/rejectChunk drive.\n private chunkPromise(chunk: DecodeChunk): Promise<unknown> {\n if (chunk.promise !== null) return chunk.promise;\n\n if (chunk.result.status === \"fulfilled\") {\n chunk.promise = Promise.resolve(chunk.result.value);\n } else if (chunk.result.status === \"rejected\") {\n chunk.promise = Promise.reject(chunk.result.error);\n // Holes nobody awaits must not become unhandled rejections; readers\n // still observe the stored error.\n void chunk.promise.catch(noop);\n } else {\n chunk.deferred = deferred<unknown>();\n chunk.promise = chunk.deferred.promise;\n }\n trackThenable(chunk.promise);\n return chunk.promise;\n }\n\n private fulfillChunk(chunk: DecodeChunk, value: unknown): void {\n if (chunk.result.status !== \"pending\") return;\n chunk.result = { status: \"fulfilled\", value };\n chunk.deferred?.resolve(value);\n }\n\n private rejectChunk(chunk: DecodeChunk, error: unknown): void {\n if (chunk.result.status !== \"pending\") return;\n chunk.result = { error, status: \"rejected\" };\n if (chunk.deferred !== null) {\n chunk.deferred.reject(error);\n void chunk.promise?.catch(noop);\n }\n if (chunk.id !== 0 && !(error instanceof PayloadDecodeAbortedError)) {\n this.observeHoleError(error);\n }\n }\n\n private observeHoleError(error: unknown): void {\n try {\n const observed = this.options.onHoleError?.(error);\n if (isThenable(observed)) void Promise.resolve(observed).then(noop, noop);\n } catch {\n // Error attribution/reporting is observational and cannot break decode.\n }\n }\n\n readChunkForRender(id: number): unknown {\n const chunk = this.getChunk(id);\n if (chunk.result.status === \"rejected\") throw chunk.result.error;\n if (chunk.result.status === \"pending\")\n return readPromise(this.chunkPromise(chunk));\n return chunk.result.value;\n }\n\n private decodeModel(model: PayloadModel): unknown {\n if (model === null) return null;\n if (Array.isArray(model)) return model.map(this.decodeChild);\n if (typeof model !== \"object\") return model;\n\n if (isPayloadSpecialModel(model)) return this.decodeSpecialModel(model);\n\n return decodePayloadRecord(model, this.decodeChild);\n }\n\n private decodeSpecialModel(\n model: PayloadElementModel | PayloadSpecialModel,\n ): unknown {\n switch (model.$fig) {\n case \"element\": {\n if (model.id !== undefined) {\n return this.defineObjectRef(\n model.id,\n (): DecodingElement => ({\n $$typeof: FigElementSymbol,\n key: model.key,\n props: {},\n type: Fragment,\n }),\n (element) => {\n element.type = this.decodeElementType(model.type);\n const props = this.decodeModel(model.props) as Props;\n element.props = props;\n this.attachElementDelivery(model.type, props);\n },\n );\n }\n const type = this.decodeElementType(model.type);\n const props = this.decodeModel(model.props) as Props & {\n key?: Key | null;\n };\n if (model.key !== null) props.key = model.key;\n const element = createElement(type, props);\n // createElement copies props, so the gate keys the element's own\n // props object — the one the component will receive.\n this.attachElementDelivery(model.type, element.props);\n return element;\n }\n case \"client\": {\n const chunk = this.chunks.get(model.id);\n if (chunk === undefined || chunk.result.status !== \"fulfilled\") {\n throw new Error(\n `Payload model referenced client row ${model.id} before it arrived.`,\n );\n }\n return chunk.result.value;\n }\n case \"fragment\":\n return Fragment;\n case \"lazy\":\n // Materialize the hole's chunk now: abort and truncation reject\n // every unresolved chunk, which must include holes that decoded but\n // were never read.\n this.getChunk(model.id);\n return createElement(PayloadStreamHole, { decode: this, id: model.id });\n case \"promise\":\n // Promise props are handed straight to consumers, so the promise\n // (and its thenable-registry tracking) materializes here.\n return this.chunkPromise(this.getChunk(model.id));\n case \"suspense\":\n return Suspense;\n case \"view-transition\":\n return ViewTransition;\n default:\n // Every remaining tag is an ordinary value tag; the shared codec\n // decoder handles it against this decode's request-wide ref store.\n return decodePayloadValueTag(model, this.valueRefs, this.decodeChild);\n }\n }\n\n private decodeElementType(\n type: string | PayloadSpecialModel,\n ): ElementType<any> {\n if (typeof type === \"string\") return type;\n return this.decodeSpecialModel(type) as ElementType<any>;\n }\n\n // A client-referencing element inherits its decode's unsettled row gate;\n // the reference wrapper reads it per element instance at render.\n private attachElementDelivery(\n typeModel: string | PayloadSpecialModel,\n props: Props,\n ): void {\n if (typeof typeModel === \"string\" || typeModel.$fig !== \"client\") return;\n const gate = this.clientRowGates.get(typeModel.id);\n if (gate !== undefined) elementGates.set(props, gate);\n const assets = this.clientRowAssets.get(typeModel.id);\n if (assets !== undefined) elementAssets.set(props, assets);\n }\n\n private defineObjectRef<T>(\n id: number,\n create: () => T,\n fill: (value: T) => void,\n ): T {\n if (this.objectRefs.has(id)) return this.objectRefs.get(id) as T;\n\n const value = create();\n this.objectRefs.set(id, value);\n try {\n fill(value);\n return value;\n } catch (error) {\n this.objectRefs.delete(id);\n throw error;\n }\n }\n}\n\nfunction PayloadStreamHole(props: {\n decode: PayloadStreamDecode;\n id: number;\n}): FigNode {\n return props.decode.readChunkForRender(props.id) as FigNode;\n}\n\n// The one client-reference wrapper: attaches the per-element asset\n// declarations, reads the per-element reveal gate (elementGates), resolves\n// the component, renders it. Owned by a stateful resolver it is reused\n// across decodes, so island identity survives re-decodes whether a given\n// decode arrives gated or not; otherwise it lives for a single decode.\n// Asynchronous resolution starts at row arrival — overlapping the rest of\n// the stream instead of serializing behind it — and latches its type;\n// thenable tracking lets a resolution settled before its first render read\n// synchronously instead of suspending for a retry beat.\n//\n// Assets attach above the suspension points: the outer component always\n// completes, so a renderer delivers the declarations with the segment that\n// contains the reference even while the module load or reveal gate still\n// suspends the content below. The reveal gate and the reference's own\n// props ride explicit content props — createElement clones props objects,\n// so the per-element WeakMaps cannot be read through the inner element.\nfunction clientReferenceWrapper(\n resolved: ElementType<any> | PromiseLike<ElementType<any>>,\n referenceId: string,\n): ElementType<any> {\n let render: (props: Props) => FigNode;\n if (!isThenable(resolved)) {\n render = (props) => createElement(resolved, props);\n } else {\n const pending = Promise.resolve(resolved);\n trackThenable(pending);\n let type: ElementType<any> | null = null;\n render = (props) => {\n if (type === null) {\n type = clientReferenceType(readPromise(pending), referenceId);\n }\n return createElement(type, props);\n };\n }\n\n function PayloadClientContent(content: {\n gate?: Promise<void>;\n props: Props;\n }): FigNode {\n // Suspends while pending; gates never reject (prepareAssets results\n // settle through noop handlers).\n if (content.gate !== undefined) readPromise(content.gate);\n return render(content.props);\n }\n\n return function PayloadClientComponent(props: Props): FigNode {\n return attachElementAssets(\n props,\n createElement(PayloadClientContent, {\n gate: elementGates.get(props),\n props,\n }),\n );\n };\n}\n\nfunction attachElementAssets(props: Props, node: FigNode): FigNode {\n const resources = elementAssets.get(props);\n return resources === undefined ? node : attachAssets(resources, node);\n}\n\nfunction clientReferenceType(value: unknown, id: string): ElementType<any> {\n if (typeof value === \"function\") return value as ElementType<any>;\n throw new Error(`Client reference \"${id}\" did not resolve to a component.`);\n}\n"],"mappings":";;;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,oBAAoB,OAAgB,IAA8B;CACzE,IAAI,OAAO,UAAU,YAAY,OAAO;CACxC,MAAM,IAAI,MAAM,qBAAqB,GAAG,kCAAkC;AAC5E"}
|
|
1
|
+
{"version":3,"file":"payload.js","names":["attachAssets","attachAssets"],"sources":["../src/payload-client-reference.ts","../src/payload.ts"],"sourcesContent":["import {\n createElement,\n type ElementType,\n type FigNode,\n type Props,\n} from \"./element.ts\";\nimport { readPromise } from \"./hooks.ts\";\nimport { assets as attachAssets, type FigAssetResource } from \"./resource.ts\";\nimport { isThenable, trackThenable } from \"./thenables.ts\";\n\nexport interface PayloadClientReference {\n assets?: readonly FigAssetResource[];\n exportName?: string;\n id: string;\n ssr?: boolean;\n}\n\nexport type ResolveClientReference = (\n reference: PayloadClientReference,\n) => ElementType<any> | PromiseLike<ElementType<any>> | undefined;\n\n/**\n * A caller-owned stateful resolver: a `ResolveClientReference` that also\n * owns component identity. Decodes given one (as `resolveClientReference`)\n * resolve every client reference to a single resolver-owned wrapper per\n * reference id, so re-decoding a payload updates islands in place instead\n * of remounting them. The caller owns the lifetime: drop entries when their\n * modules change (HMR) or the manifest swaps.\n */\nexport interface PayloadClientReferenceResolver {\n (\n reference: PayloadClientReference,\n ): ElementType<any> | PromiseLike<ElementType<any>> | undefined;\n clear(): void;\n delete(id: string): boolean;\n}\n\nconst resolverEntries = new WeakMap<\n ResolveClientReference,\n Map<string, ElementType<any>>\n>();\n\nexport function createPayloadClientReferenceResolver(\n resolve: ResolveClientReference,\n): PayloadClientReferenceResolver {\n const entries = new Map<string, ElementType<any>>();\n const resolver = Object.assign(\n (reference: PayloadClientReference) => resolve(reference),\n {\n clear: (): void => entries.clear(),\n delete: (id: string): boolean => entries.delete(id),\n },\n );\n resolverEntries.set(resolver, entries);\n return resolver;\n}\n\ninterface ElementDelivery {\n assets?: readonly FigAssetResource[];\n gate?: Promise<void>;\n}\n\n// Delivery belongs to the decoded element, not its stable component type. A\n// newer decode therefore cannot re-suspend an already-mounted island.\nconst elementDelivery = new WeakMap<Props, ElementDelivery>();\n\n/** Owns client-reference identity and per-decode delivery state. */\nexport class PayloadClientReferences {\n private readonly rows = new Map<number, ElementDelivery>();\n\n constructor(private readonly resolve?: ResolveClientReference) {}\n\n register(\n rowId: number,\n reference: PayloadClientReference,\n gate: Promise<void> | null,\n assets: readonly FigAssetResource[] | null,\n ): ElementType<any> {\n const hasDelivery = gate !== null || assets !== null;\n if (hasDelivery) {\n const delivery: ElementDelivery = {};\n if (gate !== null) delivery.gate = gate;\n if (assets !== null) delivery.assets = assets;\n this.rows.set(rowId, delivery);\n\n if (gate !== null) {\n void gate.then(() => {\n delete delivery.gate;\n if (delivery.assets === undefined) this.rows.delete(rowId);\n });\n }\n }\n\n return this.resolveComponent(reference, hasDelivery);\n }\n\n attach(rowId: number, props: Props): void {\n const delivery = this.rows.get(rowId);\n if (delivery !== undefined) elementDelivery.set(props, delivery);\n }\n\n private resolveComponent(\n reference: PayloadClientReference,\n hasDelivery: boolean,\n ): ElementType<any> {\n const entries =\n this.resolve === undefined\n ? undefined\n : resolverEntries.get(this.resolve);\n const existing = entries?.get(reference.id);\n if (existing !== undefined) return existing;\n\n let resolved: ReturnType<ResolveClientReference>;\n try {\n resolved = this.resolve?.(reference);\n } catch (error) {\n resolved = Promise.reject(error);\n }\n\n if (resolved === undefined) {\n // Never cache a missing manifest entry: a later decode may resolve it.\n return function PayloadUnresolvedClientComponent(): never {\n throw new Error(\n `Cannot render client reference \"${reference.id}\" because decodePayloadStream was not configured with a matching resolveClientReference.`,\n );\n };\n }\n\n // A direct synchronous component needs no wrapper when identity does not\n // need resolver ownership and this decode has no delivery state.\n if (entries === undefined && !isThenable(resolved) && !hasDelivery) {\n return resolved;\n }\n\n const component = clientReferenceWrapper(resolved, reference.id);\n entries?.set(reference.id, component);\n return component;\n }\n}\n\nfunction clientReferenceWrapper(\n resolved: ElementType<any> | PromiseLike<ElementType<any>>,\n referenceId: string,\n): ElementType<any> {\n let render: (props: Props) => FigNode;\n if (isThenable(resolved)) {\n const pending = Promise.resolve(resolved);\n trackThenable(pending);\n let type: ElementType<any> | null = null;\n render = (props) => {\n type ??= clientReferenceType(readPromise(pending), referenceId);\n return createElement(type, props);\n };\n } else {\n render = (props) => createElement(resolved, props);\n }\n\n function PayloadClientContent(content: {\n gate?: Promise<void>;\n props: Props;\n }): FigNode {\n if (content.gate !== undefined) readPromise(content.gate);\n return render(content.props);\n }\n\n return function PayloadClientComponent(props: Props): FigNode {\n const delivery = elementDelivery.get(props);\n const content = createElement(PayloadClientContent, {\n gate: delivery?.gate,\n props,\n });\n return delivery?.assets === undefined\n ? content\n : attachAssets(delivery.assets, content);\n };\n}\n\nfunction clientReferenceType(value: unknown, id: string): ElementType<any> {\n if (typeof value === \"function\") return value as ElementType<any>;\n throw new Error(`Client reference \"${id}\" did not resolve to a component.`);\n}\n","import type { FigDataHydrationEntry } from \"./data.ts\";\nimport {\n type AwaitedFigNode,\n createElement,\n type ElementType,\n FigElementSymbol,\n type FigNode,\n Fragment,\n type Key,\n type Props,\n Suspense,\n ViewTransition,\n} from \"./element.ts\";\nimport { readPromise } from \"./hooks.ts\";\nimport {\n createPayloadClientReferenceResolver,\n type PayloadClientReference,\n type PayloadClientReferenceResolver,\n PayloadClientReferences,\n type ResolveClientReference,\n} from \"./payload-client-reference.ts\";\nimport {\n decodePayloadDataEntries,\n decodePayloadRecord,\n decodePayloadValueTag,\n errorFromPayloadValue,\n isPayloadSpecialModel,\n jsonPayloadCodec,\n type PayloadDecodeRefs,\n type PayloadElementModel,\n type PayloadModel,\n type PayloadRow,\n type PayloadRowDecoder,\n type PayloadSpecialModel,\n type SerializedAssetResource,\n} from \"./payload-format.ts\";\nimport {\n assetResourceDestination,\n assets as attachAssets,\n type FigAssetResource,\n isFigAssetResource,\n} from \"./resource.ts\";\nimport { isThenable, trackThenable } from \"./thenables.ts\";\n\nexport {\n createPayloadClientReferenceResolver,\n type PayloadClientReference,\n type PayloadClientReferenceResolver,\n type ResolveClientReference,\n};\n\nexport type PayloadDecodeCompletion =\n | { status: \"aborted\" }\n | { status: \"complete\" }\n | { status: \"failed\"; error: unknown };\n\nexport interface PayloadDecodeOptions {\n /**\n * Receives decoded `data` rows for hydration into a data store. The\n * capability itself is expected to be generation-guarded and to ignore\n * entries after its caller loses authority.\n */\n hydrate?: (entries: readonly FigDataHydrationEntry[]) => void;\n /**\n * Observes every outlined hole rejection — an `error` row or a stream\n * failure stranding referenced rows, before or after the root fulfills.\n * Abort cancellation is excluded. Called once per rejected hole; the\n * observer is never awaited and cannot break decoding.\n */\n onHoleError?: (error: unknown) => unknown;\n /**\n * Observes the end of ingestion: called exactly once when the stream\n * settles as complete, failed, or aborted. Post-root failures reject the\n * holes they strand, but a failure with no pending slot is otherwise\n * invisible — this is the hook for reporting it. The callback is never\n * awaited, and its exceptions and rejections are swallowed, so an\n * observer cannot block or break decode teardown.\n */\n onStreamDone?: (result: PayloadDecodeCompletion) => unknown;\n /**\n * Called with delivery asset resources as soon as their rows arrive\n * (e.g. fig-dom's insertAssetResources). A returned promise gates the\n * reveal of only the content that declared a dependency on those assets;\n * gate settlement — fulfilled or rejected — releases the reveal, so a\n * failed asset never blocks content.\n */\n prepareAssets?: (\n assets: readonly FigAssetResource[],\n ) => void | PromiseLike<void>;\n /**\n * Retains delivery asset dependencies as `assets(...)` declarations on\n * their decoded owners. Document metadata is always retained because its\n * owner may mutate the document only when it commits. Server document\n * renderers enable this option to retain delivery assets too.\n */\n retainAssets?: boolean;\n /**\n * Resolves client-reference rows to components. A plain function keeps\n * identity per decode: gated and asynchronously resolved references decode\n * to per-decode wrappers and remount on re-decode. A stateful resolver\n * (created by `createPayloadClientReferenceResolver`) keeps every\n * resolvable reference's identity stable across the decodes sharing it.\n */\n resolveClientReference?: ResolveClientReference;\n signal?: AbortSignal;\n}\n\nclass PayloadDecodeAbortedError extends Error {\n constructor(reason?: unknown) {\n super(\n \"Payload decode aborted.\",\n reason === undefined ? undefined : { cause: reason },\n );\n this.name = \"PayloadDecodeAbortedError\";\n }\n}\n\n/**\n * Decode a payload row stream. The returned promise resolves with the\n * decoded root FigNode as soon as the root row decodes (and rejects only\n * when the stream fails before producing a root value, or with the root\n * row's own error); decoding continues in the background, filling outlined\n * holes as their rows arrive. Post-root failures reject the holes they\n * strand and report through `onStreamDone`. Aborting `options.signal`\n * ignores remaining rows and rejects unresolved holes with an internal\n * cancellation reason.\n */\nexport function decodePayloadStream(\n stream: ReadableStream<Uint8Array>,\n options: PayloadDecodeOptions = {},\n): Promise<AwaitedFigNode> {\n return new PayloadDecoder(stream, options).value;\n}\n\ntype DecodeChunk = {\n // The chunk's row has been ingested (decoded or rejected); reveal may still\n // be waiting on an asset gate. Truncation and abort reject only chunks\n // whose rows never arrived.\n arrived: boolean;\n // Materialized lazily: most rows settle synchronously at arrival and are\n // only ever read through result, so eagerly allocating a promise and\n // its controls per row would be waste.\n promise: Promise<unknown> | null;\n reject: ((reason: unknown) => void) | null;\n resolve: ((value: unknown) => void) | null;\n result:\n | { status: \"pending\" }\n | { status: \"fulfilled\"; value: unknown }\n | { status: \"rejected\"; error: unknown };\n};\n\ntype DecodingElement = {\n $$typeof: symbol;\n key: Key | null;\n props: Props;\n type: ElementType<any>;\n};\n\nconst noop = (): void => undefined;\n\nfunction notifyObserver<T>(\n observer: ((value: T) => unknown) | undefined,\n value: T,\n): void {\n try {\n const result = observer?.(value);\n if (isThenable(result)) void Promise.resolve(result).then(noop, noop);\n } catch {\n // Observers cannot break decode teardown or create unhandled rejections.\n }\n}\n\ninterface Deferred<T> {\n promise: Promise<T>;\n reject: (reason: unknown) => void;\n resolve: (value: T) => void;\n}\n\nfunction deferred<T>(): Deferred<T> {\n let resolve: Deferred<T>[\"resolve\"] = noop;\n let reject: Deferred<T>[\"reject\"] = noop;\n const promise = new Promise<T>((innerResolve, innerReject) => {\n resolve = innerResolve;\n reject = innerReject;\n });\n return { promise, reject, resolve };\n}\n\nfunction decodeAssetResources(\n serialized: readonly SerializedAssetResource[] | undefined,\n): FigAssetResource[] | null {\n if (serialized === undefined) return null;\n const assets = serialized.filter(isFigAssetResource);\n return assets.length === 0 ? null : assets;\n}\n\nclass PayloadDecoder {\n readonly value: Promise<AwaitedFigNode>;\n\n private readonly chunks = new Map<number, DecodeChunk>();\n private readonly objectRefs = new Map<number, unknown>();\n // Asset gates registered for a row id (assets rows carry `for`); consumed\n // when that row arrives.\n private readonly rowGates = new Map<number, Array<PromiseLike<void>>>();\n private readonly rowAssets = new Map<number, FigAssetResource[]>();\n private readonly clientReferences: PayloadClientReferences;\n private reader: ReadableStreamDefaultReader<Uint8Array> | null = null;\n private done = false;\n // Resolved on abort so arrived-but-gated chunks reveal instead of waiting\n // for asset gates that may never settle.\n private readonly gateRelease = deferred<void>();\n private removeAbortListener: () => void = noop;\n // One closure and one refs adapter reused across every decoded model, so\n // the per-node decode loop allocates only the decoded values themselves.\n private readonly decodeChild = (model: PayloadModel): unknown =>\n this.decodeModel(model);\n private readonly valueRefs: PayloadDecodeRefs = {\n define: (id, create, fill) => this.defineObjectRef(id, create, fill),\n read: (id) => {\n if (!this.objectRefs.has(id)) {\n throw new Error(`Payload referenced unknown object id ${id}.`);\n }\n return this.objectRefs.get(id);\n },\n };\n\n constructor(\n stream: ReadableStream<Uint8Array>,\n private readonly options: PayloadDecodeOptions,\n ) {\n this.clientReferences = new PayloadClientReferences(\n options.resolveClientReference,\n );\n const rowDecoder = jsonPayloadCodec.createDecoder((row) =>\n this.handleRow(row),\n );\n this.value = this.chunkPromise(this.getChunk(0)) as Promise<AwaitedFigNode>;\n\n void this.ingest(stream, rowDecoder);\n\n const signal = options.signal;\n if (signal !== undefined) {\n if (signal.aborted) {\n this.abort(signal.reason);\n } else {\n const onAbort = () => this.abort(signal.reason);\n signal.addEventListener(\"abort\", onAbort, { once: true });\n this.removeAbortListener = () =>\n signal.removeEventListener(\"abort\", onAbort);\n }\n }\n }\n\n abort(reason?: unknown): void {\n if (this.done) return;\n const error = new PayloadDecodeAbortedError(reason);\n this.gateRelease.resolve(undefined);\n void this.reader?.cancel(error).catch(noop);\n this.rejectUnresolved(error);\n this.settle({ status: \"aborted\" });\n }\n\n private async ingest(\n stream: ReadableStream<Uint8Array>,\n rowDecoder: PayloadRowDecoder,\n ): Promise<void> {\n const reader = stream.getReader();\n this.reader = reader;\n\n try {\n for (;;) {\n const { done, value } = await reader.read();\n if (this.done) return;\n if (done) break;\n rowDecoder.decode(value);\n }\n rowDecoder.flush();\n this.finishIngestion();\n } catch (error) {\n if (this.done) return;\n void reader.cancel(error).catch(noop);\n this.failIngestion(error);\n }\n }\n\n private finishIngestion(): void {\n for (const chunk of this.chunks.values()) {\n if (!chunk.arrived) {\n // The server closes the stream only after every outlined row has\n // been written, so any unresolved reference is truncation.\n this.failIngestion(\n new Error(\"Payload stream ended before all referenced rows arrived.\"),\n );\n return;\n }\n }\n this.settle({ status: \"complete\" });\n }\n\n private failIngestion(error: unknown): void {\n if (this.done) return;\n this.rejectUnresolved(error);\n this.settle({ status: \"failed\", error });\n }\n\n private rejectUnresolved(error: unknown): void {\n for (const [id, chunk] of this.chunks) {\n if (chunk.arrived) continue;\n chunk.arrived = true;\n this.rejectChunk(id, chunk, error);\n }\n }\n\n private settle(result: PayloadDecodeCompletion): void {\n if (this.done) return;\n this.done = true;\n this.removeAbortListener();\n notifyObserver(this.options.onStreamDone, result);\n }\n\n private handleRow(row: PayloadRow): void {\n if (this.done) return;\n\n switch (row.tag) {\n case \"model\": {\n const chunk = this.getChunk(row.id);\n let decoded = this.decodeModel(row.value);\n const retainedAssets = this.rowAssets.get(row.id);\n if (retainedAssets !== undefined) {\n this.rowAssets.delete(row.id);\n decoded = attachAssets(retainedAssets, decoded as FigNode);\n }\n chunk.arrived = true;\n const gates = this.rowGates.get(row.id);\n if (gates === undefined) {\n this.fulfillChunk(chunk, decoded);\n return;\n }\n this.rowGates.delete(row.id);\n void Promise.race([Promise.all(gates), this.gateRelease.promise]).then(\n () => this.fulfillChunk(chunk, decoded),\n );\n return;\n }\n case \"client\": {\n const chunk = this.getChunk(row.id);\n const reference: PayloadClientReference = { id: row.value.id };\n if (row.value.exportName !== undefined) {\n reference.exportName = row.value.exportName;\n }\n if (row.value.ssr === true) reference.ssr = true;\n const assets = decodeAssetResources(row.value.assets);\n if (assets !== null) {\n reference.assets = assets;\n }\n const retainedAssets = this.retainedAssets(assets);\n const gate = this.prepareAssets(assets);\n const component = this.clientReferences.register(\n row.id,\n reference,\n gate,\n retainedAssets,\n );\n chunk.arrived = true;\n this.fulfillChunk(chunk, component);\n return;\n }\n case \"error\": {\n const chunk = this.getChunk(row.id);\n chunk.arrived = true;\n // Reveal-gating a failure is pointless; drop any gates aimed here.\n this.rowGates.delete(row.id);\n this.rowAssets.delete(row.id);\n this.rejectChunk(row.id, chunk, errorFromPayloadValue(row.value));\n return;\n }\n case \"data\": {\n const hydrate = this.options.hydrate;\n if (hydrate === undefined) return;\n hydrate(decodePayloadDataEntries(row.value));\n return;\n }\n case \"assets\": {\n const decodedAssets = decodeAssetResources(row.value);\n const retainedAssets = this.retainedAssets(decodedAssets);\n if (retainedAssets !== null && row.for !== undefined) {\n const retained = this.rowAssets.get(row.for);\n if (retained === undefined)\n this.rowAssets.set(row.for, retainedAssets);\n else retained.push(...retainedAssets);\n }\n const gate = this.prepareAssets(decodedAssets);\n if (gate === null || row.for === undefined) return;\n const gates = this.rowGates.get(row.for);\n if (gates === undefined) this.rowGates.set(row.for, [gate]);\n else gates.push(gate);\n return;\n }\n }\n }\n\n // Never rejects and never blocks content on a failed asset: a rejected\n // prepareAssets result (or synchronous throw) settles the gate.\n private prepareAssets(\n assets: readonly FigAssetResource[] | null,\n ): Promise<void> | null {\n const prepare = this.options.prepareAssets;\n if (prepare === undefined || assets === null) return null;\n const delivery = assets.filter(\n (resource) => assetResourceDestination(resource) === \"stream\",\n );\n if (delivery.length === 0) return null;\n\n let result: void | PromiseLike<void>;\n try {\n result = prepare(delivery);\n } catch {\n return null;\n }\n if (!isThenable(result)) return null;\n const gate = Promise.resolve(result).then(noop, noop);\n trackThenable(gate);\n return gate;\n }\n\n private retainedAssets(\n assets: FigAssetResource[] | null,\n ): FigAssetResource[] | null {\n if (assets === null) return null;\n const retained =\n this.options.retainAssets === true\n ? assets\n : assets.filter(\n (resource) => assetResourceDestination(resource) === \"head\",\n );\n return retained.length === 0 ? null : retained;\n }\n\n private getChunk(id: number): DecodeChunk {\n const existing = this.chunks.get(id);\n if (existing !== undefined) return existing;\n\n const chunk: DecodeChunk = {\n arrived: false,\n promise: null,\n reject: null,\n resolve: null,\n result: { status: \"pending\" },\n };\n this.chunks.set(id, chunk);\n return chunk;\n }\n\n // Materializes (and registers with the thenable registry) on first access:\n // a settled chunk becomes an already-settled promise, a pending one gets\n // live resolvers that fulfillChunk/rejectChunk drive.\n private chunkPromise(chunk: DecodeChunk): Promise<unknown> {\n if (chunk.promise !== null) return chunk.promise;\n\n if (chunk.result.status === \"fulfilled\") {\n chunk.promise = Promise.resolve(chunk.result.value);\n } else if (chunk.result.status === \"rejected\") {\n chunk.promise = Promise.reject(chunk.result.error);\n // Holes nobody awaits must not become unhandled rejections; readers\n // still observe the stored error.\n void chunk.promise.catch(noop);\n } else {\n chunk.promise = new Promise((resolve, reject) => {\n chunk.resolve = resolve;\n chunk.reject = reject;\n });\n }\n trackThenable(chunk.promise);\n return chunk.promise;\n }\n\n private fulfillChunk(chunk: DecodeChunk, value: unknown): void {\n if (chunk.result.status !== \"pending\") return;\n chunk.result = { status: \"fulfilled\", value };\n chunk.resolve?.(value);\n chunk.resolve = null;\n chunk.reject = null;\n }\n\n private rejectChunk(id: number, chunk: DecodeChunk, error: unknown): void {\n if (chunk.result.status !== \"pending\") return;\n chunk.result = { error, status: \"rejected\" };\n if (chunk.reject !== null) {\n chunk.reject(error);\n void chunk.promise?.catch(noop);\n }\n chunk.resolve = null;\n chunk.reject = null;\n if (id !== 0 && !(error instanceof PayloadDecodeAbortedError)) {\n notifyObserver(this.options.onHoleError, error);\n }\n }\n\n readChunkForRender(id: number): unknown {\n const chunk = this.getChunk(id);\n if (chunk.result.status === \"rejected\") throw chunk.result.error;\n if (chunk.result.status === \"pending\")\n return readPromise(this.chunkPromise(chunk));\n return chunk.result.value;\n }\n\n private decodeModel(model: PayloadModel): unknown {\n if (model === null) return null;\n if (Array.isArray(model)) return model.map(this.decodeChild);\n if (typeof model !== \"object\") return model;\n\n if (isPayloadSpecialModel(model)) return this.decodeSpecialModel(model);\n\n return decodePayloadRecord(model, this.decodeChild);\n }\n\n private decodeSpecialModel(\n model: PayloadElementModel | PayloadSpecialModel,\n ): unknown {\n switch (model.$fig) {\n case \"element\": {\n if (model.id !== undefined) {\n return this.defineObjectRef(\n model.id,\n (): DecodingElement => ({\n $$typeof: FigElementSymbol,\n key: model.key,\n props: {},\n type: Fragment,\n }),\n (element) => {\n element.type = this.decodeElementType(model.type);\n const props = this.decodeModel(model.props) as Props;\n element.props = props;\n this.attachElementDelivery(model.type, props);\n },\n );\n }\n const type = this.decodeElementType(model.type);\n const props = this.decodeModel(model.props) as Props & {\n key?: Key | null;\n };\n if (model.key !== null) props.key = model.key;\n const element = createElement(type, props);\n // createElement copies props, so the gate keys the element's own\n // props object — the one the component will receive.\n this.attachElementDelivery(model.type, element.props);\n return element;\n }\n case \"client\": {\n const chunk = this.chunks.get(model.id);\n if (chunk === undefined || chunk.result.status !== \"fulfilled\") {\n throw new Error(\n `Payload model referenced client row ${model.id} before it arrived.`,\n );\n }\n return chunk.result.value;\n }\n case \"fragment\":\n return Fragment;\n case \"lazy\":\n // Materialize the hole's chunk now: abort and truncation reject\n // every unresolved chunk, which must include holes that decoded but\n // were never read.\n this.getChunk(model.id);\n return createElement(PayloadHole, { decode: this, id: model.id });\n case \"promise\":\n // Promise props are handed straight to consumers, so the promise\n // (and its thenable-registry tracking) materializes here.\n return this.chunkPromise(this.getChunk(model.id));\n case \"suspense\":\n return Suspense;\n case \"view-transition\":\n return ViewTransition;\n default:\n // Every remaining tag is an ordinary value tag; the shared codec\n // decoder handles it against this decode's request-wide ref store.\n return decodePayloadValueTag(model, this.valueRefs, this.decodeChild);\n }\n }\n\n private decodeElementType(\n type: string | PayloadSpecialModel,\n ): ElementType<any> {\n if (typeof type === \"string\") return type;\n return this.decodeSpecialModel(type) as ElementType<any>;\n }\n\n // A client-referencing element inherits its decode's unsettled row gate;\n // the reference wrapper reads it per element instance at render.\n private attachElementDelivery(\n typeModel: string | PayloadSpecialModel,\n props: Props,\n ): void {\n if (typeof typeModel === \"string\" || typeModel.$fig !== \"client\") return;\n this.clientReferences.attach(typeModel.id, props);\n }\n\n private defineObjectRef<T>(\n id: number,\n create: () => T,\n fill: (value: T) => void,\n ): T {\n if (this.objectRefs.has(id)) return this.objectRefs.get(id) as T;\n\n const value = create();\n this.objectRefs.set(id, value);\n try {\n fill(value);\n return value;\n } catch (error) {\n this.objectRefs.delete(id);\n throw error;\n }\n }\n}\n\nfunction PayloadHole(props: { decode: PayloadDecoder; id: number }): FigNode {\n return props.decode.readChunkForRender(props.id) as FigNode;\n}\n"],"mappings":";;;;AAqCA,MAAM,kCAAkB,IAAI,QAG1B;AAEF,SAAgB,qCACd,SACgC;CAChC,MAAM,0BAAU,IAAI,IAA8B;CAClD,MAAM,WAAW,OAAO,QACrB,cAAsC,QAAQ,SAAS,GACxD;EACE,aAAmB,QAAQ,MAAM;EACjC,SAAS,OAAwB,QAAQ,OAAO,EAAE;CACpD,CACF;CACA,gBAAgB,IAAI,UAAU,OAAO;CACrC,OAAO;AACT;AASA,MAAM,kCAAkB,IAAI,QAAgC;;AAG5D,IAAa,0BAAb,MAAqC;CAGN;CAF7B,uBAAwB,IAAI,IAA6B;CAEzD,YAAY,SAAmD;EAAlC,KAAA,UAAA;CAAmC;CAEhE,SACE,OACA,WACA,MACA,QACkB;EAClB,MAAM,cAAc,SAAS,QAAQ,WAAW;EAChD,IAAI,aAAa;GACf,MAAM,WAA4B,CAAC;GACnC,IAAI,SAAS,MAAM,SAAS,OAAO;GACnC,IAAI,WAAW,MAAM,SAAS,SAAS;GACvC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