-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbufferedReading.ts
More file actions
27 lines (24 loc) · 958 Bytes
/
bufferedReading.ts
File metadata and controls
27 lines (24 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { ProximaStreamClient, Offset, BufferedStreamReader } from "..";
import { strict as assert } from "assert";
export function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function main() {
const client = new ProximaStreamClient();
const name = "proxima.eth-main.blocks.1_0";
const pauseable = await client.streamEvents(name, Offset.zero);
const bufferSize = 10000;
const chunkSize = 1000;
const streamReader = BufferedStreamReader.fromStream(pauseable, bufferSize);
while (true) {
const chunk = await streamReader.read(chunkSize);
if (chunk === undefined) {
console.log("Completed");
break;
}
assert(chunk.length <= chunkSize);
console.log(`Processing batch from ${chunk[0].offset} to ${chunk[chunk.length - 1].offset}...`);
await sleep(500);
}
}
main().catch(err => console.error(err));