SSE Streaming
Stream values from the server as they happen, and read them with the same client call.
Return an async function* from .run(). The HTTP client is the same call; for await the result. WebSocket and batch reject streams. Notifications to a stream procedure are 204 and do not start the generator.
import { const tacho: <C extends Context = {}>() => Builder<C, undefined, unique symbol>tacho } from "tacho";
import { import zz } from "zod";
const const rpc: Builder<{}, undefined, unique symbol>rpc = tacho<{}>(): Builder<{}, undefined, unique symbol>tacho();
export const const router: {
ticks: ProcedureDef<{}, {
n: number;
}, AsyncGenerator<{
i: number;
}, void, unknown>>;
}
router = const rpc: <{
ticks: ProcedureDef<{}, {
n: number;
}, AsyncGenerator<{
i: number;
}, void, unknown>>;
}>(def: {
ticks: ProcedureDef<{}, {
n: number;
}, AsyncGenerator<{
i: number;
}, void, unknown>>;
}) => {
ticks: ProcedureDef<{}, {
n: number;
}, AsyncGenerator<{
i: number;
}, void, unknown>>;
}
rpc({
ticks: ProcedureDef<{}, {
n: number;
}, AsyncGenerator<{
i: number;
}, void, unknown>>
ticks: const rpc: Builder<{}, undefined, unique symbol>rpc.input<{
n: number;
}>(schema: Schema<{
n: number;
}>): Builder<{}, {
n: number;
}, unique symbol>
input(import zz.function object<{
n: z.ZodNumber;
}>(shape?: {
n: z.ZodNumber;
} | undefined, params?: string | {
error?: string | z.core.$ZodErrorMap<NonNullable<z.core.$ZodIssueInvalidType<unknown> | z.core.$ZodIssueUnrecognizedKeys>> | undefined;
message?: string | undefined | undefined;
} | undefined): z.ZodObject<{
n: z.ZodNumber;
}, z.core.$strip>
object({ n: z.ZodNumbern: import zz.function number(params?: string | z.core.$ZodNumberParams): z.ZodNumbernumber() })).run: <AsyncGenerator<{
i: number;
}, void, unknown>>(fn: (opts: {
input: {
n: number;
};
ctx: {};
}) => AsyncGenerator<{
i: number;
}, void, unknown> | Promise<AsyncGenerator<{
i: number;
}, void, unknown>>) => ProcedureDef<{}, {
n: number;
}, AsyncGenerator<{
i: number;
}, void, unknown>>
run(async function* ({
input: {
n: number;
}
input,
}) {
for (let let i: numberi = 0; let i: numberi < input: {
n: number;
}
input.n: numbern; let i: numberi++) yield { i: numberi };
}),
});
const const ticks: AsyncGenerator<{
i: number;
}, void, any>
ticks = await const client: RPCClient<{
ticks: ProcedureDef<{}, {
n: number;
}, AsyncGenerator<{
i: number;
}, void, unknown>>;
}>
client.ticks: (input: {
n: number;
}, opts?: CallOptions | undefined) => Promise<AsyncGenerator<{
i: number;
}, void, any>>
ticks({ n: numbern: 3 });
for await (const const t: {
i: number;
}
t of const ticks: AsyncGenerator<{
i: number;
}, void, any>
ticks) var console: Consoleconsole.Console.log(...data: any[]): voidThe **`console.log()`** static method outputs a message to the console.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log(const t: {
i: number;
}
t.i: numberi);
await const ticks: AsyncGenerator<{
i: number;
}, void, any>
ticks.AsyncGenerator<{ i: number; }, void, any>.return(value: void | PromiseLike<void>): Promise<IteratorResult<{
i: number;
}, void>>
return();
Pass { signal } as the second argument to abort the fetch. await ticks.return() also cancels.
const const ac: AbortControllerac = new var AbortController: new () => AbortControllerThe **`AbortController`** interface represents a controller object that allows you to abort one or more Web requests as and when desired.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortController)AbortController();
const const ticks: AsyncGenerator<{
i: number;
}, void, any>
ticks = await const client: RPCClient<{
ticks: ProcedureDef<{}, {
n: number;
}, AsyncGenerator<{
i: number;
}, void, unknown>>;
}>
client.ticks: (input: {
n: number;
}, opts?: CallOptions | undefined) => Promise<AsyncGenerator<{
i: number;
}, void, any>>
ticks({ n: numbern: 3 }, { signal?: AbortSignal | undefinedsignal: const ac: AbortControllerac.AbortController.signal: AbortSignalThe **`signal`** read-only property of the AbortController interface returns an AbortSignal object instance, which can be used to communicate with/abort an asynchronous operation as desired.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortController/signal)signal });
const ac: AbortControllerac.AbortController.abort(reason?: any): voidThe **`abort()`** method of the AbortController interface aborts an asynchronous operation before it has completed. This is able to abort fetch requests, the consumption of any response bodies, or streams.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortController/abort)abort();
Wire: each yield is data: { jsonrpc, id, result }. Handler return is event: done. Throw is event: error.