Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Last active September 10, 2019 03:39
Show Gist options
  • Select an option

  • Save alexytiger/593e67e4c4b7da7d8b249a311df7b181 to your computer and use it in GitHub Desktop.

Select an option

Save alexytiger/593e67e4c4b7da7d8b249a311df7b181 to your computer and use it in GitHub Desktop.
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