Skip to content

Commit

Permalink
feat: remove .expo extension in favor of generic fallback to legacy n…
Browse files Browse the repository at this point in the history
…ative module mechanism (#544)
  • Loading branch information
brentvatne committed Feb 22, 2021
1 parent c7f61aa commit aca9af9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
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

0 comments on commit aca9af9

Please sign in to comment.