Skip to content

Added createRcMemo primitive#853

Open
clinuxrulz wants to merge 4 commits intosolidjs-community:mainfrom
clinuxrulz:create-rc-memo
Open

Added createRcMemo primitive#853
clinuxrulz wants to merge 4 commits intosolidjs-community:mainfrom
clinuxrulz:create-rc-memo

Conversation

@clinuxrulz
Copy link
Copy Markdown

createRcMemo is similar to createLazyMemo except its lifetime is bounded by the existence of listeners. (the listeners are reference counted.)

  • Initially when there are no listeners, the internal memo is not created.
  • When it goes from 0 listeners, to some listeners the internal memo is constructed.
  • When it goes from some listeners, to zero listeners the internal memo is deconstructed.
  • The internal memo can be reconstructed again after it has been deconstructed.
  • For the context being passed along, it will carry the context of the owner of which createRcMemo was called inside.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 30, 2026

🦋 Changeset detected

Latest commit: 0ebdd1a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@solid-primitives/memo Minor
@solid-primitives/date Patch
@solid-primitives/deep Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Copy Markdown
Member

@atk atk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no documentation in the README.md, and this also needs a changeset file (please run npx changeset and add a minor version). Lastly, add createRcMemo to package.json in the primitive.list array.

/**
* Reference counted `createMemo`. The memo calculation will only run when there is at least one listener.
*
* Once the number of listeners drops to zero, the internal memo will be disposed after a microtask.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add that the prev argument 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.

Comment thread packages/memo/src/index.ts
Comment thread packages/memo/src/index.ts Outdated
let existing2 = existing!;
onCleanup(() => {
existing2.refCount--;
if (existing2.refCount == 0) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use type-safe comparisons unless you consciously want to match other types, e.g. in line 485.

@clinuxrulz
Copy link
Copy Markdown
Author

clinuxrulz commented May 1, 2026

@atk I believe I have address your requested changes.
Except for npx changeset, that command did not make any file changes on my machine, so I am a little confused.

Maybe you can try the npx changeset command on your end?

Regarding prev to clear things up. When an internal memo is disposed later on, and reconstructed again later later on, the prev value will not reset to the initial value provided to createRcMemo, it will instead carry on the last-seen prev value. If it seems unclear in the documentation still, I can try and clear it up.

I've used that createRcMemo pattern a lot in production code at work. I believe it's a handy one to have available.

@clinuxrulz
Copy link
Copy Markdown
Author

@atk changeset added. Apologies for before, I did not understand how the npx changeset worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants