Skip to content

Commit

Permalink
feat: use createSubscriber in coffee brewer (#1085)
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinderoubaix authored Jan 7, 2025
1 parent ad84dee commit 863721b
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions svelte/demo/src/bootstrap/samples/progressbar/brewer.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
import {tick} from 'svelte';
import {createSubscriber} from 'svelte/reactivity';

export class Brewer {
#running = $state(false);
#value = $state(0);
#interval?: any;
#subscribers = 0;
readonly #subscribe = createSubscriber(() => {
this.#interval = setInterval(() => {
if (this.#running && this.#value < 100) {
this.#value = this.#value + 10;
}
}, 500);
return () => {
clearInterval(this.#interval);
this.#interval = undefined;
};
});

get value() {
if ($effect.tracking()) {
$effect(() => {
if (this.#subscribers === 0) {
this.#interval = setInterval(() => {
if (this.#running && this.#value < 100) {
this.#value = this.#value + 10;
}
}, 500);
}
this.#subscribers++;
return () => {
void tick().then(() => {
this.#subscribers--;
if (this.#subscribers === 0) {
clearInterval(this.#interval);
this.#interval = undefined;
}
});
};
});
}
this.#subscribe();
return this.#value;
}

Expand Down

0 comments on commit 863721b

Please sign in to comment.