delete unused compact method

pull/16797/head
Rich Harris 6 days ago
parent b0900ee435
commit 27f6aa401c

@ -57,11 +57,10 @@ export class Renderer {
/**
* Asynchronous work associated with this renderer. `initial` is the promise from the function
* this renderer was passed to (if that function was async), and `followup` is any any additional
* work from `compact` calls that needs to complete prior to collecting this renderer's content.
* @type {{ initial: Promise<void> | undefined, followup: Promise<void>[] }}
* this renderer was passed to (if that function was async)
* @type {{ initial: Promise<void> | undefined }}
*/
promises = { initial: undefined, followup: [] };
promises = { initial: undefined };
/**
* State which is associated with the content tree as a whole.
@ -261,22 +260,6 @@ export class Renderer {
}
}
/**
* Compact everything between `start` and `end` into a single renderer, then call `fn` with the result of that renderer.
* The compacted renderer will be sync if all of the children are sync and {@link fn} is sync, otherwise it will be async.
* @param {{ start: number, end?: number, fn: (content: AccumulatedContent) => AccumulatedContent | Promise<AccumulatedContent> }} args
*/
compact({ start, end = this.#out.length, fn }) {
const child = new Renderer(this.global, this);
const to_compact = this.#out.splice(start, end - start, child);
if (this.global.mode === 'sync') {
Renderer.#compact(fn, child, to_compact, this.type);
} else {
this.promises.followup.push(Renderer.#compact_async(fn, child, to_compact, this.type));
}
}
/**
* @param {() => void} fn
*/
@ -485,36 +468,6 @@ export class Renderer {
}
}
/**
* @param {(content: AccumulatedContent) => AccumulatedContent | Promise<AccumulatedContent>} fn
* @param {Renderer} child
* @param {RendererItem[]} to_compact
* @param {RendererType} type
*/
static #compact(fn, child, to_compact, type) {
const content = Renderer.#collect_content(to_compact, type);
const transformed_content = fn(content);
if (transformed_content instanceof Promise) {
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 {
Renderer.#push_accumulated_content(child, transformed_content);
}
}
/**
* @param {(content: AccumulatedContent) => AccumulatedContent | Promise<AccumulatedContent>} fn
* @param {Renderer} child
* @param {RendererItem[]} to_compact
* @param {RendererType} type
*/
static async #compact_async(fn, child, to_compact, type) {
const content = await Renderer.#collect_content_async(to_compact, type);
const transformed_content = await fn(content);
Renderer.#push_accumulated_content(child, transformed_content);
}
/**
* Collect all of the code from the `out` array and return it as a string, or a promise resolving to a string.
* @param {RendererItem[]} items
@ -551,30 +504,13 @@ export class Renderer {
// we can't do anything until it's done and we know our `out` array is complete.
await item.promises.initial;
}
for (const followup of item.promises.followup) {
// this is sequential because `compact` could synchronously queue up additional followup work
await followup;
}
await Renderer.#collect_content_async(item.#out, item.type, content);
}
}
return content;
}
/**
* @param {Renderer} tree
* @param {AccumulatedContent} accumulated_content
*/
static #push_accumulated_content(tree, accumulated_content) {
for (const [type, content] of Object.entries(accumulated_content)) {
if (!content) continue;
const child = new Renderer(tree.global, tree);
child.type = /** @type {RendererType} */ (type);
child.push(content);
tree.#out.push(child);
}
}
/**
* @template {Record<string, any>} Props
* @param {'sync' | 'async'} mode

@ -84,26 +84,6 @@ test('creating an async child in a sync context throws', () => {
expect(() => Renderer.render(component as unknown as Component).body).toThrow('await_invalid');
});
test('compact synchronously aggregates a range and can transform into head/body', () => {
const component = (renderer: Renderer) => {
const start = renderer.length;
renderer.push('a');
renderer.push('b');
renderer.push('c');
renderer.compact({
start,
end: start + 2,
fn: (content) => {
return { head: '<h>H</h>', body: content.body + 'd' };
}
});
};
const { head, body } = Renderer.render(component as unknown as Component);
expect(head).toBe('<h>H</h>');
expect(body).toBe('<!--[-->abdc<!--]-->');
});
test('local state is shallow-copied to children', () => {
const root = new Renderer(new SSRState('sync'));
root.local.select_value = 'A';
@ -220,28 +200,6 @@ describe('async', () => {
expect(() => result.html).toThrow('html_deprecated');
});
test('compact schedules followup when compaction input is async', async () => {
const component = (renderer: Renderer) => {
renderer.push('a');
renderer.child(async ($$renderer) => {
await Promise.resolve();
$$renderer.push('X');
});
renderer.push('b');
renderer.compact({
start: 0,
fn: async (content) => ({
body: content.body.toLowerCase(),
head: await Promise.resolve('')
})
});
};
const { body, head } = await Renderer.render(component as unknown as Component);
expect(head).toBe('');
expect(body).toBe('<!--[-->axb<!--]-->');
});
test('push accepts async functions in async context', async () => {
const component = (renderer: Renderer) => {
renderer.push('a');
@ -308,25 +266,6 @@ describe('async', () => {
expect(body).toBe('<!--[-->start-async-child--end<!--]-->');
});
test('push async functions work with compact operations', async () => {
const component = (renderer: Renderer) => {
renderer.push('a');
renderer.push(async () => {
await Promise.resolve();
return 'b';
});
renderer.push('c');
renderer.compact({
start: 0,
fn: (content) => ({ head: '', body: content.body.toUpperCase() })
});
};
const { head, body } = await Renderer.render(component as unknown as Component);
expect(head).toBe('');
expect(body).toBe('<!--[-->ABC<!--]-->');
});
test('push async functions are not supported in sync context', () => {
const component = (renderer: Renderer) => {
renderer.push('a');

Loading…
Cancel
Save