change init to async

This commit is contained in:
Richard Ramos 2020-02-15 09:22:22 -04:00
parent 0bfaf32228
commit f9c18af546
2 changed files with 44 additions and 32 deletions

View File

@ -39,45 +39,39 @@ export default class Subspace {
this.isWebsocketProvider = options.disableSubscriptions ? false : !!provider.on; this.isWebsocketProvider = options.disableSubscriptions ? false : !!provider.on;
} }
init() { async init() {
return new Promise(async resolve => { if (this.options.disableDatabase === true) {
if (this.options.disableDatabase === true) { this._db = new NullDatabase("", this.events);
this._db = new NullDatabase("", this.events); } else {
} else { this._db = new Database(this.options.dbFilename, this.events);
this._db = new Database(this.options.dbFilename, this.events); }
} this.eventSyncer = new EventSyncer(this.web3, this.events, this._db, this.isWebsocketProvider);
this.eventSyncer = new EventSyncer(this.web3, this.events, this._db, this.isWebsocketProvider); this.logSyncer = new LogSyncer(this.web3, this.events, this._db);
this.logSyncer = new LogSyncer(this.web3, this.events, this._db);
this.web3.net.getId().then(netId => { this.networkId = await this.web3.net.getId();
this.networkId = netId;
});
const block = await this.web3.getBlock("latest"); const block = await this.web3.getBlock("latest");
// Preload <= 10 blocks to calculate avg block time // Preload <= 10 blocks to calculate avg block time
if (block.number !== 0) { if (block.number !== 0) {
const minBlock = Math.max(0, block.number - 9); const minBlock = Math.max(0, block.number - 9);
for (let i = minBlock; i < block.number; i++) { for (let i = minBlock; i < block.number; i++) {
this.latest10Blocks.push(this.web3.getBlock(i)); this.latest10Blocks.push(this.web3.getBlock(i));
}
this.latest10Blocks = await Promise.all(this.latest10Blocks);
} }
// Initial stats this.latest10Blocks = await Promise.all(this.latest10Blocks);
this.latestBlockNumber = block.number; }
this.latest10Blocks.push(block);
if (this.isWebsocketProvider) { // Initial stats
this._initNewBlocksSubscription(); this.latestBlockNumber = block.number;
} else { this.latest10Blocks.push(block);
this.options.callInterval = this.options.callInterval || 1000;
this._initCallInterval();
}
resolve(); if (this.isWebsocketProvider) {
}); this._initNewBlocksSubscription();
} else {
this.options.callInterval = this.options.callInterval || 1000;
this._initCallInterval();
}
} }
contract(contractInstance) { contract(contractInstance) {

View File

@ -1,4 +1,5 @@
import createKeccakHash from "keccak"; import createKeccakHash from "keccak";
import {map} from "rxjs/operators";
export function isAddress(address) { export function isAddress(address) {
return /^(0x)?[0-9a-fA-F]{40}$/i.test(address); return /^(0x)?[0-9a-fA-F]{40}$/i.test(address);
@ -24,3 +25,20 @@ export function toChecksumAddress(address) {
} }
return ret; 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;
}
})
);
}