grab this.local from parent directly

pull/16748/head
Rich Harris 3 days ago
parent 8329315c6b
commit 1f5f385fe5

@ -74,13 +74,12 @@ export class Payload {
/** /**
* @param {SSRState} global * @param {SSRState} global
* @param {{ select_value: string | undefined }} [local]
* @param {Payload | undefined} [parent] * @param {Payload | undefined} [parent]
* @param {PayloadType} [type] * @param {PayloadType} [type]
*/ */
constructor(global, local = { select_value: undefined }, parent, type) { constructor(global, parent, type) {
this.global = global; this.global = global;
this.local = { ...local }; this.local = parent ? { ...parent.local } : { select_value: undefined };
this.#parent = parent; this.#parent = parent;
this.type = type ?? parent?.type ?? 'body'; this.type = type ?? parent?.type ?? 'body';
} }
@ -93,7 +92,7 @@ export class Payload {
* @returns {void} * @returns {void}
*/ */
child(render, type) { child(render, type) {
const child = new Payload(this.global, this.local, this, type); const child = new Payload(this.global, this, type);
this.#run_child(child, render); this.#run_child(child, render);
} }
@ -106,7 +105,7 @@ export class Payload {
*/ */
component(render, fn) { component(render, fn) {
push(fn); push(fn);
const child = new Payload(this.global, this.local, this); const child = new Payload(this.global, this);
child.#is_component_body = true; child.#is_component_body = true;
this.#run_child(child, render); this.#run_child(child, render);
pop(); pop();
@ -129,7 +128,7 @@ export class Payload {
* @param {{ start: number, end?: number, fn: (content: AccumulatedContent) => AccumulatedContent | Promise<AccumulatedContent> }} args * @param {{ start: number, end?: number, fn: (content: AccumulatedContent) => AccumulatedContent | Promise<AccumulatedContent> }} args
*/ */
compact({ start, end = this.#out.length, fn }) { compact({ start, end = this.#out.length, fn }) {
const child = new Payload(this.global, this.local, this); const child = new Payload(this.global, this);
const to_compact = this.#out.splice(start, end - start, child); const to_compact = this.#out.splice(start, end - start, child);
if (this.global.mode === 'sync') { if (this.global.mode === 'sync') {
@ -188,7 +187,7 @@ export class Payload {
* @deprecated this is needed for legacy component bindings * @deprecated this is needed for legacy component bindings
*/ */
copy() { copy() {
const copy = new Payload(this.global, this.local, this.#parent, this.type); const copy = new Payload(this.global, this.#parent, this.type);
copy.#out = this.#out.map((item) => (item instanceof Payload ? item.copy() : item)); copy.#out = this.#out.map((item) => (item instanceof Payload ? item.copy() : item));
copy.promises = this.promises; copy.promises = this.promises;
return copy; return copy;
@ -358,7 +357,7 @@ export class Payload {
static #push_accumulated_content(tree, accumulated_content) { static #push_accumulated_content(tree, accumulated_content) {
for (const [type, content] of Object.entries(accumulated_content)) { for (const [type, content] of Object.entries(accumulated_content)) {
if (!content) continue; if (!content) continue;
const child = new Payload(tree.global, tree.local, tree, /** @type {PayloadType} */ (type)); const child = new Payload(tree.global, tree, /** @type {PayloadType} */ (type));
child.push(content); child.push(content);
tree.#out.push(child); tree.#out.push(child);
} }

@ -28,7 +28,7 @@ test('child type switches content area (head vs body)', () => {
}); });
test('child inherits parent type when not specified', () => { test('child inherits parent type when not specified', () => {
const parent = new Payload(new SSRState('sync'), undefined, undefined, 'head'); const parent = new Payload(new SSRState('sync'), undefined, 'head');
parent.push('<meta name="x"/>'); parent.push('<meta name="x"/>');
parent.child(($$payload) => { parent.child(($$payload) => {
$$payload.push('<style>/* css */</style>'); $$payload.push('<style>/* css */</style>');
@ -160,7 +160,7 @@ test('local state is shallow-copied to children', () => {
}); });
test('subsume replaces tree content and state from other', () => { test('subsume replaces tree content and state from other', () => {
const a = new Payload(new SSRState('async'), undefined, undefined, 'head'); const a = new Payload(new SSRState('async'), undefined, 'head');
a.push('<meta />'); a.push('<meta />');
a.local.select_value = 'A'; a.local.select_value = 'A';
@ -182,7 +182,7 @@ test('subsume replaces tree content and state from other', () => {
}); });
test('subsume refuses to switch modes', () => { test('subsume refuses to switch modes', () => {
const a = new Payload(new SSRState('sync'), undefined, undefined, 'head'); const a = new Payload(new SSRState('sync'), undefined, 'head');
a.push('<meta />'); a.push('<meta />');
a.local.select_value = 'A'; a.local.select_value = 'A';
@ -271,7 +271,7 @@ test('push handles async functions with different timing', async () => {
}); });
test('push async functions work with head content type', async () => { test('push async functions work with head content type', async () => {
const payload = new Payload(new SSRState('async'), undefined, undefined, 'head'); const payload = new Payload(new SSRState('async'), undefined, 'head');
payload.push(async () => { payload.push(async () => {
await Promise.resolve(); await Promise.resolve();
return '<title>Async Title</title>'; return '<title>Async Title</title>';

Loading…
Cancel
Save