Skip to content

Commit 0f16dc8

Browse files
authored
chore: conditional import of AsyncStorage (#329)
Conditionally import AsyncStorage for backwards compatibility with RN < 0.71.
1 parent 37ff40e commit 0f16dc8

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

packages/sdk/react-native/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"ios": "yarn && yarn ./example && yarn build && (cd example/ && yarn ios-go)"
4141
},
4242
"peerDependencies": {
43-
"react": "*"
43+
"react": "*",
44+
"react-native": "*"
4445
},
4546
"dependencies": {
4647
"@launchdarkly/js-client-sdk-common": "0.0.1",
@@ -64,6 +65,7 @@
6465
"launchdarkly-js-test-helpers": "^2.2.0",
6566
"prettier": "^3.0.0",
6667
"react": "^18.2.0",
68+
"react-native": "^0.73.1",
6769
"rimraf": "^5.0.5",
6870
"ts-jest": "^29.1.0",
6971
"typedoc": "0.25.0",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* eslint-disable import/no-mutable-exports,global-require */
2+
3+
/**
4+
* For react-native version >= 0.71, the LaunchDarkly React-Native SDK uses
5+
* @react-native-async-storage/async-storage for bootstrapping. This is a native
6+
* dependency.
7+
*
8+
* If you are using expo, then adding the LaunchDarkly React Native
9+
* SDK from npm and re-running pod install should suffice.
10+
*
11+
* If you are not using expo, you will need to explicitly add
12+
* @react-native-async-storage/async-storage as a dependency to your project
13+
* and re-run pod install for auto-linking to work. This is because auto-link
14+
* does not work with transitive dependencies:
15+
* https://github.com/react-native-community/cli/issues/1347
16+
*
17+
* For react-native version < 0.71, the built-in react-native AsyncStorage
18+
* module is used.
19+
*/
20+
let ConditionalAsyncStorage: any;
21+
22+
try {
23+
// react-native version < 0.71
24+
ConditionalAsyncStorage = require('react-native').AsyncStorage;
25+
} catch (e) {
26+
// react-native version >= 0.71
27+
ConditionalAsyncStorage = require('@react-native-async-storage/async-storage').default;
28+
}
29+
30+
export default ConditionalAsyncStorage;

packages/sdk/react-native/src/platform.ts renamed to packages/sdk/react-native/src/platform/index.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,4 @@
11
/* eslint-disable max-classes-per-file */
2-
3-
/**
4-
* The LaunchDarkly SDK uses async-storage for bootstrapping and this is a native
5-
* dependency.
6-
*
7-
* If you are using expo, then adding the LaunchDarkly React Native
8-
* SDK from npm and re-running pod install should suffice.
9-
*
10-
* If you are not using expo, you will need to explicitly add
11-
* @react-native-async-storage/async-storage as a dependency to your project
12-
* and re-run pod install for auto-linking to work. This is because auto-link
13-
* does not work with transitive dependencies:
14-
* https://github.com/react-native-community/cli/issues/1347
15-
*/
16-
import AsyncStorage from '@react-native-async-storage/async-storage';
17-
182
import type {
193
Crypto,
204
Encoding,
@@ -34,9 +18,10 @@ import type {
3418
Storage,
3519
} from '@launchdarkly/js-client-sdk-common';
3620

37-
import { name, version } from '../package.json';
38-
import { btoa, uuidv4 } from './polyfills';
39-
import RNEventSource from './react-native-sse';
21+
import { name, version } from '../../package.json';
22+
import { btoa, uuidv4 } from '../polyfills';
23+
import RNEventSource from '../react-native-sse';
24+
import AsyncStorage from './ConditionalAsyncStorage';
4025

4126
class PlatformRequests implements Requests {
4227
createEventSource(url: string, eventSourceInitDict: EventSourceInitDict): EventSource {

0 commit comments

Comments
 (0)