Skip to content

Publicly allow dispose() on an effect callback #697

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 4 commits into from
Jun 11, 2025

Conversation

JoviDeCroock
Copy link
Member

This enables us to access this.dispose() within the callback of an effect. We needed to do some typing shenanigans to allow for an unspecified this like we need in our adapter packages so we can override _start and others.

Resolves #395

Copy link

changeset-bot bot commented Jun 11, 2025

🦋 Changeset detected

Latest commit: 5c388a1

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

This PR includes changesets to release 1 package
Name Type
@preact/signals-core 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

netlify bot commented Jun 11, 2025

Deploy Preview for preact-signals-demo ready!

Name Link
🔨 Latest commit 5c388a1
🔍 Latest deploy log https://app.netlify.com/projects/preact-signals-demo/deploys/684974266511c70007ed257f
😎 Deploy Preview https://deploy-preview-697--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 project configuration.

Copy link
Contributor

github-actions bot commented Jun 11, 2025

Size Change: +37 B (+0.04%)

Total Size: 87.6 kB

Filename Size Change
docs/dist/assets/index.********.js 1.09 kB +1 B (+0.09%)
docs/dist/assets/signals-core.module.********.js 1.51 kB +9 B (+0.6%)
docs/dist/assets/signals.module.********.js 2.04 kB +2 B (+0.1%)
docs/dist/demos-********.js 4.32 kB +1 B (+0.02%)
docs/dist/react-********.js 241 B +2 B (+0.84%)
packages/core/dist/signals-core.js 1.54 kB +11 B (+0.72%)
packages/core/dist/signals-core.mjs 1.56 kB +11 B (+0.71%)
ℹ️ View Unchanged
Filename Size
docs/dist/assets/bench.********.js 1.59 kB
docs/dist/assets/client.********.js 46.2 kB
docs/dist/assets/jsxRuntime.module.********.js 283 B
docs/dist/assets/preact.module.********.js 4.01 kB
docs/dist/assets/style.********.js 21 B
docs/dist/assets/style.********.css 1.24 kB
docs/dist/basic-********.js 243 B
docs/dist/nesting-********.js 1.13 kB
packages/preact/dist/signals.js 1.57 kB
packages/preact/dist/signals.mjs 1.53 kB
packages/react-transform/dist/signals-*********.js 5.97 kB
packages/react-transform/dist/signals-transform.mjs 5.13 kB
packages/react-transform/dist/signals-transform.umd.js 6.08 kB
packages/react/dist/signals.js 188 B
packages/react/dist/signals.mjs 150 B

compressed-size-action

@JoviDeCroock JoviDeCroock force-pushed the allow-dispose-publicly branch from 3d69e8b to 62ca0fa Compare June 11, 2025 07:53
a.value = "aaa";
expect(spy).to.be.calledOnce;
});

Copy link
Member

Choose a reason for hiding this comment

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

Nit: Let's add a test where dispose() is called from inside the callback and after that the returned dispose function is called. Just to ensure that we're not erroring when it's called twice

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.

LGTM, I'm not entirely sure about the use case but it seems like some folks need this. We should document that this means that the effect is essentially "dead" after calling dispose() from inside the callback in our docs.

Comment on lines +845 to +847
Effect.prototype.dispose = function () {
this._dispose();
};

Choose a reason for hiding this comment

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

This would be great 🙏 I've got a couple of places in @dnd-kit where I need to set up temporary effects and tear them down from within the effect after a condition is met, for example:

effect(() => {
  if (manager.dragOperation.status.value === 'dropped') {
    this.dispose();
    performDropAnimation();
  }
});

Right now the only way to safely do this is:

const dispose = effect(() => {
  if (manager.dragOperation.status.value === 'dropped') {
    queueMicrotask(() => dispose());
    performDropAnimation();
  }
});

@JoviDeCroock JoviDeCroock merged commit 5e7a167 into main Jun 11, 2025
6 checks passed
@JoviDeCroock JoviDeCroock deleted the allow-dispose-publicly branch June 11, 2025 12:25
});
const dispose = effect(function () {
spy();
this.dispose();
Copy link
Member

Choose a reason for hiding this comment

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

Super tiny nit: Maybe also a test where you call spy() just after dispose()

@github-actions github-actions bot mentioned this pull request Jun 11, 2025
@JoviDeCroock JoviDeCroock mentioned this pull request Jun 11, 2025
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.

Allow effect code to end the effect?
3 participants