Fix React Native runtime visibility packaging#4683
Open
KyleAMathews wants to merge 1 commit into
Open
Conversation
commit: |
✅ Deploy Preview for electric-next ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4683 +/- ##
===========================================
+ Coverage 60.03% 73.63% +13.60%
===========================================
Files 395 88 -307
Lines 43763 10088 -33675
Branches 12587 3102 -9485
===========================================
- Hits 26271 7428 -18843
+ Misses 17411 2603 -14808
+ Partials 81 57 -24
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Fixes React Native/Expo app lifecycle handling for ShapeStream by replacing brittle runtime
require('react-native')probing with a Metro/Exporeact-nativepackage export. React Native builds now wireAppStateautomatically, whileruntimeVisibilityremains available as an explicit escape hatch for custom bundlers/runtimes.Root cause
The previous React Native visibility detection depended on runtime access to
require('react-native')viaglobalThis.requireorFunction('return require'). In Metro/Hermes builds,requireis available at module scope but not necessarily onglobalThis, and the function fallback can returnundefined. When that happened,runtimeVisibilitystayed unset and the AppState listener was never installed.Approach
ShapeStream.src/react-native.ts) that statically importsAppStatefromreact-nativeand registers the default runtime visibility adapter.react-nativeconditional package export, plus a legacy top-levelreact-nativefield, so Metro/Expo native builds resolve the RN entrypoint from standard imports:runtimeVisibilityas the explicit override/fallback for custom runtimes or Metro configs that do not resolve thereact-nativecondition.package.jsonso the AppState registration is not tree-shaken while preserving tree-shaking for the rest of the package.Key invariants
react-native.runtimeVisibilitystill wins over the default adapter.Non-goals
react-nativeas a runtime dependency of@electric-sql/client.requireprobing or Metro internals.runtimeVisibilityAPI; it remains the escape hatch.ShapeStreamwrapper in this PR.Trade-offs
The RN entrypoint uses top-level registration through a small shared factory. This keeps the main
ShapeStreamimplementation simple and avoids duplicating the class for React Native, but it means the RN entrypoint must be marked as side-effectful. ThesideEffectsallowlist is limited to the RN entry files so other package entrypoints remain tree-shakable.Verification
I also ran the targeted wake-detection unit test with a temporary Vitest config that skips the integration global setup; all 15 tests in
test/wake-detection.test.tspassed. The normal package test command attempted to run global integration setup and timed out waiting for Electric to be active in this environment.Files changed
.changeset/fresh-ravens-foreground.md— patch changeset for@electric-sql/client.packages/typescript-client/package.json— adds RN conditional export, top-level RN field, CJS RN target, and sideEffects allowlist for RN entrypoints.packages/typescript-client/tsup.config.ts— builds the RN entrypoint and externalizesreact-native.packages/typescript-client/src/client.ts— removes runtime RN probing and reads the optional default runtime visibility factory.packages/typescript-client/src/react-native.ts— RN entrypoint that wires AppState lifecycle handling and re-exports the normal client API.packages/typescript-client/src/runtime-visibility.ts— shared default runtime visibility factory registration.packages/typescript-client/src/react-native-shim.d.ts— minimal local typing for the RN entrypoint without adding an RN dependency.packages/typescript-client/test/wake-detection.test.ts— updates the RN test to exercise the RN entrypoint instead of global require detection.packages/typescript-client/SPEC.md— updates the pause-lock notes to describe RN handling through package exports.packages/typescript-client/README.md,website/docs/sync/api/clients/typescript.md,website/docs/sync/integrations/expo.md— document automatic Metro/Expo handling and explicitruntimeVisibilityfallback.