Skip to content

Commit 9b666f5

Browse files
feat: use createSubscriber in coffee brewer
1 parent f1cda47 commit 9b666f5

1 file changed

Lines changed: 13 additions & 23 deletions

File tree

svelte/demo/src/bootstrap/samples/progressbar/brewer.svelte.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
1-
import {tick} from 'svelte';
1+
import {createSubscriber} from 'svelte/reactivity';
22

33
export class Brewer {
44
#running = $state(false);
55
#value = $state(0);
66
#interval?: any;
7-
#subscribers = 0;
7+
readonly #subscribe = createSubscriber(() => {
8+
this.#interval = setInterval(() => {
9+
if (this.#running && this.#value < 100) {
10+
this.#value = this.#value + 10;
11+
}
12+
}, 500);
13+
return () => {
14+
clearInterval(this.#interval);
15+
this.#interval = undefined;
16+
};
17+
});
818

919
get value() {
10-
if ($effect.tracking()) {
11-
$effect(() => {
12-
if (this.#subscribers === 0) {
13-
this.#interval = setInterval(() => {
14-
if (this.#running && this.#value < 100) {
15-
this.#value = this.#value + 10;
16-
}
17-
}, 500);
18-
}
19-
this.#subscribers++;
20-
return () => {
21-
void tick().then(() => {
22-
this.#subscribers--;
23-
if (this.#subscribers === 0) {
24-
clearInterval(this.#interval);
25-
this.#interval = undefined;
26-
}
27-
});
28-
};
29-
});
30-
}
20+
this.#subscribe();
3121
return this.#value;
3222
}
3323

0 commit comments

Comments
 (0)