Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Created January 11, 2020 03:10
Show Gist options
  • Save alexytiger/2f84c46a3bca857b399de09eef28c1e2 to your computer and use it in GitHub Desktop.
Save alexytiger/2f84c46a3bca857b399de09eef28c1e2 to your computer and use it in GitHub Desktop.
e-book
private widgetObservable = (id: number): Observable<PurchaseWidgetModel> =>
from(this.contractToken.getContractKeyAtIndex(id)).pipe(
switchMap(key => from(this.contractToken.getContractByKey(key)).pipe(
map(address => {
const widget: PurchaseWidgetModel = {
productKey: utils.parseBytes32String(key as ethers.utils.Arrayish),
contractAddress: address as string
};
return widget;
})
))
)
public getPurchaseContractList(): Observable<PurchaseWidgetModel[]> {
return from(this.contractToken.getContractCount()).pipe(
map((bigNumber: ethers.utils.BigNumber) => bigNumber.toNumber()),
tap((contractCount: number) => console.log('contractCount: ', contractCount)),
switchMap((contractCount: number) => {
if (contractCount === 0) {
return of([]);
} else {
// we get array [0,1,....contractCount-1]
const countArr: number[] = Array.from(Array(contractCount)).map((e, i) => i);
const source = of(countArr);
return source.pipe(
mergeMap(ids => forkJoin(ids.map(this.widgetObservable)))
);
}
})
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment