polywrap-client-threads-js
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

Run the Polywrap Client within a separate thread, enabling the application to terminate wrapper invocations, even in cases where WebAssembly modules are stuck in infinite loops.

If the wrapper were being invoked within the JS main thread, the infinite loop would halt the entire application.

Some reasons why you would want to use this "client thread" architecture are:

  • Your application must be long-running and robust
  • Executing foreign code

The Gelato nodes are an example of such a use-case: https://docs.gelato.network/guides/writing-a-resolver/polywrap-resolver

Example Usage (also see e2e tests):

const pool = new ThreadPool({
  maxThreads: 50,
  aquireTimeout: 200,
});
const thread = new ClientThread({
  pool
});

const job = thread.invoke({
  uri: "ens/v2.uniswap.web3api.eth",
  module: "query",
  method: "fetchTokenData",
  input: {
    chainId: "MAINNET",
    address: "0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9",
  }
});

await new Promise<void>((resolve) => {
  setTimeout(() => resolve(), 300);
});

job.getStatus(); // ThreadStatus.RUNNING

job.terminate();

job.getStatus(); // ThreadStatus.TERMINATED
job.getResult(); // undefined
job.getError(); // undefined
await job.promise // { }

Readme

Keywords

none

Package Sidebar

Install

npm i polywrap-client-threads-js

Weekly Downloads

6

Version

0.3.0

License

none

Unpacked Size

5.16 MB

Total Files

36

Last publish

Collaborators

  • brandon.cs