aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util/progress.ts
diff options
context:
space:
mode:
authorSteve Klebanoff <steve.klebanoff@gmail.com>2018-10-31 01:18:00 +0800
committerSteve Klebanoff <steve.klebanoff@gmail.com>2018-10-31 01:18:00 +0800
commit25039a036c13336eba82904ecb53f6517286c0b5 (patch)
treedd954b9a01b06a367bbbf541116222dc34751606 /packages/instant/src/util/progress.ts
parent728f70f51b2fe4c5037e7b74a5dfb29f1f8ca4c9 (diff)
downloaddexon-0x-contracts-25039a036c13336eba82904ecb53f6517286c0b5.tar
dexon-0x-contracts-25039a036c13336eba82904ecb53f6517286c0b5.tar.gz
dexon-0x-contracts-25039a036c13336eba82904ecb53f6517286c0b5.tar.bz2
dexon-0x-contracts-25039a036c13336eba82904ecb53f6517286c0b5.tar.lz
dexon-0x-contracts-25039a036c13336eba82904ecb53f6517286c0b5.tar.xz
dexon-0x-contracts-25039a036c13336eba82904ecb53f6517286c0b5.tar.zst
dexon-0x-contracts-25039a036c13336eba82904ecb53f6517286c0b5.zip
WIP: Progress ticker with finishing
Diffstat (limited to 'packages/instant/src/util/progress.ts')
-rw-r--r--packages/instant/src/util/progress.ts89
1 files changed, 89 insertions, 0 deletions
diff --git a/packages/instant/src/util/progress.ts b/packages/instant/src/util/progress.ts
new file mode 100644
index 000000000..33b1cc313
--- /dev/null
+++ b/packages/instant/src/util/progress.ts
@@ -0,0 +1,89 @@
+import { Dispatch } from 'redux';
+
+import { PROGRESS_TICK_INTERVAL_MS } from '../constants';
+import { Action, actions } from '../redux/actions';
+
+const curTimeUnix = () => {
+ return new Date().getTime();
+};
+
+enum TickingState {
+ Ticking,
+ Finishing,
+}
+interface TickingNormalState {
+ state: TickingState.Ticking;
+}
+interface TickingFinishingState {
+ state: TickingState.Finishing;
+ increasePercentageEveryTick: number;
+}
+type TickingStatus = TickingNormalState | TickingFinishingState;
+
+const TICKS_PER_SECOND = 1000 / PROGRESS_TICK_INTERVAL_MS;
+export class Progress {
+ 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;
+ this._percentageDone = 0;
+ this._intervalId = window.setInterval(this._tick.bind(this), PROGRESS_TICK_INTERVAL_MS); // TODO: is bind necessary?
+ this._tickingStatus = { state: TickingState.Ticking };
+ // TODO: clear interval
+ }
+
+ 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,
+ };
+ }
+
+ private _tick(): void {
+ const percentageDone =
+ this._tickingStatus.state === TickingState.Finishing
+ ? this._tickFinishing(this._tickingStatus)
+ : this._tickNormal();
+
+ // TODO: max 100
+
+ this._percentageDone = percentageDone;
+ console.log('percentageDone', this._percentageDone);
+ // TODO: max 95
+ if (percentageDone >= 100) {
+ this._clearInterval();
+ }
+ return;
+ }
+
+ // TODO: take param and move out
+ private _tickNormal(): number {
+ const elapsedTimeMs = curTimeUnix() - this._startTimeUnix;
+ // TODO: zero and negative check, use mins and maxs everywhere
+ const percentageDone = elapsedTimeMs / this._expectedTimeMs * 100;
+ return percentageDone;
+ }
+
+ private _tickFinishing(finishingState: TickingFinishingState): number {
+ return this._percentageDone + finishingState.increasePercentageEveryTick;
+ }
+
+ private _clearInterval(): void {
+ return window.clearInterval(this._intervalId);
+ }
+
+ // reset function
+
+ // end function
+}