8000 feat: add signal peek by Shinigami92 · Pull Request #51 · un-ts/unuse · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add signal peek #51

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

Merged
merged 1 commit into from
Jul 1, 2025
Merged

feat: add signal peek #51

merged 1 commit into from
Jul 1, 2025

Conversation

Shinigami92
Copy link
Member
@Shinigami92 Shinigami92 commented Jun 27, 2025

adds the peek function to unSignal and unComputed

taken from #27

this can later be used to retrieve a value without triggering inside an effect

I added @teleskop150750 so he gets credits for it and the next PRs from him can CI-run without approvals


Important

Add peek() method to UnSignal for non-reactive value access, with tests in index.spec.ts.

  • New Features:
    • Added peek() method to UnSignal in index.ts to access current signal value without triggering effects.
  • Tests:
    • Updated index.spec.ts to include tests for peek() method, ensuring non-reactive access pattern.
  • Misc:
    • Minor formatting change in index.ts.

This description was created by Ellipsis for b3c3732. You can customize this summary. It will automatically update as commits are pushed.


Summary by CodeRabbit

  • New Features

    • Introduced a new peek() method for signals, allowing users to access the current signal value without triggering reactive effects.
  • Tests

    • Added tests to verify the behavior of the new peek() method and its non-reactive access pattern for signals.

@Shinigami92 Shinigami92 self-assigned this Jun 27, 2025
@Shinigami92 Shinigami92 added the feature New feature label Jun 27, 2025
Copy link
changeset-bot bot commented Jun 27, 2025

🦋 Changeset detected

Latest commit: d9c6ae5

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

This PR includes changesets to release 6 packages
Name Type
unuse-reactivity Minor
unuse Minor
unuse-angular Minor
unuse-react Minor
unuse-solid Minor
unuse-vue Minor

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
coderabbitai bot commented Jun 27, 2025

Walkthrough

A new feature has been introduced to the UnSignal interface by adding a peek() method, enabling non-reactive access to a signal's value. Associated tests were added to verify this functionality, and a changeset file documents the feature addition. No changes were made to exported entity declarations beyond the new method.

Changes

File(s) Change Summary
.changeset/dark-rats-ask.md Added changeset documenting the new peek feature for signals.
packages/unuse-reactivity/src/unSignal/index.ts Added peek() method to UnSignal interface and implemented it in the unSignal function; updated type guard.
packages/unuse-reactivity/src/unSignal/index.spec.ts Enhanced tests to verify peek() behavior and its non-reactivity; updated mocks to include peek.
packages/unuse-reactivity/src/unComputed/index.ts Added a whitespace line after a property; no functional changes.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Signal as unSignal
    participant Effect as unEffect

    User->>Signal: set(value)
    Signal->>Effect: Notify reactive effects

    User->>Signal: get()
    Signal->>Effect: Register dependency, return value

    User->>Signal: peek()
    Signal-->>User: Return value (no dependency tracking)
Loading

Possibly related PRs

Suggested reviewers

  • Shinigami92

Poem

A signal now can quietly peek,
Its value shown, but silence sleek.
No ripple wakes the reactive sea,
Just a glance—what could it be?
Rabbits cheer with twitching ears,
For sneaky peeks bring subtle cheers!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b3c3732 and d9c6ae5.

📒 Files selected for processing (4)
  • .changeset/dark-rats-ask.md (1 hunks)
  • packages/unuse-reactivity/src/unComputed/index.ts (1 hunks)
  • packages/unuse-reactivity/src/unSignal/index.spec.ts (6 hunks)
  • packages/unuse-reactivity/src/unSignal/index.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • packages/unuse-reactivity/src/unComputed/index.ts
  • .changeset/dark-rats-ask.md
  • packages/unuse-reactivity/src/unSignal/index.spec.ts
  • packages/unuse-reactivity/src/unSignal/index.ts
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor
github-actions bot commented Jun 27, 2025

Coverage Report

Status Category Percentage Covered / Total
🔴 Lines 81.32% (🎯 90%)
⬆️ +0.03%
1232 / 1515
🔴 Statements 81.32% (🎯 90%)
⬆️ +0.03%
1232 / 1515
🔴 Functions 77.66% (🎯 90%)
⬆️ +0.21%
80 / 103
🔴 Branches 83.71% (🎯 85%)
⬆️ +0.13%
329 / 393
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/unuse-reactivity/src/unComputed/index.ts 87.5%
🟰 ±0%
81.25%
🟰 ±0%
100%
🟰 ±0%
87.5%
🟰 ±0%
25-26, 29-30, 35-36
packages/unuse-reactivity/src/unSignal/index.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
Unchanged Files
packages/unuse-angular/src/index.ts 94.2% 100% 83.33% 94.2% 143-146
packages/unuse-react/src/index.ts 69.86% 68.18% 100% 69.86% 49-55, 58-71, 139-143, 152
packages/unuse-reactivity/src/index.ts 0% 0% 0% 0% 1-5
packages/unuse-reactivity/src/unEffect/index.ts 95.65% 66.66% 100% 95.65% 26
packages/unuse-reactivity/src/unEffectScope/index.ts 90.9% 50% 100% 90.9% 24-25
packages/unuse-reactivity/src/unReactiveSystem/index.ts 84.76% 88% 66.66% 84.76% 38-44, 68-69, 80-81, 92-93, 96-97, 100-103, 154-155, 188-189
packages/unuse-reactivity/src/unWatch/index.ts 100% 100% 100% 100%
packages/unuse-solid/src/index.ts 83.09% 86.95% 100% 83.09% 52-59, 69, 141-145
packages/unuse-vue/src/index.ts 100% 100% 100% 100%
packages/unuse/src/index.ts 0% 0% 0% 0% 1-15
packages/unuse/src/_framework/index.ts 81.53% 62.5% 100% 81.53% 48-49, 67-68, 76-77, 85-86, 94-95, 102-103
packages/unuse/src/_testUtils/angular.ts 100% 100% 100% 100%
packages/unuse/src/_testUtils/react.ts 100% 100% 100% 100%
packages/unuse/src/_testUtils/solid.ts 100% 100% 100% 100%
packages/unuse/src/_testUtils/vue.ts 100% 100% 100% 100%
packages/unuse/src/isClient/index.ts 100% 0% 100% 100%
packages/unuse/src/isObject/index.ts 100% 100% 100% 100%
packages/unuse/src/isWorker/index.ts 66.66% 0% 100% 66.66% 5
packages/unuse/src/toArray/index.ts 100% 50% 100% 100%
packages/unuse/src/toUnSignal/index.ts 80.7% 83.33% 66.66% 80.7% 17-18, 31-32, 35-36, 43-44, 81-87, 98-99, 113-120, 130
packages/unuse/src/tryOnScopeDispose/index.ts 77.77% 78.57% 50% 77.77% 11-14, 23-24, 35-36, 40-41
packages/unuse/src/unAccess/index.ts 73% 80% 66.66% 73% 43-44, 48-49, 66-67, 71, 80-81, 84-93, 97-98, 104-105, 114-115, 118-119, 122-123
packages/unuse/src/unRefElement/index.ts 75% 33.33% 100% 75% 29, 36-37
packages/unuse/src/unResolve/index.ts 92.66% 97.14% 71.42% 92.66% 71-72, 93-94, 125-128
packages/unuse/src/useEventListener/index.ts 96.34% 86.66% 100% 96.34% 192-193, 198
packages/unuse/src/useIntervalFn/index.ts 92.85% 80% 100% 92.85% 77-78, 82-83
packages/unuse/src/useToggle/index.ts 100% 100% 100% 100%
packages/unuse/src/useWebSocket/index.ts 46.66% 41.66% 33.33% 46.66% 161-163, 203-212, 216-218, 222-224, 233-235, 244-256, 262-263, 270-275, 278-308, 311-312, 315-326, 330-355, 367-368
Generated in workflow #159 for commit d9c6ae5 by the Vitest Coverage Report Action

@Shinigami92 Shinigami92 requested a review from JounQin June 27, 2025 21:21
@Shinigami92 Shinigami92 force-pushed the feat-add-signal-peek branch 2 times, most recently from b46d3d2 to 8ea8fee Compare June 30, 2025 20:01
@Shinigami92 Shinigami92 marked this pull request as ready for review June 30, 2025 20:02
Copy link
Contributor
@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed everything up to 8ea8fee in 1 minute and 8 seconds. Click for details.
  • Reviewed 133 lines of code in 4 files
  • Skipped 0 files when reviewing.
  • Skipped posting 2 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. packages/unuse-reactivity/src/unSignal/index.ts:107
  • Draft comment:
    Inconsistency: The 'update' method returns the new value but the interface specifies a void return. Adjust the implementation or the type signature.
  • Reason this comment was not posted:
    Comment was not on a location in the diff, so it can't be submitted as a review comment.
2. packages/unuse-reactivity/src/unComputed/index.ts:56
  • Draft comment:
    The PR description mentions adding a 'peek' function to unComputed, but no such method is implemented. Clarify the intent or update the description.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% The comment is asking the PR author to clarify the intent or update the description, which violates the rule against asking the author to update the PR description or confirm their intention. The comment does not provide a specific code suggestion or ask for a test to be written. Therefore, it should be removed.

Workflow ID: wflow_lm0hjizdOZlLmXJL

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

Copy link
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/unuse-reactivity/src/unSignal/index.ts (1)

117-127: Consider updating the type guard to include peek method.

The isUnSignal function checks for get, set, and update methods but doesn't check for the newly added peek method. For complete type safety, consider adding a check for peek as well.

 export function isUnSignal<T>(value: unknown): value is UnSignal<T> {
   return (
     !!value &&
     typeof value === 'object' &&
     UN_SIGNAL in value &&
     value[UN_SIGNAL] === true &&
     typeof (value as UnSignal<T>).get === 'function' &&
+    typeof (value as UnSignal<T>).peek === 'function' &&
     typeof (value as UnSignal<T>).set === 'function' &&
     typeof (value as UnSignal<T>).update === 'function'
   );
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c89b29c and 8ea8fee.

📒 Files selected for processing (4)
  • .changeset/dark-rats-ask.md (1 hunks)
  • packages/unuse-reactivity/src/unComputed/index.ts (1 hunks)
  • packages/unuse-reactivity/src/unSignal/index.spec.ts (4 hunks)
  • packages/unuse-reactivity/src/unSignal/index.ts (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/unuse-reactivity/src/unSignal/index.spec.ts (2)
packages/unuse-reactivity/src/unSignal/index.ts (1)
  • unSignal (63-112)
packages/unuse-reactivity/src/unEffect/index.ts (1)
  • unEffect (15-39)
🔇 Additional comments (8)
packages/unuse-reactivity/src/unComputed/index.ts (1)

57-57: LGTM: Cosmetic formatting improvement.

The whitespace addition improves code formatting consistency.

packages/unuse-reactivity/src/unSignal/index.ts (2)

38-41: LGTM: Well-documented interface addition.

The peek() method is properly documented and clearly explains its non-reactive behavior.


105-105: LGTM: Correct implementation of non-reactive value access.

The implementation correctly returns the state value directly without any reactive tracking or linking, which is exactly the intended behavior for peek().

.changeset/dark-rats-ask.md (1)

1-11: LGTM: Proper changeset documentation.

The changeset correctly documents this as a minor feature addition across all relevant packages with a clear description.

packages/unuse-reactivity/src/unSignal/index.spec.ts (4)

1-3: LGTM: Proper test setup with required imports.

The imports correctly add the necessary testing utilities (vi for mocking and unEffect for reactive testing).


16-16: LGTM: Proper assertions for peek method existence.

The tests correctly verify that the peek method exists and is a function on both parameterized and non-parameterized signals.

Also applies to: 27-27


57-85: LGTM: Comprehensive reactive behavior testing.

The tests properly verify that set and update operations trigger reactive effects, with the spy being called twice (initial effect run + triggered by value change).


87-101: LGTM: Excellent test for non-reactive peek behavior.

This test perfectly demonstrates the key feature of peek() - it returns the current value without triggering reactive effects. The spy being called only once (initial effect run) while set calls don't trigger additional effect runs proves the non-reactive behavior is working correctly.

@Shinigami92 Shinigami92 force-pushed the feat-add-signal-peek branch 2 times, most recently from caeee8f to b3c3732 Compare June 30, 2025 20:19
Copy link
Contributor
@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed caeee8f in 1 minute and 15 seconds. Click for details.
  • Reviewed 141 lines of code in 4 files
  • Skipped 0 files when reviewing.
  • Skipped posting 5 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. .changeset/dark-rats-ask.md:10
  • Draft comment:
    Release notes file is formatted correctly with appropriate version bumps.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
2. packages/unuse-reactivity/src/unComputed/index.ts:56
  • Draft comment:
    An extra blank line is added in the UnComputed interface; no peek is implemented here. Confirm if this change is intentional.
  • Reason this comment was not posted:
    Confidence changes required: 33% <= threshold 50% None
3. packages/unuse-reactivity/src/unSignal/index.spec.ts:87
  • Draft comment:
    Test for 'peek' properly verifies that its call does not trigger reactive effects.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
4. packages/unuse-reactivity/src/unSignal/index.ts:105
  • Draft comment:
    The 'peek' implementation correctly returns the raw state value without triggering effects.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
5. packages/unuse-reactivity/src/unSignal/index.ts:107
  • Draft comment:
    The 'update' method returns a value even though its interface signature implies void; consider aligning the return type for consistency.
  • Reason this comment was not posted:
    Comment was not on a location in the diff, so it can't be submitted as a review comment.

Workflow ID: wflow_JIGWXjyHF1Zny2Gh

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

Copy link
Contributor
@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed b3c3732 in 1 minute and 7 seconds. Click for details.
  • Reviewed 157 lines of code in 4 files
  • Skipped 0 files when reviewing.
  • Skipped posting 3 draft comments. View those below.
  • Modify your setting 8000 s and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. .changeset/dark-rats-ask.md:1
  • Draft comment:
    Changeset file looks good; version bumps and titles are correctly specified.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
2. packages/unuse-reactivity/src/unSignal/index.spec.ts:87
  • Draft comment:
    Good test for peek(): it confirms that using peek() does not trigger reactive effects.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
3. packages/unuse-reactivity/src/unSignal/index.ts:107
  • Draft comment:
    The update() method returns a value despite its interface specifying a void return. Consider removing 'return state.value' for consistency.
  • Reason this comment was not posted:
    Comment was not on a location in the diff, so it can't be submitted as a review comment.

Workflow ID: wflow_mHareIEmlJHYKOg9

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

Co-authored-by: teleskop150750 <62594983+teleskop150750@users.noreply.github.com>
@JounQin JounQin force-pushed the feat-add-signal-peek branch from b3c3732 to d9c6ae5 Compare July 1, 2025 01:56
@Shinigami92 Shinigami92 added this pull request to the merge queue Jul 1, 2025
Merged via the queue into main with commit c753147 Jul 1, 2025
13 checks passed
@Shinigami92 Shinigami92 deleted the feat-add-signal-peek branch July 1, 2025 04:10
@github-actions github-actions bot mentioned this pull request Jul 1, 2025
@teleskop150750
Copy link
Contributor

@Shinigami92

Should we use untrack to avoid breaking the logic of the work?

Like: preact
https://github.com/preactjs/signals/blob/4045d2d86b720546848d5163d5b683792c0a5af3/packages/core/src/index.ts#L371-L379

Signal.prototype.peek = function () {
	const prevContext = evalContext;
	evalContext = undefined;
	try {
		return this.value;
	} finally {
		evalContext = prevContext;
	}
};

Alien-signals untrack

https://github.com/stackblitz/alien-signals/blob/d76bee7f6dc05ec05ccca13111d54382e375cf1a/tests/untrack.spec.ts#L10-L12

	const src = signal(0);

	let computedTriggerTimes = 0;
	const c = computed(() => {
		computedTriggerTimes++;
		const currentSub = setCurrentSub(undefined);
		const value = src();
		setCurrentSub(currentSub);
		return value;
	});

@teleskop150750
Copy link
Contributor
teleskop150750 commented Jul 1, 2025

In fact, I don't know yet what this code affects. If you remove it, all tests pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0