UNPKG

@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

37 lines (36 loc) 1.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useBlockTracking = void 0; const utils_1 = require("@graphql-tools/utils"); const shared_js_1 = require("./shared.js"); function useBlockTracking(configInput) { const config = { ...shared_js_1.DEFAULT_CONFIG, ...configInput, }; const minBlockMap = new Map(); return { onSubgraphExecute({ fusiongraph, subgraphName, executionRequest, setExecutionRequest }) { setExecutionRequest((0, shared_js_1.transformExecutionRequest)(executionRequest, config, fusiongraph, false, minBlockMap.get(subgraphName))); function handleResult(result) { const newBlockNumber = (0, shared_js_1.getNewBlockNumberFromExecutionResult)(result, config); const currentMinBlockNumber = minBlockMap.get(subgraphName); if (newBlockNumber != null && (currentMinBlockNumber == null || newBlockNumber > currentMinBlockNumber)) { minBlockMap.set(subgraphName, newBlockNumber); } } return ({ result }) => { if ((0, utils_1.isAsyncIterable)(result)) { return { onNext({ result }) { handleResult(result); }, }; } handleResult(result); return; }; }, }; } exports.useBlockTracking = useBlockTracking;