-
Notifications
You must be signed in to change notification settings - Fork 149
Added createRcMemo primitive #853
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
Open
clinuxrulz
wants to merge
4
commits into
solidjs-community:main
Choose a base branch
from
clinuxrulz:create-rc-memo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b350623
added createRcMemo primitive
clinuxrulz 7e73e3f
additional documentation for createRcMemo; using type-safe === instea…
clinuxrulz 6e97516
documented server side behavour for createRcMemo
clinuxrulz 0ebdd1a
added changeset for createRcMemo primitive
clinuxrulz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@solid-primitives/memo": minor | ||
| --- | ||
|
|
||
| Added createRcMemo primitive for memos that are only awake while there are listeners. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| import { describe, it, expect, vi } from "vitest"; | ||
| import { createRcMemo } from "../src/index.js"; | ||
| import { createComputed, createContext, createRoot, createSignal, getOwner, onCleanup, useContext } from "solid-js"; | ||
|
|
||
| const sleep = (ms: number) => new Promise<void>(resolve => setTimeout(resolve, ms)); | ||
| const nextTick = () => new Promise<void>(resolve => queueMicrotask(resolve)); | ||
|
|
||
| describe("createRcMemo", () => { | ||
| it("only creates a memo when there are listeners", async () => { | ||
| const [count, setCount] = createSignal(0); | ||
| let runs = 0; | ||
| const memo = createRcMemo(() => { | ||
| runs++; | ||
| return count(); | ||
| }); | ||
|
|
||
| expect(runs).toBe(0); | ||
|
|
||
| // Initial access without listener | ||
| expect(memo()).toBe(0); | ||
| expect(runs).toBe(1); | ||
|
|
||
| setCount(1); | ||
| // Still 1 because it's not tracking without listener | ||
| expect(runs).toBe(1); | ||
| expect(memo()).toBe(1); | ||
| expect(runs).toBe(2); | ||
|
|
||
| // Add listener | ||
| await createRoot(async dispose => { | ||
| createComputed(() => { | ||
| memo(); | ||
| }); | ||
| expect(runs).toBe(3); | ||
|
|
||
| setCount(2); | ||
| expect(runs).toBe(4); | ||
| expect(memo()).toBe(2); | ||
| expect(runs).toBe(4); // Should be memoized | ||
|
|
||
| dispose(); | ||
| await nextTick(); | ||
| }); | ||
|
|
||
| // After disposal and microtask, memo should be gone | ||
| setCount(3); | ||
| expect(runs).toBe(4); | ||
| expect(memo()).toBe(3); | ||
| expect(runs).toBe(5); | ||
| }); | ||
|
|
||
| it("disposes the memo after a microtask when there are no listeners", async () => { | ||
| const [count, setCount] = createSignal(0); | ||
| let runs = 0; | ||
| let disposed = false; | ||
| const memo = createRcMemo(() => { | ||
| runs++; | ||
| onCleanup(() => { | ||
| disposed = true; | ||
| }); | ||
| return count(); | ||
| }); | ||
|
|
||
| await createRoot(async dispose => { | ||
| createComputed(() => { | ||
| memo(); | ||
| }); | ||
| expect(runs).toBe(1); | ||
| expect(disposed).toBe(false); | ||
|
|
||
| dispose(); | ||
| expect(disposed).toBe(false); // Not yet disposed, waiting for microtask | ||
|
|
||
| await nextTick(); | ||
| expect(disposed).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| it("keeps the memo alive if a new listener is added within the same microtask", async () => { | ||
| const [count, setCount] = createSignal(0); | ||
| let runs = 0; | ||
| let disposed = false; | ||
| const memo = createRcMemo(() => { | ||
| runs++; | ||
| onCleanup(() => { | ||
| disposed = true; | ||
| }); | ||
| return count(); | ||
| }); | ||
|
|
||
| const dispose1 = createRoot(dispose => { | ||
| createComputed(() => { | ||
| memo(); | ||
| }); | ||
| return dispose; | ||
| }); | ||
|
|
||
| expect(runs).toBe(1); | ||
| dispose1(); | ||
|
|
||
| // Before microtask, add another listener | ||
| createRoot(dispose => { | ||
| createComputed(() => { | ||
| memo(); | ||
| }); | ||
| // Should reuse existing memo | ||
| expect(runs).toBe(1); | ||
| expect(disposed).toBe(false); | ||
| dispose(); | ||
| }); | ||
|
|
||
| await nextTick(); | ||
| expect(disposed).toBe(true); | ||
| }); | ||
|
|
||
| it("supports multiple listeners", async () => { | ||
| const [count, setCount] = createSignal(0); | ||
| let runs = 0; | ||
| let disposed = false; | ||
| const memo = createRcMemo(() => { | ||
| runs++; | ||
| onCleanup(() => { | ||
| disposed = true; | ||
| }); | ||
| return count(); | ||
| }); | ||
|
|
||
| const dispose1 = createRoot(dispose => { | ||
| createComputed(() => { | ||
| memo(); | ||
| }); | ||
| return dispose; | ||
| }); | ||
|
|
||
| const dispose2 = createRoot(dispose => { | ||
| createComputed(() => { | ||
| memo(); | ||
| }); | ||
| return dispose; | ||
| }); | ||
|
|
||
| expect(runs).toBe(1); | ||
|
|
||
| dispose1(); | ||
| await nextTick(); | ||
| expect(disposed).toBe(false); | ||
|
|
||
| dispose2(); | ||
| await nextTick(); | ||
| expect(disposed).toBe(true); | ||
| }); | ||
|
|
||
| it("passes context to the inner memo", async () => { | ||
| const MyContext = createContext(123); | ||
| let capturedContext: any; | ||
|
|
||
| const { memo, dispose } = createRoot(dispose => { | ||
| const memo = createRcMemo(() => { | ||
| capturedContext = useContext(MyContext); | ||
| return 0; | ||
| }); | ||
| return { memo, dispose }; | ||
| }); | ||
|
|
||
| createRoot(dis => { | ||
| createComputed(() => { | ||
| memo(); | ||
| }); | ||
| dis(); | ||
| }); | ||
|
|
||
| expect(capturedContext).toBe(123); | ||
|
|
||
| dispose(); | ||
| }); | ||
|
|
||
| it("persists the last seen value as initial value for the next recreation", async () => { | ||
| const [count, setCount] = createSignal(0); | ||
| let capturedPrev: any; | ||
| const memo = createRcMemo(prev => { | ||
| capturedPrev = prev; | ||
| return count(); | ||
| }); | ||
|
|
||
| // 1st life | ||
| await createRoot(async dispose => { | ||
| createComputed(() => memo()); | ||
| expect(capturedPrev).toBeUndefined(); | ||
| setCount(1); | ||
| expect(capturedPrev).toBe(0); | ||
| dispose(); | ||
| await nextTick(); | ||
| }); | ||
|
|
||
| // 2nd life | ||
| await createRoot(async dispose => { | ||
| createComputed(() => memo()); | ||
| expect(capturedPrev).toBe(1); // Should have persisted the last value | ||
| dispose(); | ||
| await nextTick(); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add that the
prevargument to the memo will be based on the previous value after being without listeners and being re-initialized, even if intermediate computations would have led to a different value. Maybe consider providing an additional stale argument (or similar mechanism) to the handler.