Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private _start(): void {
this._queueIntervalIdIfExists = intervalUtils.setAsyncExcludingInterval(
async () => {
const taskAsync = this._queue.shift();
if (taskAsync === undefined) {
return Promise.resolve();
}
await taskAsync();
},
this._queueIntervalMs,
(err: Error) => {
logUtils.log(`Unexpected err: ${err} - ${JSON.stringify(err)}`);
// tslint:disable-next-line:no-floating-promises
errorReporter.reportAsync(err);
},
);
}
}
private _createPollingSubscription(makerAssetData: string, takerAssetData: string): NodeJS.Timer {
const assetPairKey = OrderStore.getKeyForAssetPair(makerAssetData, takerAssetData);
const pollingIntervalId = intervalUtils.setAsyncExcludingInterval(
async () => {
const previousOrderSet = await this._orderStore.getOrderSetForAssetPairAsync(assetPairKey);
const orders = await this._fetchLatestOrdersAsync(makerAssetData, takerAssetData);
const orderSet = new OrderSet();
await orderSet.addManyAsync(orders);
const diff = await previousOrderSet.diffAsync(orderSet);
await this._updateStoreAsync({ ...diff, assetPairKey });
},
this._pollingIntervalMs,
(_: Error) => {
// TODO(dave4506) Add richer errors
throw new Error(`Fetching latest orders for asset pair ${makerAssetData}/${takerAssetData}`);
},
);
return pollingIntervalId;
}
public start(intervalTimeMs: number): void {
if (this._intervalId !== undefined) {
throw new Error('Heartbeat is running, please stop before restarting');
}
if (this._performImmediatelyOnStart) {
// tslint:disable-next-line:no-floating-promises
this._performFunction();
}
// tslint:disable-next-line:no-unbound-method
this._intervalId = intervalUtils.setAsyncExcludingInterval(this._performFunction, intervalTimeMs, _.noop);
}
private _startWatchingGasPrice(): void {
if (!_.isUndefined(this._watchGasPriceIntervalId)) {
return; // we are already watching
}
const oneMinuteInMs = 60000;
// tslint:disable-next-line:no-floating-promises
this._updateDefaultGasPriceAsync();
this._watchGasPriceIntervalId = intervalUtils.setAsyncExcludingInterval(
this._updateDefaultGasPriceAsync.bind(this),
oneMinuteInMs,
(err: Error) => {
logUtils.log(`Watching gas price failed: ${err.stack}`);
this._stopWatchingGasPrice();
},
);
}
private _stopWatchingGasPrice(): void {
public async startEmittingUserBalanceStateAsync(): Promise {
if (this._watchBalanceIntervalId !== undefined) {
return; // we are already emitting the state
}
this._prevUserEtherBalanceInWei = undefined;
await this._updateBalanceAsync();
this._watchBalanceIntervalId = intervalUtils.setAsyncExcludingInterval(
this._updateBalanceAsync.bind(this),
5000,
(err: Error) => {
logUtils.log(`Watching network and balances failed: ${err.stack}`);
this._stopEmittingUserBalanceState();
},
);
}
private async _updateBalanceAsync(): Promise {
const newTokenBalancePromise = new Promise((resolve: (balance: BigNumber) => void, reject) => {
const tokenPollInterval = intervalUtils.setAsyncExcludingInterval(
async () => {
const [balance] = await this.getTokenBalanceAndAllowanceAsync(
this._userAddressIfExists,
token.address,
);
if (!balance.eq(currBalance)) {
intervalUtils.clearAsyncExcludingInterval(tokenPollInterval);
resolve(balance);
}
},
5000,
(err: Error) => {
logUtils.log(`Polling tokenBalance failed: ${err}`);
intervalUtils.clearAsyncExcludingInterval(tokenPollInterval);
reject(err);
},
(resolve: (receipt: TransactionReceiptWithDecodedLogs) => void, reject) => {
const intervalId = intervalUtils.setAsyncExcludingInterval(
async () => {
if (wasTimeoutExceeded) {
intervalUtils.clearAsyncExcludingInterval(intervalId);
return reject(Web3WrapperErrors.TransactionMiningTimeout);
}
transactionReceipt = await this.getTransactionReceiptIfExistsAsync(txHash);
if (transactionReceipt !== undefined) {
intervalUtils.clearAsyncExcludingInterval(intervalId);
const logsWithDecodedArgs = _.map(
transactionReceipt.logs,
this.abiDecoder.tryToDecodeLogOrNoop.bind(this.abiDecoder),
);
const transactionReceiptWithDecodedLogArgs: TransactionReceiptWithDecodedLogs = {
...transactionReceipt,
logs: logsWithDecodedArgs,
public subscribe(callback: OnOrderStateChangeCallback): void {
assert.isFunction('callback', callback);
if (this._callbackIfExists !== undefined) {
throw new Error(OrderWatcherError.SubscriptionAlreadyPresent);
}
this._callbackIfExists = callback;
this._eventWatcher.subscribe(
this._addLockToCallbackAsync.bind(this, this._onEventWatcherCallbackAsync.bind(this)),
);
this._expirationWatcher.subscribe(this._addLockToCallbackAsync.bind(this, this._onOrderExpired.bind(this)));
this._cleanupJobIntervalIdIfExists = intervalUtils.setAsyncExcludingInterval(
this._addLockToCallbackAsync.bind(this, this._cleanupAsync.bind(this)),
this._cleanupJobInterval,
(err: Error) => {
this.unsubscribe();
callback(err);
},
);
}
/**