make it work in dev

parallelize-async-work
ComputerGuy 5 days ago
parent 9ab08aaf15
commit 1c2ea75416

@ -97,6 +97,7 @@ export interface ParallelizedChunk {
/** index in instance body */
position: number;
bindings: Binding[];
type: 'promise_all' | 'all';
}
export type Context = import('zimmerframe').Context<AST.SvelteNode, ClientTransformState>;

@ -152,12 +152,17 @@ export function Program(node, context) {
chunk.position + offset,
0,
b.declaration(chunk.kind, [
b.declarator(declarator.id, b.call(b.await(b.call('$.save', declarator.init))))
b.declarator(
declarator.id,
chunk.type === 'promise_all'
? b.await(declarator.init)
: b.call(b.await(b.call('$.save', declarator.init)))
)
])
);
} else {
const pattern = b.array_pattern(chunk.declarators.map(({ id }) => id));
const init = b.call('$.all', ...chunk.declarators.map(({ init }) => init));
const init = b.call(`$.${chunk.type}`, ...chunk.declarators.map(({ init }) => init));
body.splice(
chunk.position + offset,
0,

@ -71,7 +71,8 @@ export function VariableDeclaration(node, context) {
];
if (
context.state.current_parallelized_chunk &&
context.state.current_parallelized_chunk.kind === node.kind
context.state.current_parallelized_chunk.kind === node.kind &&
context.state.current_parallelized_chunk.type === 'all'
) {
context.state.current_parallelized_chunk.declarators.push(...declarators);
context.state.current_parallelized_chunk.bindings.push(...bindings);
@ -79,11 +80,13 @@ export function VariableDeclaration(node, context) {
context.path.at(-1)
).body.indexOf(node);
} else {
/** @type {ParallelizedChunk} */
const chunk = {
kind: node.kind,
declarators,
position: /** @type {Program} */ (context.path.at(-1)).body.indexOf(node),
bindings
bindings,
type: 'all'
};
context.state.current_parallelized_chunk = chunk;
context.state.parallelized_chunks.push(chunk);
@ -257,7 +260,6 @@ export function VariableDeclaration(node, context) {
is_async &&
context.state.analysis.instance &&
context.state.scope === context.state.analysis.instance.scope &&
!dev &&
// TODO make it work without this
declarator.id.type === 'Identifier'
) {
@ -266,6 +268,7 @@ export function VariableDeclaration(node, context) {
...context.state.scope.get_bindings(declarator)
]);
}
const type = parallelize ? (dev ? 'promise_all' : 'all') : null;
/** @type {VariableDeclarator[]} */
const derived_declarators = [];
@ -289,7 +292,13 @@ export function VariableDeclaration(node, context) {
);
if (!parallelize) call = b.call(b.await(b.call('$.save', call)));
if (dev) call = b.call('$.tag', call, b.literal(declarator.id.name));
if (dev) {
call = b.call(
'$.tag' + (parallelize ? '_async' : ''),
call,
b.literal(declarator.id.name)
);
}
bindings.push(/** @type {Binding} */ (context.state.scope.get(declarator.id.name)));
derived_declarators.push(b.declarator(declarator.id, call));
} else {
@ -373,7 +382,8 @@ export function VariableDeclaration(node, context) {
}));
if (
context.state.current_parallelized_chunk &&
context.state.current_parallelized_chunk.kind === node.kind
context.state.current_parallelized_chunk.kind === node.kind &&
context.state.current_parallelized_chunk.type === type
) {
context.state.current_parallelized_chunk.declarators.push(...declarators);
context.state.current_parallelized_chunk.bindings.push(...bindings);
@ -381,11 +391,13 @@ export function VariableDeclaration(node, context) {
context.path.at(-1)
).body.indexOf(node);
} else {
/** @type {ParallelizedChunk} */
const chunk = {
kind: node.kind,
declarators,
position: /** @type {Program} */ (context.path.at(-1)).body.indexOf(node),
bindings
bindings,
type: /** @type {ParallelizedChunk['type']} */ (type)
};
context.state.current_parallelized_chunk = chunk;
context.state.parallelized_chunks.push(chunk);

@ -184,6 +184,17 @@ export function tag(source, label) {
return source;
}
/**
* @template T
* @param {Promise<Value<T>>} promise
* @param {string} label
* @returns {Promise<Value<T>>}
*/
export async function tag_async(promise, label) {
const source = await promise;
return tag(source, label);
}
/**
* @param {unknown} value
* @param {string} label

@ -7,7 +7,7 @@ export { add_locations } from './dev/elements.js';
export { hmr } from './dev/hmr.js';
export { create_ownership_validator } from './dev/ownership.js';
export { check_target, legacy_api } from './dev/legacy.js';
export { trace, tag, tag_proxy } from './dev/tracing.js';
export { trace, tag, tag_async, tag_proxy } from './dev/tracing.js';
export { inspect } from './dev/inspect.js';
export { async } from './dom/blocks/async.js';
export { validate_snippet_args } from './dev/validation.js';
@ -102,6 +102,7 @@ export {
all,
async_body,
for_await_track_reactivity_loss,
promise_all,
save,
track_reactivity_loss
} from './reactivity/async.js';

@ -202,3 +202,5 @@ export function all(...promises) {
)
);
}
export const promise_all = Promise.all;

Loading…
Cancel
Save