@ -1,3 +1,6 @@
import { pop , push , set _ssr _context , ssr _context } from './context.js' ;
import * as e from './errors.js' ;
/** @typedef {'head' | 'body'} PayloadType */
/** @typedef {'head' | 'body'} PayloadType */
/** @typedef {{ [key in PayloadType]: string }} AccumulatedContent */
/** @typedef {{ [key in PayloadType]: string }} AccumulatedContent */
/** @typedef {{ start: number, end: number, fn: (content: AccumulatedContent) => AccumulatedContent | Promise<AccumulatedContent> }} Compaction */
/** @typedef {{ start: number, end: number, fn: (content: AccumulatedContent) => AccumulatedContent | Promise<AccumulatedContent> }} Compaction */
@ -9,8 +12,6 @@
* @ typedef { string | Payload } PayloadItem
* @ typedef { string | Payload } PayloadItem
* /
* /
import { pop , push , set _ssr _context , ssr _context } from './context.js' ;
/ * *
/ * *
* Payloads are basically a tree of ` string | Payload ` s , where each ` Payload ` in the tree represents
* Payloads are basically a tree of ` string | Payload ` s , where each ` Payload ` in the tree represents
* work that may or may not have completed . A payload can be { @ link collect } ed to aggregate the
* work that may or may not have completed . A payload can be { @ link collect } ed to aggregate the
@ -198,8 +199,9 @@ export class Payload {
* /
* /
subsume ( other ) {
subsume ( other ) {
if ( this . global . mode !== other . global . mode ) {
if ( this . global . mode !== other . global . mode ) {
// TODO message - this should be impossible though
throw new Error (
throw new Error ( 'invariant: a payload cannot switch modes' ) ;
"invariant: A payload cannot switch modes. If you're seeing this, there's a compiler bug. File an issue!"
) ;
}
}
this . global . subsume ( other . global ) ;
this . global . subsume ( other . global ) ;
@ -228,7 +230,9 @@ export class Payload {
const content = Payload . # collect _content ( to _compact , type ) ;
const content = Payload . # collect _content ( to _compact , type ) ;
const transformed _content = fn ( content ) ;
const transformed _content = fn ( content ) ;
if ( transformed _content instanceof Promise ) {
if ( transformed _content instanceof Promise ) {
throw new Error ( 'invariant: should never reach this' ) ;
throw new Error (
"invariant: Somehow you've encountered asynchronous work while rendering synchronously. If you're seeing this, there's a compiler bug. File an issue!"
) ;
} else {
} else {
Payload . # push _accumulated _content ( child , transformed _content ) ;
Payload . # push _accumulated _content ( child , transformed _content ) ;
}
}
@ -281,8 +285,7 @@ export class Payload {
const result = render ( child _payload ) ;
const result = render ( child _payload ) ;
if ( result instanceof Promise ) {
if ( result instanceof Promise ) {
if ( this . global . mode === 'sync' ) {
if ( this . global . mode === 'sync' ) {
// TODO more-proper error
e . async _in _sync ( ) ;
throw new Error ( 'Encountered an asynchronous component while rendering synchronously' ) ;
}
}
// just to avoid unhandled promise rejections -- we'll end up throwing in `collect_async` if something fails
// just to avoid unhandled promise rejections -- we'll end up throwing in `collect_async` if something fails
result . catch ( ( ) => { } ) ;
result . catch ( ( ) => { } ) ;