Skip to content

Commit

Permalink
WIP: Update counter.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgp1130 committed Dec 5, 2023
1 parent 3be28bf commit 0bd048f
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/demo/auto-counter.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { component } from 'hydroactive';
import { cached, signal } from 'hydroactive/signals.js';

/** Automatically increments the count over time. */
export const AutoCounter = component('auto-counter', (comp) => {
const label = comp.host.query('span')!;
let count = Number(label.text);
const countStr = signal(label.text);
const count = cached(() => Number(countStr()));

comp.connected(() => {
const id = setInterval(() => {
count++;
label.native.textContent = count.toString();
countStr.set((count() + 1).toString());
}, 1_000);

return () => {
clearInterval(id);
};
});

comp.effect(() => {
label.native.textContent = count().toString();
});
});

0 comments on commit 0bd048f

Please sign in to comment.