Skip to content

Commit

Permalink
feat: converting generators to async iterables
Browse files Browse the repository at this point in the history
Related #500
Related #501

[ci skip]
  • Loading branch information
tegefaulkes committed Feb 10, 2023
1 parent ff8e311 commit 1b33d12
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/RPC/RPCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class RPCClient<M extends ClientManifest> {
public async serverStreamCaller<I extends JSONValue, O extends JSONValue>(
method: string,
parameters: I,
): Promise<AsyncGenerator<O>> {
): Promise<AsyncIterable<O>> {
const callerInterface = await this.rawDuplexStreamCaller<I, O>(method);
const writer = callerInterface.writable.getWriter();
await writer.write(parameters);
Expand All @@ -183,7 +183,7 @@ class RPCClient<M extends ClientManifest> {
@ready(new rpcErrors.ErrorRpcDestroyed())
public async clientStreamCaller<I extends JSONValue, O extends JSONValue>(
method: string,
f: (output: Promise<O>) => AsyncGenerator<I | undefined>,
f: (output: Promise<O>) => AsyncIterable<I | undefined>,
): Promise<void> {
const callerInterface = await this.rawClientStreamCaller<I, O>(method);
const writer = callerInterface.writable.getWriter();
Expand All @@ -203,7 +203,7 @@ class RPCClient<M extends ClientManifest> {
@ready(new rpcErrors.ErrorRpcDestroyed())
public async duplexStreamCaller<I extends JSONValue, O extends JSONValue>(
method: string,
f: (output: AsyncGenerator<O>) => AsyncGenerator<I>,
f: (output: AsyncIterable<O>) => AsyncIterable<I>,
): Promise<void> {
const callerInterface = await this.rawDuplexStreamCaller<I, O>(method);
const outputGenerator = async function* () {
Expand Down
2 changes: 1 addition & 1 deletion src/RPC/RPCServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class RPCServer {
const events = this.events;
const outputGen = async function* (): AsyncGenerator<JsonRpcResponse> {
if (ctx.signal.aborted) throw ctx.signal.reason;
const dataGen = async function* () {
const dataGen = async function* (): AsyncIterable<I> {
for await (const data of forwardStream) {
yield data.params as I;
}
Expand Down
12 changes: 6 additions & 6 deletions src/RPC/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ type RawHandlerImplementation = HandlerImplementation<
type DuplexHandlerImplementation<
I extends JSONValue = JSONValue,
O extends JSONValue = JSONValue,
> = HandlerImplementation<AsyncGenerator<I>, AsyncGenerator<O>>;
> = HandlerImplementation<AsyncIterable<I>, AsyncIterable<O>>;
type ServerHandlerImplementation<
I extends JSONValue = JSONValue,
O extends JSONValue = JSONValue,
> = HandlerImplementation<I, AsyncGenerator<O>>;
> = HandlerImplementation<I, AsyncIterable<O>>;
type ClientHandlerImplementation<
I extends JSONValue = JSONValue,
O extends JSONValue = JSONValue,
> = HandlerImplementation<AsyncGenerator<I>, Promise<O>>;
> = HandlerImplementation<AsyncIterable<I>, Promise<O>>;
type UnaryHandlerImplementation<
I extends JSONValue = JSONValue,
O extends JSONValue = JSONValue,
Expand All @@ -156,17 +156,17 @@ type UnaryCallerImplementation<
type ServerCallerImplementation<
I extends JSONValue = JSONValue,
O extends JSONValue = JSONValue,
> = (parameters: I) => Promise<AsyncGenerator<O>>;
> = (parameters: I) => Promise<AsyncIterable<O>>;

type ClientCallerImplementation<
I extends JSONValue = JSONValue,
O extends JSONValue = JSONValue,
> = (f: (output: Promise<O>) => AsyncGenerator<I | undefined>) => Promise<void>;
> = (f: (output: Promise<O>) => AsyncIterable<I | undefined>) => Promise<void>;

type DuplexCallerImplementation<
I extends JSONValue = JSONValue,
O extends JSONValue = JSONValue,
> = (f: (output: AsyncGenerator<O>) => AsyncGenerator<I>) => Promise<void>;
> = (f: (output: AsyncIterable<O>) => AsyncIterable<I>) => Promise<void>;

// Raw callers

Expand Down

0 comments on commit 1b33d12

Please sign in to comment.