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

Implement effect cleanups #183

Merged
merged 10 commits into from
Sep 19, 2022
Merged

Implement effect cleanups #183

merged 10 commits into from
Sep 19, 2022

Conversation

jviide
Copy link
Contributor

@jviide jviide commented Sep 19, 2022

This pull request implements effect cleanup functions. An effect callback can return a function that's then run the next time the effect either gets rerun or disposed. This pull request also converts the useSignalEffect hook for the Preact and React integrations to use effect cleanups.

The effect(compute: () => void) signature is modified to effect(compute: () => unknown):

const s = signal(0);

const dispose = effect(() => {
  if (s.value === 0) {
    effect(() => () => console.log("hello"));
  } else {
    effect(() => () => console.log("what a"));
  }

  return () => console.log("world");
});

s.value++; // Prints "hello" & "world"
dispose(); // Prints "what a" & "world"

Apart from testing basic functionality, the tests try to cover several corner cases and specific behaviors:

  • The cleanup functions of nested effects are run before the parent effect's cleanup.
  • When an effect or computed signal is running its nested effect cleanups, and one of the cleanups throws, the rest of the cleanups are still completed and the error is only then thrown upwards.
  • If one of effect's nested cleanups throws the effect's own cleanup function still gets called.
  • If an effect's own cleanup or one of its nested cleanups throw, the effect becomes essentially disposed. The error is thrown up the stack.
  • If a computed signal's nested effects fail when the computed value is being revalidated, all of the computed signal's dependency links are severed. Accessing the computed's value from thereon throws the error.
  • If an effect throws an error then its cleanup function is assumed to be undefined.
  • Effects can be disposed and retriggered during their own cleanups or nested cleanups.

@changeset-bot
Copy link

changeset-bot bot commented Sep 19, 2022

🦋 Changeset detected

Latest commit: 79ff1e7

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

This PR includes changesets to release 3 packages
Name Type
@preact/signals-core Minor
@preact/signals Minor
@preact/signals-react 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

@netlify
Copy link

netlify bot commented Sep 19, 2022

Deploy Preview for preact-signals-demo ready!

Name Link
🔨 Latest commit 79ff1e7
🔍 Latest deploy log https://app.netlify.com/sites/preact-signals-demo/deploys/63289d9c7d810800080bda92
😎 Deploy Preview https://deploy-preview-183--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

github-actions bot commented Sep 19, 2022

Size Change: +454 B (+1%)

Total Size: 67.5 kB

Filename Size Change
docs/dist/assets/client.********.js 45.8 kB +1 B (0%)
docs/dist/assets/index.********.js 1.08 kB +244 B (+29%) 🚨
docs/dist/assets/signals-core.module.********.js 1.41 kB +96 B (+7%) 🔍
docs/dist/assets/signals.module.********.js 1.92 kB +1 B (0%)
docs/dist/basic-********.js 246 B +2 B (+1%)
docs/dist/demos-********.js 3.35 kB +4 B (0%)
docs/dist/nesting-********.js 1.13 kB +1 B (0%)
docs/dist/react-********.js 240 B +3 B (+1%)
packages/core/dist/signals-core.js 1.46 kB +104 B (+8%) 🔍
packages/core/dist/signals-core.mjs 1.48 kB +102 B (+7%) 🔍
packages/preact/dist/signals.js 1.13 kB -21 B (-2%)
packages/preact/dist/signals.mjs 1.11 kB -26 B (-2%)
packages/react/dist/signals.js 833 B -27 B (-3%)
packages/react/dist/signals.mjs 810 B -30 B (-4%)
ℹ️ View Unchanged
Filename Size
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

cleanup = result
}
})
return callback.current();
Copy link
Member

Choose a reason for hiding this comment

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

I like that useSignalEffect got a lot simpler!

Copy link
Member

@marvinhagemeister marvinhagemeister left a comment

Choose a reason for hiding this comment

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

This is awesome!

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