Skip to content

Commit aca9af9

Browse files
authored
feat: remove .expo extension in favor of generic fallback to legacy native module mechanism (#544)
1 parent c7f61aa commit aca9af9

File tree

4 files changed

+49
-28
lines changed

4 files changed

+49
-28
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const {NativeModules} = require('react-native');
2+
3+
export default function shouldFallbackToLegacyNativeModule() {
4+
const expoConstants =
5+
NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants;
6+
7+
if (expoConstants) {
8+
/**
9+
* In SDK <= 39, appOwnership is defined in managed apps but executionEnvironment is not.
10+
* In bare React Native apps using expo-constants, appOwnership is never defined, so
11+
* isLegacySdkVersion will be false in that context.
12+
*/
13+
const isLegacySdkVersion = expoConstants.appOwnership && !expoConstants.executionEnvironment;
14+
15+
/**
16+
* Expo managed apps don't include the @react-native-async-storage/async-storage
17+
* native modules yet, but the API interface is the same, so we can use the version
18+
* exported from React Native still.
19+
*
20+
* If in future releases (eg: @react-native-async-storage/async-storage >= 2.0.0) this
21+
* will likely not be valid anymore, and the package will need to be included in the Expo SDK
22+
* to continue to work.
23+
*/
24+
if (isLegacySdkVersion || ['storeClient', 'standalone'].includes(expoConstants.executionEnvironment)) {
25+
return true;
26+
}
27+
}
28+
29+
return false;
30+
}

website/docs/Installation.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ title: Installation
44
sidebar_label: Installation
55
---
66

7-
8-
97
### Get library
108

119
With npm:
@@ -18,6 +16,11 @@ With Yarn:
1816
yarn add @react-native-async-storage/async-storage
1917
```
2018

19+
With Expo CLI:
20+
```bash
21+
expo install @react-native-async-storage/async-storage
22+
```
23+
2124
### Link
2225

2326
#### Android & iOS

0 commit comments

Comments
 (0)