forked from web-platform-tests/wpt.fyi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loading-state.js
73 lines (66 loc) · 1.64 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
/**
* 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).
*/
// eslint-disable-next-line no-unused-vars
const LoadingState = (superClass) => class extends superClass {
static get properties() {
return {
loadingCount: {
type: Number,
value: 0,
observer: 'loadingCountChanged',
},
isLoading: {
type: Boolean,
computed: 'computeIsLoading(loadingCount)',
notify: true,
},
onLoadingComplete: Function,
};
}
computeIsLoading(loadingCount) {
return !!loadingCount;
}
loadingCountChanged(now, then) {
if (now === 0 && then > 0 && this.onLoadingComplete) {
this.onLoadingComplete();
}
}
async load(promise, opt_errHandler) {
this.loadingCount++;
try {
return await promise;
} catch (e) {
// eslint-disable-next-line no-console
console.log(`Failed to load: ${e}`);
if (opt_errHandler) {
opt_errHandler(e);
}
} finally {
this.loadingCount--;
}
}
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 };