-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathloading-state.js
109 lines (103 loc) · 3 KB
/
loading-state.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Copyright 2018 The WPT Dashboard Project. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/**
* LoadingState is a behaviour component for indicating when information is
* still being loaded (generally, fetched).
*/
const LoadingState = (superClass) => class extends superClass {
static get properties() {
return {
/**
* The number of active loading operations.
*/
loadingCount: {
type: Number,
value: 0,
observer: '_loadingCountChanged',
},
/**
* Whether the component is currently loading data.
* Computed based on `loadingCount`.
*/
isLoading: {
type: Boolean,
value: false,
computed: '_computeIsLoading(loadingCount)',
readOnly: true,
},
/**
* A callback function to be executed when loading is complete.
*/
onLoadingComplete: {
type: Function,
},
};
}
/**
* Computes the `isLoading` property based on `loadingCount`.
* @param {number} loadingCount The current loading count.
* @return {boolean} True if loading, false otherwise.
*/
_computeIsLoading(loadingCount) {
return loadingCount > 0;
}
/**
* Observer for `loadingCount` changes.
* Calls `onLoadingComplete` when loading finishes.
* @param {number} now The new loading count.
* @param {number} then The previous loading count.
*/
_loadingCountChanged(now, then) {
if (now === 0 && then > 0 && this.onLoadingComplete) {
this.onLoadingComplete();
}
}
/**
* Tracks a promise, incrementing `loadingCount` while it's pending.
* @param {Promise} promise The promise to track.
* @param {Function} opt_errHandler An optional error handler.
* @return {Promise} A promise that resolves/rejects with the original promise.
*/
async load(promise, opt_errHandler) {
this.loadingCount++;
try {
return await promise;
} catch (e) {
// eslint-disable-next-line no-console
console.error(`Failed to load: ${e}`);
if (opt_errHandler) {
opt_errHandler(e);
}
} finally {
this.loadingCount--;
}
}
/**
* Retries a function with exponential backoff.
* @param {Function} f The function to retry.
* @param {Function} shouldRetry A function that determines if retrying should continue.
* @param {number} num The maximum number of retries.
* @param {number} wait The initial wait time in milliseconds.
* @return {Promise} A promise that resolves with the result of `f` or rejects.
*/
retry(f, shouldRetry, num, wait) {
let count = 0;
const retry = () => {
count++;
return f().catch(err => {
if (count >= num || !shouldRetry(err)) {
throw err;
}
return new Promise((resolve, reject) => window.setTimeout(
() => retry().then(resolve, reject),
wait
));
});
};
return retry();
}
};
export { LoadingState };