How to use the @0x/utils.intervalUtils.setAsyncExcludingInterval function in @0x/utils

To help you get started, we’ve selected a few @0x/utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github 0xProject / 0x-monorepo / packages / testnet-faucets / src / ts / dispatch_queue.ts View on Github external
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);
            },
        );
    }
}
github 0xProject / 0x-monorepo / packages / orderbook / src / order_provider / sra_polling_order_provider.ts View on Github external
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;
    }
github 0xProject / 0x-monorepo / packages / instant / src / util / heartbeater.ts View on Github external
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);
    }
github 0xProject / 0x-monorepo / packages / website / ts / blockchain.ts View on Github external
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 {
github 0xProject / 0x-monorepo / packages / website / ts / blockchain_watcher.ts View on Github external
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 {
github 0xProject / 0x-monorepo / packages / website / ts / blockchain.ts View on Github external
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);
                },
github 0xProject / 0x-monorepo / packages / web3-wrapper / src / web3_wrapper.ts View on Github external
(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,
github 0xProject / 0x-monorepo / packages / order-watcher / src / order_watcher / order_watcher.ts View on Github external
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);
            },
        );
    }
    /**