@graphprotocol/client-block-tracking
Version:
`graph-client` implements automatic block tracking using `number_gte` filter of `graph-node`. This automates the process [of fetching and tracking the block number of entites](https://thegraph.com/docs/en/developer/distributed-systems/#polling-for-updated
33 lines (32 loc) • 1.38 kB
JavaScript
import { isAsyncIterable } from '@graphql-tools/utils';
import { DEFAULT_CONFIG, getNewBlockNumberFromExecutionResult, transformExecutionRequest, } from './shared.js';
export function useBlockTracking(configInput) {
const config = {
...DEFAULT_CONFIG,
...configInput,
};
const minBlockMap = new Map();
return {
onSubgraphExecute({ subgraph, subgraphName, executionRequest, setExecutionRequest }) {
setExecutionRequest(transformExecutionRequest(executionRequest, config, subgraph, false, minBlockMap.get(subgraphName)));
function handleResult(result) {
const newBlockNumber = getNewBlockNumberFromExecutionResult(result, config);
const currentMinBlockNumber = minBlockMap.get(subgraphName);
if (newBlockNumber != null && (currentMinBlockNumber == null || newBlockNumber > currentMinBlockNumber)) {
minBlockMap.set(subgraphName, newBlockNumber);
}
}
return ({ result }) => {
if (isAsyncIterable(result)) {
return {
onNext({ result }) {
handleResult(result);
},
};
}
handleResult(result);
return;
};
},
};
}