|
| 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