Skip to content

Commit a5fadd9

Browse files
committed
feat(progress): adds queuing service to progress
1 parent 101a405 commit a5fadd9

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

demo/components/DemoProgress.svelte

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@
6464
6565
const label = $derived(NeoProgressStatus.Active === controlledState ? 'pause' : 'play');
6666
67-
$inspect(controlledState);
68-
6967
const bar = $state<NeoProgressBarProps>({
7068
elevation: -1,
7169
borderless: false,

src/lib/progress/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export { default as NeoProgress } from './NeoProgress.svelte';
22
export { default as NeoProgressBar } from './NeoProgressBar.svelte';
33

4+
export * from './neo-progress-service.svelte';
45
export { NeoProgressDirection, NeoProgressStatus } from './neo-progress.model.js';
56

67
export type * from './neo-progress-bar.model.js';
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import type { NeoProgressHTMLElement, NeoProgressStart } from '~/progress/neo-progress.model.js';
2+
3+
import { getUUID } from '@dvcol/common-utils/common/string';
4+
import { SvelteSet } from 'svelte/reactivity';
5+
6+
import { NeoProgressStatus } from '~/progress/neo-progress.model.js';
7+
8+
/**
9+
* Queuing service to keep track of concurrent call to progress bar
10+
*/
11+
export class NeoProgressService {
12+
readonly #ref: NeoProgressHTMLElement;
13+
#active = $state(new SvelteSet<string>());
14+
15+
get ref() {
16+
return this.#ref;
17+
}
18+
19+
get value() {
20+
return this.ref.value;
21+
}
22+
23+
get buffer() {
24+
return this.ref.buffer;
25+
}
26+
27+
get status() {
28+
return this.ref.status;
29+
}
30+
31+
get active(): Set<string> {
32+
return $state.snapshot(this.#active);
33+
}
34+
35+
constructor(ref: NeoProgressHTMLElement) {
36+
this.#ref = ref;
37+
}
38+
39+
sync() {
40+
if (this.status === NeoProgressStatus.Active) return;
41+
if (this.status === NeoProgressStatus.Indeterminate) return;
42+
this.#active.clear();
43+
return this.active;
44+
}
45+
46+
start(opts?: NeoProgressStart, id: string = getUUID()): string | undefined {
47+
this.sync();
48+
this.#active.add(id);
49+
if (this.status === NeoProgressStatus.Active) this.ref.reset(true, opts);
50+
else this.ref.start(opts);
51+
return id;
52+
}
53+
54+
cancel(id?: string, force = !id): string | undefined {
55+
this.sync();
56+
if (id) this.#active.delete(id);
57+
if (force || this.#active.size === 0) void this.ref.cancel();
58+
return id;
59+
}
60+
61+
complete(id?: string, force = !id): string | undefined {
62+
this.sync();
63+
if (id) this.#active.delete(id);
64+
if (force || this.#active.size === 0) void this.ref.complete();
65+
return id;
66+
}
67+
68+
error(id?: string, force = !id): string | undefined {
69+
this.sync();
70+
if (id) this.#active.delete(id);
71+
if (force || this.#active.size === 0) void this.ref.complete({ state: NeoProgressStatus.Error });
72+
return id;
73+
}
74+
75+
success(id?: string, force = !id): string | undefined {
76+
this.sync();
77+
if (id) this.#active.delete(id);
78+
if (force || this.#active.size === 0) void this.ref.complete({ state: NeoProgressStatus.Success });
79+
return id;
80+
}
81+
82+
warning(id?: string, force = !id): string | undefined {
83+
this.sync();
84+
if (id) this.#active.delete(id);
85+
if (force || this.#active.size === 0) void this.ref.complete({ state: NeoProgressStatus.Warning });
86+
return id;
87+
}
88+
}

0 commit comments

Comments
 (0)