-
Notifications
You must be signed in to change notification settings - Fork 54
/
ProviderTracker.ts
48 lines (44 loc) · 1.33 KB
/
ProviderTracker.ts
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
48
import { BehaviorSubject } from 'rxjs';
export interface ProviderFnStats {
numCalls: number;
numResponses: number;
numFailures: number;
/**
* Either:
* - at least 1 response received
* - was set manually (via setStatInitialized) to indicate that it won't make a request on wallet initialization
*/
initialized?: boolean;
didLastRequestFail?: boolean;
}
export const CLEAN_FN_STATS: ProviderFnStats = { numCalls: 0, numFailures: 0, numResponses: 0 };
/** Wraps a Provider, tracking # of calls of each function */
export abstract class ProviderTracker {
protected async trackedCall<T>(call: () => Promise<T>, tracker: BehaviorSubject<ProviderFnStats>) {
tracker.next({ ...tracker.value, numCalls: tracker.value.numCalls + 1 });
try {
const result = await call();
const numResponses = tracker.value.numResponses + 1;
tracker.next({
...tracker.value,
didLastRequestFail: false,
initialized: true,
numResponses
});
return result;
} catch (error) {
tracker.next({
...tracker.value,
didLastRequestFail: true,
numFailures: tracker.value.numFailures + 1
});
throw error;
}
}
setStatInitialized(tracker: BehaviorSubject<ProviderFnStats>) {
tracker.next({
...tracker.value,
initialized: true
});
}
}