Skip to content

Commit

Permalink
feat: add BatchToTrxIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
Jblew committed Feb 8, 2019
1 parent 9551c4c commit e9c3fa7
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/accounthistory/BatchToTrxIterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as _ from "lodash";

import { UnifiedSteemTransaction } from "../blockchain/UnifiedSteemTransaction";
import { AsyncIterator } from "../iterator/AsyncIterator";

export class BatchToTrxIterator implements AsyncIterator<UnifiedSteemTransaction> {
private batchIterator: AsyncIterator<UnifiedSteemTransaction[]>;
private currentBatch: UnifiedSteemTransaction[] = [];
private done: boolean = false;

public constructor(batchIterator: AsyncIterator<UnifiedSteemTransaction[]>) {
this.batchIterator = batchIterator;
}

public async next(): Promise<IteratorResult<UnifiedSteemTransaction>> {
if (this.done) throw AsyncIterator.AsyncIteratorError.iteratorAlreadyDoneError();
await this.loadNextBatchIfRequired();

const shifted = this.currentBatch.shift();
if (!shifted) throw new Error("BatchToTrxIterator: current batch could not be shifted");

return { done: this.done, value: shifted };
}

private async loadNextBatchIfRequired() {
if (this.currentBatch.length === 0) {
const { done, value } = await this.batchIterator.next();
this.currentBatch = value;
this.done = done;
}
}
}

0 comments on commit e9c3fa7

Please sign in to comment.