feat(react): add usePGliteOptional for use outside a provider#953
Open
claygeo wants to merge 1 commit into
Open
feat(react): add usePGliteOptional for use outside a provider#953claygeo wants to merge 1 commit into
claygeo wants to merge 1 commit into
Conversation
Resolves electric-sql#878. Components that render before an async or lazy PGliteProvider mounts currently crash because usePGlite() throws. This adds an opt-in usePGliteOptional() hook that returns null in that case, and makes the string-query form of useLiveQuery / useLiveIncrementalQuery tolerate a not-yet-mounted provider (returning undefined until one appears) instead of throwing. usePGlite() is unchanged: same return type T, same throw-on-missing- provider behavior, so no existing call site or the provider.test-d.tsx type contract breaks.
da87481 to
4f3e3ab
Compare
Contributor
Author
|
Friendly nudge on this one, and a heads-up that I have reworked it since it was first opened. I rebased onto current This resolves #878 (two people hit it). Happy to rename the hook or split the docs change out if you'd prefer. Would appreciate a review when you have a moment. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Calling
usePGlite()without a<PGliteProvider>ancestor throws:When the database is created asynchronously (lazy load, code-split, optional feature), components can render before the provider mounts, and the throw crashes the tree. This is issue #878, with a second report in the thread.
Approach
This revision keeps
usePGlite()exactly as-is instead of changing its return type.usePGlite(): Tstill throws when no provider is mounted, so:const db = usePGlite(); db.query(...)pattern still type-checks,provider.test-d.tsxcontractexpectTypeOf(usePGlite()).toEqualTypeOf<T>()stays green.It adds an opt-in escape hatch instead:
usePGliteOptional(): T | nullreturnsnullrather than throwing when there is no provider.makePGliteProvider()returns it too, so typed providers get a typed optional hook.useLiveQueryImplnow resolves the db withusePGliteOptional(). The string-query branch clears results and skips the subscription (reportingundefined) until a provider mounts, then subscribes automatically sincedbis in the effect deps. TheLiveQueryand promise overloads are unchanged; they never needed the context db.Tests / docs
provider.test.tsx:usePGlite()still throws outside a provider;usePGliteOptional()returnsnull, does not throw, and honors an explicitdbargument.provider.test-d.tsx: addsexpectTypeOf(usePGliteOptional()).toEqualTypeOf<T | null>()plus the wrong-extension@ts-expect-error, alongside the unchangedusePGlite()assertion.usePGliteOptionalsection.@electric-sql/pglite-react.Closes #878.