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
Promise support in watch #507
Comments
Thanks for reporting.
Does |
Here's derive using async/await https://codesandbox.io/s/derive-with-promises-yx2fkq?file=/src/App.js and here's subscribe using async/await https://codesandbox.io/s/subscribe-with-promises-7pokmq I think the key difference is that
Whether |
https://codesandbox.io/s/derive-with-promises-yx2fkq?file=/src/App.js https://codesandbox.io/s/subscribe-with-promises-7pokmq
So, option 1 is to leave it as is. Option 2 could be removing I'd be appreciated if you work on docs/wikis. |
I think option 1 is superior to option 2. I've found a very nice use for
The one alternative option is having specific logic to allow async/await for watch and treat an async function as if it were returning void. |
I thought this would be confusing and people might misuse. Here's the diff that could be acceptable: diff --git a/src/utils/watch.ts b/src/utils/watch.ts
index 14fbd63..bc41d30 100644
--- a/src/utils/watch.ts
+++ b/src/utils/watch.ts
@@ -2,7 +2,7 @@ import { subscribe } from '../vanilla'
type Cleanup = () => void
type WatchGet = <T extends object>(proxyObject: T) => T
-type WatchCallback = (get: WatchGet) => Cleanup | void | undefined
+type WatchCallback = (get: WatchGet) => Cleanup | void | Promise<void>
type WatchOptions = {
sync?: boolean
}
@@ -75,7 +75,7 @@ export function watch(
})
// If there's a cleanup, we add this to the cleanups set
- if (cleanupReturn) {
+ if (typeof cleanupReturn === 'function') {
cleanups.add(cleanupReturn)
}
} finally { Note: I dropped Would you like to open a PR with this fix and a test to cover it? |
Just a note here, Vue allows async watch functions. Though it also doesn't utilize any kind of watch "cleanup". There are definitely good use cases. |
Hey I would like to help. this is my first open source contribution :) |
@NeriRos Yes, please go ahead. Note that I'm not yet confident if the outcome is mergeable. |
Implements pmndrs#507
* feat: Support promise in watch util Implements #507 * chore: reverted return type of callback in subscribe, removed mem optimization * chore: useFakeTimers * fix: return type + sleep * chore: code order --------- Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
https://codesandbox.io/s/breaking-watch-ct246n
If you attempt to use async/await in watch it'll break the cleanup cycle. I added a quick test locally and fooled around with it and can avoid the issue with something along these lines:
However, I'm not sure that's the desired behavior either as that would also mean you can't run a cleanup function using async/await without additional logic, but maybe that's ok.
Derive handles this scenario already of working with promises, so it feels a little odd that watch is the odd function out when it comes to promises. I suppose one workaround is doing a manual subscribe dance around the promises and their subscriptions.
The text was updated successfully, but these errors were encountered: