8000 refactor(reactivity): unSignal by teleskop150750 · Pull Request #64 · un-ts/unuse · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor(reactivity): unSignal #64

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

teleskop150750
Copy link
Contributor
@teleskop150750 teleskop150750 commented Jul 4, 2025

Important

Refactor unSignal function in index.ts by inlining setter logic into set method and updating peek to prevent side effects.

  • Refactor unSignal function in index.ts:
    • Inline setter function logic directly into set method, removing separate setter function.
    • Update peek method to use setCurrentSub to prevent side effects during get call.

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

Copy link
changeset-bot bot commented Jul 4, 2025

⚠️ No Changeset found

Latest commit: fd3e2e4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Copy link
Contributor
github-actions bot commented Jul 4, 2025

Coverage Report

Status Category Percentage Covered / Total
🔴 Lines 81.36% (🎯 90%)
⬆️ +0.04%
1236 / 1519
🔴 Statements 81.36% (🎯 90%)
⬆️ +0.04%
1236 / 1519
🔴 Functions 77.66% (🎯 90%)
🟰 ±0%
80 / 103
🔴 Branches 83.71% (🎯 85%)
🟰 ±0%
329 / 393
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
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/unComputed/index.ts 87.5% 81.25% 100% 87.5% 25-26, 29-30, 35-36
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 #165 for commit fd3e2e4 by the Vitest Coverage Report Action

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.

Caution

Changes requested ❌

Reviewed everything up to fd3e2e4 in 1 minute and 33 seconds. Click for details.
  • Reviewed 67 lines of code in 1 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:93
  • Draft comment:
    The peek method now temporarily disables subscription tracking and calls this.get(), which can trigger updates (e.g. through updateSignal and propagation) instead of simply returning state.value. Verify that this behavioral change was intended.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 30% <= threshold 50% The comment is asking the PR author to verify if a behavioral change was intended, which violates the rule against asking for confirmation of intention. However, it does provide a specific observation about the change in behavior, which could be useful for the author to consider. Despite this, the primary focus is on verification, which is not allowed.
2. packages/unuse-reactivity/src/unSignal/index.ts:75
  • Draft comment:
    Changing from arrow functions to method shorthand alters the binding of 'this'. Ensure that these methods (get, peek, set, update) are always invoked as object methods; otherwise, if they are destructured, 'this' might be undefined.
  • Reason this comment was not posted:
    Comment looked like it was already resolved.

Workflow ID: wflow_tggEOOaqvdJ1CrX5

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

}
},
update(updater) {
this.set(updater(state.value));
return state.value;
Copy link
Contributor

Choose a reason for hiding this comment

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

The update method now returns state.value, which deviates from the interface (expected to return void). Confirm if returning the new value is the desired behavior.

Suggested change
return state.value;

peek() {
const prev = setCurrentSub(undefined);
const val = this.get();
setCurrentSub(prev);
Copy link
Member

Choose a reason for hiding this comment

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

How is this different with previous solution?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How is this different with previous solution?

#51 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

Doesn't state.value achieve the same goal?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://github.com/stackblitz/alien-signals/blob/d76bee7f6dc05ec05ccca13111d54382e375cf1a/src/index.ts#L294-L301

Yes, because this code does not run. I can't say how critical it is)) It takes time to understand the algorithm more deeply

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
coderabbitai bot commented Jul 4, 2025

Warning

Rate limit exceeded

@teleskop150750 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 7 minutes and 23 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between f692fc7 and fd3e2e4.

📒 Files selected for processing (1)
  • packages/unuse-reactivity/src/unSignal/index.ts (3 hunks)
✨ 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.

}
},
update(updater) {
this.set(updater(state.value));
return state.value;
Copy link
Member
@JounQin JounQin Jul 4, 2025

Choose a reason for hiding this comment

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

It you change the peek implementation, update should be changed at the same time.

Suggested change
return state.value;
return this.peek();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why do we need changes here?

Copy link
Member

Choose a reason for hiding this comment

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

If state.value is always source of truth, why peek() needs change?

}
},
update(updater) {
this.set(updater(state.value));
Copy link
Member

Choose a reason for hiding this comment

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

Should we also change this line?

Suggested change
this.set(updater(state.value));
this.set(updater(this.peek()));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand your logic. For some reason it seems to me that the insert operation can be done without it. ))

My motivation was to save the source code to get the value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

logic for computed and signal has become uniform

Copy link
Member

Choose a reason for hiding this comment

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

We don't have to uniform them if they don't need to, and we should be consistent in unSignal itself.

setter(updater(state.value));
peek() {
const prev = setCurrentSub(undefined);
const val = this.get();
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: I'm not a fan of this, that's why I also declare function variables inside the function body first and then "export/expose" in the return statement what's needed.

Instead we could use the code-style like in every other composable like this:

Details
export function unSignal<T>(initialValue?: T): UnSignal<T> {
  const state: UnSignalState<T> = {
    previousValue: initialValue as T,
    value: initialValue as T,
    subs: undefined,
    subsTail: undefined,
    flags: 1 satisfies ReactiveFlags.Mutable,
  };

  const get: UnSignal<T>['get'] = () => {
    const value = state.value;
    if (
      state.flags & (16 satisfies ReactiveFlags.Dirty) &&
      updateSignal(state, value)
    ) {
      const subs = state.subs;
      if (subs !== undefined) {
        shallowPropagate(subs);
      }
    }

    if (REACTIVITY_STATE.activeSub !== undefined) {
      link(state, REACTIVITY_STATE.activeSub);
    }

    return value;
  };

  const peek: UnSignal<T>['peek'] = () => {
    const prev = setCurrentSub(undefined);
    const val = get();
    setCurrentSub(prev);
    return val;
  };

  const set: UnSignal<T>['set'] = (newValue) => {
    if (state.value !== (state.value = newValue)) {
      state.flags = 17 as ReactiveFlags.Mutable | ReactiveFlags.Dirty;
      const subs = state.subs;
      if (subs !== undefined) {
        propagate(subs);
        if (!REACTIVITY_STATE.batchDepth) {
          flush();
        }
      }
    }
  };

  const update: UnSignal<T>['update'] = (updater) => {
    set(updater(state.value));
    return state.value;
  };

  return {
    [UN_SIGNAL]: true,
    get,
    peek,
    set,
    update,
  };
}

This resolves at the same time my other complain about code-style.

Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer TypeScript type inferring instead of declaring types everywhere explicitly, too verbose to write. 😂

Copy link
Member

Choose a reason for hiding this comment

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

This is a special kind of composable structure, and the typedefs are there to bind them strictly to the exposing results. Otherwise they are function instead of const fnVar

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.

3 participants
0