From 37405038e8f377b65f4dc32b09932fb9ba38efc5 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Tue, 30 Oct 2018 10:56:11 -0700 Subject: Stall at 95 percent --- packages/instant/src/constants.ts | 1 + packages/instant/src/util/progress.ts | 72 ++++++++++++++++++++++------------- 2 files changed, 46 insertions(+), 27 deletions(-) (limited to 'packages/instant/src') diff --git a/packages/instant/src/constants.ts b/packages/instant/src/constants.ts index a21de2a0f..e099044df 100644 --- a/packages/instant/src/constants.ts +++ b/packages/instant/src/constants.ts @@ -8,3 +8,4 @@ export const DEFAULT_GAS_PRICE = GWEI_IN_WEI.mul(6); export const ETH_GAS_STATION_API_BASE_URL = 'https://ethgasstation.info'; export const COINBASE_API_BASE_URL = 'https://api.coinbase.com/v2'; export const PROGRESS_TICK_INTERVAL_MS = 100; +export const PROGRESS_STALL_AT_PERCENTAGE = 95; diff --git a/packages/instant/src/util/progress.ts b/packages/instant/src/util/progress.ts index 33b1cc313..5649a78c8 100644 --- a/packages/instant/src/util/progress.ts +++ b/packages/instant/src/util/progress.ts @@ -1,6 +1,7 @@ +import * as _ from 'lodash'; import { Dispatch } from 'redux'; -import { PROGRESS_TICK_INTERVAL_MS } from '../constants'; +import { PROGRESS_STALL_AT_PERCENTAGE, PROGRESS_TICK_INTERVAL_MS } from '../constants'; import { Action, actions } from '../redux/actions'; const curTimeUnix = () => { @@ -8,42 +9,51 @@ const curTimeUnix = () => { }; enum TickingState { - Ticking, + None, + Running, Finishing, } -interface TickingNormalState { - state: TickingState.Ticking; +interface TickingNoneState { + state: TickingState.None; } -interface TickingFinishingState { +interface TickingRunningStatus { + state: TickingState.Running; +} +interface TickingFinishingStatus { state: TickingState.Finishing; increasePercentageEveryTick: number; } -type TickingStatus = TickingNormalState | TickingFinishingState; +type TickingStatus = TickingNoneState | TickingRunningStatus | TickingFinishingStatus; const TICKS_PER_SECOND = 1000 / PROGRESS_TICK_INTERVAL_MS; export class Progress { - private _startTimeUnix: number; - private _expectedTimeMs: number; - private _intervalId: number; + private _startTimeUnix?: number; + private _expectedTimeMs?: number; + private _intervalId?: number; private _percentageDone: number; private _tickingStatus: TickingStatus; - // TODO: take in dispatch - constructor(expectedTimeMs: number) { - const curTime = curTimeUnix(); - this._startTimeUnix = curTime; - this._expectedTimeMs = expectedTimeMs; + constructor() { + this._startTimeUnix = undefined; + this._expectedTimeMs = undefined; this._percentageDone = 0; - this._intervalId = window.setInterval(this._tick.bind(this), PROGRESS_TICK_INTERVAL_MS); // TODO: is bind necessary? - this._tickingStatus = { state: TickingState.Ticking }; + this._intervalId = undefined; + this._tickingStatus = { state: TickingState.None }; // TODO: clear interval } + public beginRunning(expectedTimeMs: number): void { + this._clearTimer(); + this._startTimeUnix = curTimeUnix(); + this._expectedTimeMs = expectedTimeMs; + this._percentageDone = 0; + this._intervalId = window.setInterval(this._tick.bind(this), PROGRESS_TICK_INTERVAL_MS); + this._tickingStatus = { state: TickingState.Running }; + } + public setFinishing(): void { const percentLeft = 100 - this._percentageDone; - console.log('percentLeft', percentLeft); const increasePercentageEveryTick = percentLeft / TICKS_PER_SECOND; - console.log('increase Tick', increasePercentageEveryTick); this._tickingStatus = { state: TickingState.Finishing, increasePercentageEveryTick, @@ -51,31 +61,39 @@ export class Progress { } private _tick(): void { - const percentageDone = + const rawPercentageDone = this._tickingStatus.state === TickingState.Finishing ? this._tickFinishing(this._tickingStatus) : this._tickNormal(); - // TODO: max 100 - + const maxPercentage = this._tickingStatus.state === TickingState.Finishing ? 100 : PROGRESS_STALL_AT_PERCENTAGE; + const percentageDone = Math.min(rawPercentageDone, maxPercentage); this._percentageDone = percentageDone; console.log('percentageDone', this._percentageDone); - // TODO: max 95 if (percentageDone >= 100) { - this._clearInterval(); + this._clearTimer(); } return; } - // TODO: take param and move out + private _clearTimer(): void { + if (this._intervalId) { + window.clearTimeout(this._intervalId); + } + } + private _tickNormal(): number { + if (_.isUndefined(this._startTimeUnix) || _.isUndefined(this._expectedTimeMs)) { + throw new Error('Cant tick, missing var'); + } + const elapsedTimeMs = curTimeUnix() - this._startTimeUnix; - // TODO: zero and negative check, use mins and maxs everywhere - const percentageDone = elapsedTimeMs / this._expectedTimeMs * 100; + const safeElapsedTimeMs = Math.max(elapsedTimeMs, 1); + const percentageDone = safeElapsedTimeMs / this._expectedTimeMs * 100; return percentageDone; } - private _tickFinishing(finishingState: TickingFinishingState): number { + private _tickFinishing(finishingState: TickingFinishingStatus): number { return this._percentageDone + finishingState.increasePercentageEveryTick; } -- cgit v1.2.3