Support for AbortSignal and batch processing
This release adds 2 new features:
1. Support for AbortSignal:
The methods: readState
, readStateFor
, viewState
have now an optional parameter signal: AbortSignal
https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal).
The main usecase is an ability to stop the contract evaluation in case it takes too much time:
const signal = AbortSignal.timeout(2000);
const contract = warp.contract("Daj-MNSnH55TDfxqC7v4eq0lKzVIwh98srUaWqyuZtY");
const result = await contract.readStateBatch(null, null, signal);
If the AbortSignal is triggered - the SDK will throw AbortError
.
You can also use AbortController
to have a full control over when the processing will be aborted, e.g in UI.:
const controller = new AbortController();
const signal = controller.signal;
const abortBtn = document.querySelector(".abort");
abortBtn.addEventListener("click", () => {
controller.abort();
});
const contract = warp.contract("Daj-MNSnH55TDfxqC7v4eq0lKzVIwh98srUaWqyuZtY");
const result = await contract.readStateBatch(null, null, signal);
2. Support for batch processing:
A new method has been added to the Contract
interface:
readStateBatch(pagesPerBatch: number, signal: AbortSignal): Promise<SortKeyCacheResult<EvalStateResult<State>>>
This method is helpfull for evaluating contracts with a large amount of interactions. Instead of loading all of the contract's interactions first and then evaluting them (which might consume a LOT of memory) - the SDK (when this new method is used) first loads the amount of interactions pages specified in the pagesPerBatch
parameter, evaluates them, loads next batch of interactions, etc...
The parameter pagesPerBatch
defines how many pages of interactions the implementation of the InteractionsLoader
will load.
E.g.:
-
if the
WarpGatewayInteractionsLoader
is being used (the default in case offorMainnet
Warp instance) - each page contains at most 5000 interactions.
So for thepagesPerBatch = 1
, the SDK will load 5000 interactions, evaluate them, store the result in cache, load another 5000 interactions, etc.
For thepagesPerBatch = 3
, the SDK will load 15000 interactions, evaluate them, load next 15000 interactions, etc. -
if the
ArweaveGatewayInteractionsLoader
is being used - each page contains at most 100 interactions. So for thepagesPerBatch = 1
, the SDK will load 100 interactions, evaluate them, store the result in cache, load another 100 interactions, etc.
ForpagesPerBatch = 5
- it will load 500 interactions, evaluate them, load another 500 interactions, etc..