Skip to content

Commit b9d4e00

Browse files
committed
indexer: update reloadIndexer() logic with context
1 parent 072545b commit b9d4e00

9 files changed

Lines changed: 29 additions & 11 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "indexer: update reloadIndexer() logic with context",
4+
"packageName": "@apibara/indexer",
5+
"email": "jadejajaipal5@gmail.com",
6+
"dependentChangeType": "patch"
7+
}

examples/cli-drizzle/indexers/2-starknet.indexer.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
import { starknetUsdcTransfers } from "@/lib/schema";
12
import { drizzleStorage, useDrizzleStorage } from "@apibara/plugin-drizzle";
23
import { drizzle } from "@apibara/plugin-drizzle";
34
import { StarknetStream } from "@apibara/starknet";
45
import { defineIndexer } from "apibara/indexer";
5-
6-
import { starknetUsdcTransfers } from "@/lib/schema";
76
import { useLogger } from "apibara/plugins";
87
import type { ApibaraRuntimeConfig } from "apibara/types";
98
import { hash } from "starknet";
@@ -16,8 +15,8 @@ export default async function (runtimeConfig: ApibaraRuntimeConfig) {
1615

1716
console.log("[2-starknet] Starknet indexer waiting.....");
1817

19-
// Simulating a API call that takes 5 seconds
20-
await new Promise((resolve) => setTimeout(resolve, 5000));
18+
// Simulating a API call that takes 500 ms
19+
await new Promise((resolve) => setTimeout(resolve, 500));
2120

2221
console.log("[2-starknet] Starknet indexer done waiting.....");
2322

packages/cli/src/core/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ export { createApibara } from "./apibara";
22
export { build } from "./build/build";
33
export { prepare } from "./build/prepare";
44
export { writeTypes } from "./build/types";
5-
export { ReloadIndexerRequest, reloadIndexer } from "./utils";

packages/cli/src/runtime/dev.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { runWithReconnect } from "@apibara/indexer";
1+
import { ReloadIndexerRequest, runWithReconnect } from "@apibara/indexer";
22
import { createAuthenticatedClient } from "@apibara/protocol";
33
import { getRuntimeDataFromEnv } from "apibara/common";
4-
import { ReloadIndexerRequest } from "apibara/core";
54
import { defineCommand, runMain } from "citty";
65
import type { ConsolaInstance } from "consola";
76
import { blueBright } from "picocolors";

packages/cli/src/runtime/start.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { runWithReconnect } from "@apibara/indexer";
1+
import { ReloadIndexerRequest, runWithReconnect } from "@apibara/indexer";
22
import { createAuthenticatedClient } from "@apibara/protocol";
33
import {
44
checkForUnknownArgs,
55
getProcessedRuntimeConfig,
66
getRuntimeDataFromEnv,
77
} from "apibara/common";
8-
import { ReloadIndexerRequest } from "apibara/core";
98
import { defineCommand, runMain } from "citty";
109
import consola, { type ConsolaInstance } from "consola";
1110
import { blueBright } from "picocolors";

packages/indexer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@
8282
"peerDependencies": {
8383
"vitest": "^1.6.0"
8484
}
85-
}
85+
}

packages/indexer/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./indexer";
22
export { useIndexerContext } from "./context";
3+
export * from "./utils";

packages/indexer/src/indexer.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
import { createIndexerMetrics, createTracer } from "./otel";
3434
import type { IndexerPlugin } from "./plugins";
3535
import { useInternalContext } from "./plugins/context";
36+
import { ReloadIndexerRequest } from "./utils";
3637

3738
export type UseMiddlewareFunction = (
3839
fn: MiddlewareFunction<IndexerContext>,
@@ -317,6 +318,11 @@ export async function run<TFilter, TBlock>(
317318
},
318319
);
319320

321+
if (context._reload) {
322+
context._reload = false;
323+
throw new ReloadIndexerRequest();
324+
}
325+
320326
await middleware(context, async () => {
321327
let block: TBlock | null;
322328

@@ -395,6 +401,11 @@ export async function run<TFilter, TBlock>(
395401
}
396402
});
397403

404+
if (context._reload) {
405+
context._reload = false;
406+
throw new ReloadIndexerRequest();
407+
}
408+
398409
span.end();
399410
});
400411

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { useIndexerContext } from "../context";
2+
13
export function reloadIndexer() {
2-
throw new ReloadIndexerRequest();
4+
const context = useIndexerContext();
5+
context._reload = true;
36
}
47

58
export class ReloadIndexerRequest extends Error {

0 commit comments

Comments
 (0)