-
-
Notifications
You must be signed in to change notification settings - Fork 109
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
Conversation
🦋 Changeset detectedLatest commit: 5c388a1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
✅ Deploy Preview for preact-signals-demo ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Size Change: +37 B (+0.04%) Total Size: 87.6 kB
ℹ️ View Unchanged
|
3d69e8b
to
62ca0fa
Compare
a.value = "aaa"; | ||
expect(spy).to.be.calledOnce; | ||
}); | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this 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.
Effect.prototype.dispose = function () { | ||
this._dispose(); | ||
}; |
There was a problem hiding this comment.
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();
}
});
}); | ||
const dispose = effect(function () { | ||
spy(); | ||
this.dispose(); |
There was a problem hiding this comment.
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()
This enables us to access
this.dispose()
within the callback of an effect. We needed to do some typing shenanigans to allow for an unspecifiedthis
like we need in our adapter packages so we can override_start
and others.Resolves #395