Skip to content

Commit 7865e48

Browse files
committed
chore: remove .expo extension in favor of generic fallback to legacy native module mechanism
1 parent 078d228 commit 7865e48

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

src/RCTAsyncStorage.expo.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/RCTAsyncStorage.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
const {NativeModules} = require('react-native');
1+
const {NativeModules, TurboModuleRegistry} = require('react-native');
2+
const shouldFallbackToLegacyNativeModule = require('./shouldFallbackToLegacyNativeModule');
23

3-
const RCTAsyncStorage =
4+
let RCTAsyncStorage =
45
NativeModules.PlatformLocalStorage || // Support for external modules, like react-native-windows
56
NativeModules.RNC_AsyncSQLiteDBStorage ||
67
NativeModules.RNCAsyncStorage;
78

9+
if (!RCTAsyncStorage && shouldFallbackToLegacyNativeModule()) {
10+
// TurboModuleRegistry falls back to NativeModules so we don't have to try go
11+
// assign NativeModules' counterparts if TurboModuleRegistry would resolve
12+
// with undefined.
13+
if (TurboModuleRegistry) {
14+
RCTAsyncStorage = TurboModuleRegistry.get('AsyncSQLiteDBStorage') || TurboModuleRegistry.get('AsyncLocalStorage');
15+
} else {
16+
RCTAsyncStorage = NativeModules.AsyncSQLiteDBStorage || NativeModules.AsyncLocalStorage;
17+
}
18+
}
19+
820
export default RCTAsyncStorage;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const {NativeModules} = require('react-native');
2+
3+
export default function shouldFallbackToLegacyNativeModule() {
4+
const expoExecutionEnvironment = NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants?.executionEnvironment;
5+
6+
/**
7+
* Expo managed apps don't include the @react-native-async-storage/async-storage
8+
* native modules yet, but the API interface is the same, so we can use the version
9+
* exported from React Native still.
10+
*
11+
* If in future releases (eg: @react-native-async-storage/async-storage >= 2.0.0) this
12+
* will likely not be valid anymore, and the package will need to be included in the Expo SDK
13+
* to continue to work.
14+
*/
15+
if (expoExecutionEnvironment && ['storeClient', 'standalone'].includes(expoExecutionEnvironment)) {
16+
return true;
17+
}
18+
19+
return false;
20+
}

0 commit comments

Comments
 (0)