Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
add { signal } option (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber authored May 16, 2024
1 parent b2cde10 commit 14dbeb9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ Options:
- `provider`: Ethers provider
- `rpcUrl`: JSON-RPC endpoint URL
- `rpcHeader`: JSON-RPC headers object
- `signal`: Optional `AbortSignal`
21 changes: 16 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const makeRequest = async ({
address,
rpcUrl,
rpcHeaders,
fromBlock
fromBlock,
signal
}) => {
const req = {
jsonrpc: '2.0',
Expand All @@ -31,7 +32,8 @@ const makeRequest = async ({
...rpcHeaders,
'content-type': 'application/json'
},
body: JSON.stringify(req)
body: JSON.stringify(req),
signal
})
if (!res.ok) {
throw new Error(
Expand All @@ -58,7 +60,8 @@ export async function * onContractEvent ({
contract,
provider,
rpcUrl,
rpcHeaders
rpcHeaders,
signal
}) {
const address = await contract.getAddress()
let lastBlock = await provider.getBlockNumber()
Expand All @@ -76,9 +79,13 @@ export async function * onContractEvent ({
address,
rpcUrl,
rpcHeaders,
fromBlock: lastBlock
fromBlock: lastBlock,
signal
}))
} catch (err) {
if (err.name === 'AbortError') {
throw err
}
console.error(err)
}

Expand All @@ -95,7 +102,11 @@ export async function * onContractEvent ({
const iterationEnd = new Date()
const iterationDuration = iterationEnd - iterationStart
if (iterationDuration < MIN_POLL_INTERVAL) {
await timers.setTimeout(MIN_POLL_INTERVAL - iterationDuration)
await timers.setTimeout(
MIN_POLL_INTERVAL - iterationDuration,
null,
{ signal }
)
}
}
}

0 comments on commit 14dbeb9

Please sign in to comment.