Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 40 additions & 27 deletions packages/vortex-core/src/std/awaited.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,53 @@ import { type Signal, useState } from "../signal";
* @deprecated This is a very delicate API, it's reccommended that unless you're running exclusively on the client, you use `useAwait` to get a callable await function.
*/
export function awaited<T>(value: Promise<T>): Signal<T | undefined> {
const result = useState<T | undefined>(undefined);
const streaming = useOptionalStreaming();
const result = useState<T | undefined>(undefined);
const streaming = useOptionalStreaming();

async function fetchValue() {
if (streaming) {
using _loading = streaming.markLoading();
result.set(await value);
} else {
result.set(await value);
}
}
async function fetchValue() {
if (streaming) {
using _loading = streaming.markLoading();
result.set(await value);
} else {
result.set(await value);
}
}

fetchValue();
fetchValue();

return result;
return result;
}

export function useAwait(): <T>(value: Promise<T>) => Signal<T | undefined> {
const streaming = useOptionalStreaming();
export function useAwait(): <T>(value: Promise<T> | T) => Signal<T | undefined> {
const streaming = useOptionalStreaming();

return <T>(value: Promise<T>) => {
const result = useState<T | undefined>(undefined);
return <T>(value: Promise<T> | T) => {
const result = useState<T | undefined>(undefined);

async function fetchValue() {
if (streaming) {
using _loading = streaming.markLoading();
result.set(await value);
} else {
result.set(await value);
}
}
if (!(value instanceof Promise)) {
result.set(value);
return result;
}

fetchValue();
if (typeof Bun !== "undefined") {
const peeked = Bun.peek(value);

return result;
}
if (!(peeked instanceof Promise)) {
result.set(peeked);
}
}

async function fetchValue() {
if (streaming) {
using _loading = streaming.markLoading();
result.set(await value);
} else {
result.set(await value);
}
}

fetchValue();

return result;
}
}
7 changes: 4 additions & 3 deletions packages/wormhole/src/build/adapters/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function DevAdapter(): DevAdapter {

codegenSource += `const entrypointProps = JSON.parse(${JSON.stringify(JSON.stringify(entrypointProps))});`;

codegenSource += `export function main(props) {`;
codegenSource += `export async function main(props) {`;

codegenSource += 'const loaders = [';

Expand All @@ -78,14 +78,15 @@ export function DevAdapter(): DevAdapter {
codegenSource += `const root = document.documentElement;`;
}

codegenSource += `return INTERNAL_entrypoint({
codegenSource += `return await INTERNAL_entrypoint({
props: entrypointProps,
loaders,
renderer,
root,
pathname: props.pathname,
context: props.context,
lifetime: props.lifetime ?? new Lifetime(),`;
lifetime: props.lifetime ?? new Lifetime(),
preload: true,`;

if (location === "client") {
codegenSource += `supplement: props.supplement,`;
Expand Down
1 change: 1 addition & 0 deletions packages/wormhole/src/build/adapters/vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ export function VercelAdapter(): VercelAdapter {
pathname,
context,
lifetime,
preload: true
});`;
codegenSource += `const streamutil = INTERNAL_createStreamUtility();`;
codegenSource += `const html = printHTML(root);`;
Expand Down
Loading