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

Proposal: Implement Vue-style effect cleanups #190

Closed
wants to merge 2 commits into from
Closed

Conversation

jviide
Copy link
Contributor

@jviide jviide commented Sep 19, 2022

This pull request suggest the following middle point between having and not havig autodisposed effects. It also modifies effect cleanups introduced in #183, allowing scheduling multiple effect cleanups during the same effect run:

import { signal, effect } from "@preact/signals-core";

effect((cleanup) => {
  cleanup(() => {
    console.log("this will run the next time the effect rerun or disposed");
  });
  cleanup(() => {
    console.log("ditto");
  });
});

Effects aren't parented to their contexts by default anymore, and there is no autodisposal. It's pretty easy to achieve a similar effect:

import { signal, effect } from "@preact/signals-core";

effect((cleanup) => {
  cleanup(effect(() => {
    console.log("this effect will be autodisposed, sort of");
  }));
});

This version avoids autodisposal's "spooky action by distance" (accidentally binding an effect's lifetime to another one) by making the disposals explicit, while getting most of autodisposal's benefits. It also requires a bit less code 🙂

@changeset-bot
Copy link

changeset-bot bot commented Sep 19, 2022

⚠️ No Changeset found

Latest commit: b1f1ba9

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

@netlify
Copy link

netlify bot commented Sep 19, 2022

Deploy Preview for preact-signals-demo ready!

Name Link
🔨 Latest commit b1f1ba9
🔍 Latest deploy log https://app.netlify.com/sites/preact-signals-demo/deploys/6328fdd8a2ed210009f121b6
😎 Deploy Preview https://deploy-preview-190--preact-signals-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions
Copy link
Contributor

Size Change: +257 B (0%)

Total Size: 67.7 kB

Filename Size Change
docs/dist/assets/index.********.js 1.08 kB +247 B (+30%) 🚨
docs/dist/assets/signals-core.module.********.js 1.39 kB -13 B (-1%)
docs/dist/assets/signals.module.********.js 1.96 kB +1 B (0%)
docs/dist/basic-********.js 245 B +1 B (0%)
docs/dist/demos-********.js 3.35 kB +3 B (0%)
docs/dist/nesting-********.js 1.13 kB -1 B (0%)
docs/dist/react-********.js 239 B -1 B (0%)
packages/core/dist/signals-core.js 1.44 kB -18 B (-1%)
packages/core/dist/signals-core.mjs 1.46 kB -22 B (-1%)
packages/preact/dist/signals.js 1.2 kB +13 B (+1%)
packages/preact/dist/signals.mjs 1.17 kB +14 B (+1%)
packages/react/dist/signals.js 847 B +14 B (+2%)
packages/react/dist/signals.mjs 829 B +19 B (+2%)
ℹ️ View Unchanged
Filename Size
docs/dist/assets/client.********.js 45.8 kB
docs/dist/assets/jsxRuntime.module.********.js 282 B
docs/dist/assets/preact.module.********.js 4 kB
docs/dist/assets/style.********.js 21 B
docs/dist/assets/style.********.css 1.21 kB

compressed-size-action

@jviide jviide changed the title Proposal: Implement @vue/reactivity style effect cleanups Proposal: Implement Vue-style effect cleanups Sep 19, 2022
@jviide
Copy link
Contributor Author

jviide commented Sep 20, 2022

Based on Slack discussion we won't be going with this approach for now. Superceded by #191.

@jviide jviide closed this Sep 20, 2022
@jviide jviide deleted the cleanup-redux branch September 20, 2022 20:13
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.

1 participant