-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
42 lines (36 loc) · 1.06 KB
/
index.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
export interface UrlSpinnerOptions {
spinner?: string[];
duration?: number;
}
const defaultOptions: UrlSpinnerOptions = {
spinner: ["◓", "◑", "◒", "◐"],
duration: 10000
};
export function startSpinning(options?: UrlSpinnerOptions) {
if (!options) options = defaultOptions;
else options = Object.assign({}, defaultOptions, options);
const url = location.href;
const render = makeRenderer(options.spinner!);
const timer = setInterval(render, 100, options);
if (options.duration) {
setTimeout(stopSpinning(timer, url), options.duration);
}
return stopSpinning(timer, url);
}
function makeRenderer(spinner: string[]) {
let currentSpinnerIndex = 0;
return function render() {
window.history.replaceState(null, "", `?${spinner[currentSpinnerIndex]}`);
if (currentSpinnerIndex === spinner.length - 1) {
currentSpinnerIndex = 0;
} else {
++currentSpinnerIndex;
}
};
}
function stopSpinning(timer: number, url: string) {
return () => {
if (timer) clearInterval(timer);
history.replaceState(null, "", url);
};
}