From f9c18af546bb66004234404ba27a015fbb12ee2b Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Sat, 15 Feb 2020 09:22:22 -0400 Subject: [PATCH] change init to async --- src/subspace.js | 58 ++++++++++++++++++++++--------------------------- src/utils.js | 18 +++++++++++++++ 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/src/subspace.js b/src/subspace.js index b402b7b..4272488 100644 --- a/src/subspace.js +++ b/src/subspace.js @@ -39,45 +39,39 @@ export default class Subspace { this.isWebsocketProvider = options.disableSubscriptions ? false : !!provider.on; } - init() { - return new Promise(async resolve => { - if (this.options.disableDatabase === true) { - this._db = new NullDatabase("", this.events); - } else { - this._db = new Database(this.options.dbFilename, this.events); - } - this.eventSyncer = new EventSyncer(this.web3, this.events, this._db, this.isWebsocketProvider); - this.logSyncer = new LogSyncer(this.web3, this.events, this._db); + async init() { + if (this.options.disableDatabase === true) { + this._db = new NullDatabase("", this.events); + } else { + this._db = new Database(this.options.dbFilename, this.events); + } + this.eventSyncer = new EventSyncer(this.web3, this.events, this._db, this.isWebsocketProvider); + this.logSyncer = new LogSyncer(this.web3, this.events, this._db); - this.web3.net.getId().then(netId => { - this.networkId = netId; - }); + this.networkId = await this.web3.net.getId(); - const block = await this.web3.getBlock("latest"); + const block = await this.web3.getBlock("latest"); - // Preload <= 10 blocks to calculate avg block time - if (block.number !== 0) { - const minBlock = Math.max(0, block.number - 9); - for (let i = minBlock; i < block.number; i++) { - this.latest10Blocks.push(this.web3.getBlock(i)); - } - - this.latest10Blocks = await Promise.all(this.latest10Blocks); + // Preload <= 10 blocks to calculate avg block time + if (block.number !== 0) { + const minBlock = Math.max(0, block.number - 9); + for (let i = minBlock; i < block.number; i++) { + this.latest10Blocks.push(this.web3.getBlock(i)); } - // Initial stats - this.latestBlockNumber = block.number; - this.latest10Blocks.push(block); + this.latest10Blocks = await Promise.all(this.latest10Blocks); + } - if (this.isWebsocketProvider) { - this._initNewBlocksSubscription(); - } else { - this.options.callInterval = this.options.callInterval || 1000; - this._initCallInterval(); - } + // Initial stats + this.latestBlockNumber = block.number; + this.latest10Blocks.push(block); - resolve(); - }); + if (this.isWebsocketProvider) { + this._initNewBlocksSubscription(); + } else { + this.options.callInterval = this.options.callInterval || 1000; + this._initCallInterval(); + } } contract(contractInstance) { diff --git a/src/utils.js b/src/utils.js index 054ce74..d5656c8 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,4 +1,5 @@ import createKeccakHash from "keccak"; +import {map} from "rxjs/operators"; export function isAddress(address) { return /^(0x)?[0-9a-fA-F]{40}$/i.test(address); @@ -24,3 +25,20 @@ export function toChecksumAddress(address) { } return ret; } + +export function mapFunc(observable) { + return prop => observable.pipe( + map(x => { + if (typeof prop === "string") { + return x[prop]; + } + if (Array.isArray(prop)) { + let newValues = {}; + prop.forEach(p => { + newValues[p] = x[p]; + }); + return newValues; + } + }) + ); +}