Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: don't recompute when dependent signals come back to previous values used for last computation #197

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/signal-polyfill/src/wrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,35 @@ describe("Pruning", () => {

expect(w.getPending().length).toBe(0);
});
it("does not recompute when the dependent values go back to the ones used for last computation", () => {
const s = new Signal.State(0);
let n = 0;
const c = new Signal.Computed(() => (n++, s.get()));
expect(n).toBe(0);
expect(c.get()).toBe(0);
expect(n).toBe(1);
s.set(1);
expect(n).toBe(1);
s.set(0);
expect(n).toBe(1);
expect(c.get()).toBe(0); // the last time c was computed was with s = 0, no need to recompute
expect(n).toBe(1);
});
it("does not recompute when the dependent values go back to the ones used for last computation (with extra computed)", () => {
const s = new Signal.State(0);
let n = 0;
const extra = new Signal.Computed(() => s.get());
const c = new Signal.Computed(() => (n++, extra.get()));
expect(n).toBe(0);
expect(c.get()).toBe(0);
expect(n).toBe(1);
s.set(1);
expect(n).toBe(1);
s.set(0);
expect(n).toBe(1);
expect(c.get()).toBe(0); // the last time c was computed was with s = 0, no need to recompute
expect(n).toBe(1);
});
});

describe("Prohibited contexts", () => {
Expand Down