Skip to content

Commit e9c3fa7

Browse files
committed
feat: add BatchToTrxIterator
1 parent 9551c4c commit e9c3fa7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as _ from "lodash";
2+
3+
import { UnifiedSteemTransaction } from "../blockchain/UnifiedSteemTransaction";
4+
import { AsyncIterator } from "../iterator/AsyncIterator";
5+
6+
export class BatchToTrxIterator implements AsyncIterator<UnifiedSteemTransaction> {
7+
private batchIterator: AsyncIterator<UnifiedSteemTransaction[]>;
8+
private currentBatch: UnifiedSteemTransaction[] = [];
9+
private done: boolean = false;
10+
11+
public constructor(batchIterator: AsyncIterator<UnifiedSteemTransaction[]>) {
12+
this.batchIterator = batchIterator;
13+
}
14+
15+
public async next(): Promise<IteratorResult<UnifiedSteemTransaction>> {
16+
if (this.done) throw AsyncIterator.AsyncIteratorError.iteratorAlreadyDoneError();
17+
await this.loadNextBatchIfRequired();
18+
19+
const shifted = this.currentBatch.shift();
20+
if (!shifted) throw new Error("BatchToTrxIterator: current batch could not be shifted");
21+
22+
return { done: this.done, value: shifted };
23+
}
24+
25+
private async loadNextBatchIfRequired() {
26+
if (this.currentBatch.length === 0) {
27+
const { done, value } = await this.batchIterator.next();
28+
this.currentBatch = value;
29+
this.done = done;
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)