Skip to content

chore: remove .expo extension in favor of generic fallback to legacy native module mechanism #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions src/RCTAsyncStorage.expo.js

This file was deleted.

16 changes: 14 additions & 2 deletions src/RCTAsyncStorage.js
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;
30 changes: 30 additions & 0 deletions src/shouldFallbackToLegacyNativeModule.js
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;
}
7 changes: 5 additions & 2 deletions website/docs/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ title: Installation
sidebar_label: Installation
---



### Get library

With npm:
Expand All @@ -18,6 +16,11 @@ With Yarn:
yarn add @react-native-async-storage/async-storage
```

With Expo CLI:
```bash
expo install @react-native-async-storage/async-storage
```

### Link

#### Android & iOS
Expand Down