Skip to content

Commit

Permalink
feat: JSON-RPC responses iterable iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Aug 14, 2024
1 parent 290ac55 commit df103ae
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
7 changes: 7 additions & 0 deletions wasm-node/javascript/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

## Unreleased

### Added

- Add `jsonRpcResponses` async iterable iterator to `Chain` connection
50 changes: 32 additions & 18 deletions wasm-node/javascript/src/internals/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,27 +552,41 @@ export function start(options: ClientOptions, wasmModule: SmoldotBytecode | Prom
default: throw new Error("Internal error: unknown json_rpc_send error code: " + retVal)
}
},
jsonRpcResponses: {
next: async () => {
while (true) {
if (!state.chains.has(chainId))
return { done: true, value: undefined };
if (options.disableJsonRpc)
throw new JsonRpcDisabledError();
if (state.instance.status === "destroyed")
throw state.instance.error;
if (state.instance.status !== "ready")
throw new Error(); // Internal error. Never supposed to happen.

// Try to pop a message from the queue.
const message = state.instance.instance.peekJsonRpcResponse(chainId);
if (message)
return { done: false, value: message };

// If no message is available, wait for one to be.
await new Promise<void>((resolve) => {
state.chains.get(chainId)!.jsonRpcResponsesPromises.push(resolve)
});
}
},
[Symbol.asyncIterator]() {
return this
}
},
nextJsonRpcResponse: async () => {
while (true) {
if (!state.chains.has(chainId))
throw new AlreadyDestroyedError();
if (options.disableJsonRpc)
return Promise.reject(new JsonRpcDisabledError());
if (state.instance.status === "destroyed")
throw state.instance.error;
if (state.instance.status !== "ready")
throw new Error(); // Internal error. Never supposed to happen.
const result = await newChain.jsonRpcResponses.next();

// Try to pop a message from the queue.
const message = state.instance.instance.peekJsonRpcResponse(chainId);
if (message)
return message;

// If no message is available, wait for one to be.
await new Promise<void>((resolve) => {
state.chains.get(chainId)!.jsonRpcResponsesPromises.push(resolve)
});
if (result.done) {
throw new AlreadyDestroyedError();
}

return result.value;
},
remove: () => {
if (state.instance.status === "destroyed")
Expand Down
14 changes: 14 additions & 0 deletions wasm-node/javascript/src/public-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,20 @@ export interface Chain {
*/
nextJsonRpcResponse(): Promise<string>;

/**
* JSON-RPC responses or notifications async iterable.
*
* Each chain contains a buffer of the responses waiting to be sent out. Iterating over this
* pulls one element from the buffer. If the iteration happen at a slower rate than
* responses are generated, then the buffer will eventually become full, at which point calling
* {@link Chain.sendJsonRpc} will throw an exception. The size of this buffer can be configured
* through {@link AddChainOptions.jsonRpcMaxPendingRequests}.
*
* @throws {@link JsonRpcDisabledError} If the JSON-RPC system was disabled in the options of the chain.
* @throws {@link CrashError} If the background client has crashed.
*/
readonly jsonRpcResponses: AsyncIterableIterator<string>

/**
* Disconnects from the blockchain.
*
Expand Down

0 comments on commit df103ae

Please sign in to comment.