-
Notifications
You must be signed in to change notification settings - Fork 70
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
feat: rework realtime subscription hooks #35
Conversation
To view this pull requests documentation preview, visit the following URL: docs.page/invertase/react-query-firebase~35 Documentation is deployed and generated using docs.page. |
Hello, is there anything we can help to push this PR over the line? |
Happy to help as well, this fix is crucial for an implementation in prod :) |
I'm also stuck and can't go to production without this fix. I've had great hopes for this lib as it was used by Google showcasing Firebase 9, sadly it seems the repo is abandoned, I'll start looking for alternatives. |
Has anyone else tested this? It looks like the owner says it should work but is concerned about edge cases. |
I'm going to try and get some resource from the company assigned to this soon, apologies it's been really busy. |
This can't be stated enough but the community greatly appreciates your work! |
(just an update) Currently working on this, experiencing some strange behaviour with the hooks in a test, |
OK actually made some progress on this, curious what people think. I'm using a library called import { QueryKey } from "react-query";
import { Auth, IdTokenResult, AuthError } from "firebase/auth";
import { Observable } from "rxjs";
import {
useSubscription,
UseSubscriptionOptions,
} from "react-query-subscription";
import { UseSubscriptionResult } from "react-query-subscription/types/use-subscription";
export function idTokenFromAuth(
auth: Auth,
options?: {
forceRefresh?: boolean;
}
): Observable<IdTokenResult | null> {
return new Observable<IdTokenResult | null>(function subscribe(subscriber) {
const unsubscribe = auth.onIdTokenChanged(async (user) => {
let token: IdTokenResult | null = null;
if (user) {
token = await user.getIdTokenResult(options?.forceRefresh);
}
subscriber.next(token);
});
subscriber.add(unsubscribe);
});
}
export function useAuthIdToken<R = IdTokenResult | null>(
key: QueryKey,
auth: Auth,
options?: {
forceRefresh?: boolean;
},
useSubscriptionOptions?: Omit<
UseSubscriptionOptions<IdTokenResult | null, AuthError, R>,
"queryFn"
>
): UseSubscriptionResult<R | AuthError> {
return useSubscription(key, () => idTokenFromAuth(auth, options), {
...useSubscriptionOptions,
});
} |
tests still need fixing etc, and will rewrite the rest of the subscriptions like this. |
Is adding |
This is a fair point to be honest, it just seemed like they handled subscriptions quite nicely. @Ehesp What do you think? |
Yeah |
@cabljac great! let us know when we can jump in and test! :) |
hm seems a bit trickier to implement than initially hoped. |
Even replicating the parts you need from |
Made some headway! essentially tweaking @Ehesp's solution a bit, but taking some ideas from |
Codecov Report
@@ Coverage Diff @@
## main #35 +/- ##
==========================================
- Coverage 62.57% 59.73% -2.84%
==========================================
Files 12 22 +10
Lines 489 457 -32
Branches 60 44 -16
==========================================
- Hits 306 273 -33
- Misses 183 184 +1
Continue to review full report at Codecov.
|
I think I've refactored all the subscription hooks now:
If anyone notices there's one missing in this list let me know :). I've also added tests for the issue, which seem to pass. I have a feeling there's also some work on the error handling to do, and possibly some tests there. ALSO so i don't forget later, two points that i'll put in separate issues:
|
@cabljac amazing work, all seems to be in order. Should we as part of this PR bump the react dependencies to 18? I've used it with react 18 for some time now and haven't faced any issues |
Co-authored-by: 김윤섭 <[email protected]>
Co-authored-by: 김윤섭 <[email protected]>
OK so last bit of this is to add at least a single test for the error case, but that requires setting up firestore.rules and changing test setup a bit. |
a couple of tests pass on their own, but fail when run in series, and I can't figure out why. I skipped them and merged this. If people get a chance and are able to test it out, that would be greatly appreciated :D |
Fixes #25
This PR reworks hooks that depend on realtime subscriptions. Currently, if a hook declares a realtime subscription (via the
subscribe: true
option or by default (e.g. auth hooks)), astaleTime
ofInfinity
is being set by default. If other hooks with the sameQueryKey
are created, the existing subscription will be used (which is expected).The issue is, "Infinity" data is never considered stale for the remainder of the applications lifecycle. So assuming there is always an active subscription then this doesn't cause any issues (since the subscription always updates the cache). However, if all active hooks are unmounted, and remounted, the actual
queryFn
won't be executed again (since it'll just return the data within the query cache). However since all hooks have actually unmounted, there is no active subscription thus the data will never be updated again.This PR basically reworks the effected hooks by;
### TODOs
utils
package (since it causes weird building if it's directly import via rollup).