-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove .expo extension in favor of generic fallback to legacy n…
…ative module mechanism (#544)
- Loading branch information
1 parent
c7f61aa
commit aca9af9
Showing
4 changed files
with
49 additions
and
28 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
const {NativeModules} = require('react-native'); | ||
const {NativeModules, TurboModuleRegistry} = require('react-native'); | ||
const shouldFallbackToLegacyNativeModule = require('./shouldFallbackToLegacyNativeModule'); | ||
|
||
const RCTAsyncStorage = | ||
let RCTAsyncStorage = | ||
NativeModules.PlatformLocalStorage || // Support for external modules, like react-native-windows | ||
NativeModules.RNC_AsyncSQLiteDBStorage || | ||
NativeModules.RNCAsyncStorage; | ||
|
||
if (!RCTAsyncStorage && shouldFallbackToLegacyNativeModule()) { | ||
// TurboModuleRegistry falls back to NativeModules so we don't have to try go | ||
// assign NativeModules' counterparts if TurboModuleRegistry would resolve | ||
// with undefined. | ||
if (TurboModuleRegistry) { | ||
RCTAsyncStorage = TurboModuleRegistry.get('AsyncSQLiteDBStorage') || TurboModuleRegistry.get('AsyncLocalStorage'); | ||
} else { | ||
RCTAsyncStorage = NativeModules.AsyncSQLiteDBStorage || NativeModules.AsyncLocalStorage; | ||
} | ||
} | ||
|
||
export default RCTAsyncStorage; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const {NativeModules} = require('react-native'); | ||
|
||
export default function shouldFallbackToLegacyNativeModule() { | ||
const expoConstants = | ||
NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants; | ||
|
||
if (expoConstants) { | ||
/** | ||
* In SDK <= 39, appOwnership is defined in managed apps but executionEnvironment is not. | ||
* In bare React Native apps using expo-constants, appOwnership is never defined, so | ||
* isLegacySdkVersion will be false in that context. | ||
*/ | ||
const isLegacySdkVersion = expoConstants.appOwnership && !expoConstants.executionEnvironment; | ||
|
||
/** | ||
* Expo managed apps don't include the @react-native-async-storage/async-storage | ||
* native modules yet, but the API interface is the same, so we can use the version | ||
* exported from React Native still. | ||
* | ||
* If in future releases (eg: @react-native-async-storage/async-storage >= 2.0.0) this | ||
* will likely not be valid anymore, and the package will need to be included in the Expo SDK | ||
* to continue to work. | ||
*/ | ||
if (isLegacySdkVersion || ['storeClient', 'standalone'].includes(expoConstants.executionEnvironment)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} |
This file contains 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