blob: 19b448bef9c0dd0aa93abb0b7ad7b6a4b7c6011f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
import * as _ from 'lodash';
import { asyncData } from './../redux/async_data';
import { Store } from './../redux/store';
export class AccountUpdateHeartbeat {
private _intervalId?: number;
private _pendingRequest?: boolean;
private _store?: Store;
public start(store: Store, intervalTimeMs: number): void {
if (!_.isUndefined(this._intervalId)) {
throw new Error('Heartbeat is running, please stop before restarting');
}
this._store = store;
// Kick off initial first request
this._performActionAsync(true);
// Set interval for heartbeat
this._intervalId = window.setInterval(this._performActionAsync.bind(this, false), intervalTimeMs);
}
public stop(): void {
if (!_.isUndefined(this._intervalId)) {
window.clearInterval(this._intervalId);
this._resetState();
}
}
private _resetState(): void {
this._intervalId = undefined;
this._pendingRequest = false;
this._store = undefined;
}
private async _performActionAsync(setLoading: boolean): Promise<void> {
if (this._pendingRequest || _.isUndefined(this._store)) {
return;
}
this._pendingRequest = true;
try {
await asyncData.fetchAccountInfoAndDispatchToStore(this._store, { setLoading });
} finally {
this._pendingRequest = false;
}
}
}
|