Skip to content
This repository was archived by the owner on Mar 13, 2023. It is now read-only.

refactor: make async storage a peer dependency #44

Closed
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
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ PODS:
- RNCAsyncStorage (1.15.17):
- React-Core
- SocketRocket (0.6.0)
- sovran-react-native (0.4.3):
- sovran-react-native (0.4.5):
- React-Core
- Yoga (1.14.0)
- YogaKit (1.18.1):
Expand Down Expand Up @@ -534,7 +534,7 @@ SPEC CHECKSUMS:
ReactCommon: d98c6c96b567f9b3a15f9fd4cc302c1eda8e3cf2
RNCAsyncStorage: 6bd5a7ba3dde1c3facba418aa273f449bdc5437a
SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608
sovran-react-native: d9adc84c3666579aacd5d96e315e399cd52241ac
sovran-react-native: fd3dc8f1a4b14acdc4ad25fc6b4ac4f52a2a2a15
Yoga: 9b6696970c3289e8dea34b3eda93f23e61fb8121
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,16 @@
}
},
"peerDependencies": {
"@react-native-async-storage/async-storage": "*",
"react": "*",
"react-native": "*"
},
"peerDependenciesMeta": {
"@react-native-async-storage/async-storage": {
"optional": true
}
},
"dependencies": {
"@react-native-async-storage/async-storage": "^1.15.15",
"ansi-regex": "5.0.1",
"deepmerge": "^4.2.2",
"shell-quote": "1.7.3"
Expand Down
16 changes: 13 additions & 3 deletions src/persistor/async-storage-persistor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import type { Persistor } from './persistor';

let AsyncStorage: {
getItem: (key: string) => Promise<string | null>;
setItem: (key: string, value: string) => Promise<void>;
} | null;

try {
AsyncStorage = require('@react-native-async-storage/async-storage');
} catch (error) {
AsyncStorage = null;
}

/**
* Persistor implementation using AsyncStorage
*/
export const AsyncStoragePersistor: Persistor = {
get: async <T>(key: string): Promise<T | undefined> => {
try {
const persistedStateJSON = await AsyncStorage.getItem(key);
const persistedStateJSON = await AsyncStorage?.getItem?.(key);
if (persistedStateJSON !== null && persistedStateJSON !== undefined) {
return JSON.parse(persistedStateJSON);
}
Expand All @@ -20,7 +30,7 @@ export const AsyncStoragePersistor: Persistor = {

set: async <T>(key: string, state: T): Promise<void> => {
try {
await AsyncStorage.setItem(key, JSON.stringify(state));
await AsyncStorage?.setItem?.(key, JSON.stringify(state));
} catch (e) {
console.error(e);
}
Expand Down
19 changes: 0 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1663,13 +1663,6 @@
dependencies:
"@octokit/openapi-types" "^11.2.0"

"@react-native-async-storage/async-storage@^1.15.15":
version "1.16.1"
resolved "https://registry.yarnpkg.com/@react-native-async-storage/async-storage/-/async-storage-1.16.1.tgz#1dbaa9e0f9736e4ab8fc04c628bbb608fd80b068"
integrity sha512-aQ7ka+Ii1e/q+7AVFIZPt4kDeSH8b784wMDtz19Kf4A7hf+OgCHBlUQpOXsrv8XxhlBxu0hv4tfrDO15ChnV0Q==
dependencies:
merge-options "^3.0.4"

"@react-native-community/cli-debugger-ui@^6.0.0-rc.0":
version "6.0.0-rc.0"
resolved "https://registry.yarnpkg.com/@react-native-community/cli-debugger-ui/-/cli-debugger-ui-6.0.0-rc.0.tgz#774378626e4b70f5e1e2e54910472dcbaffa1536"
Expand Down Expand Up @@ -5404,11 +5397,6 @@ is-plain-obj@^1.1.0:
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-1.1.0.tgz#71a50c8429dfca773c92a390a4a03b39fcd51d3e"
integrity sha1-caUMhCnfync8kqOQpKA7OfzVHT4=

is-plain-obj@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-2.1.0.tgz#45e42e37fccf1f40da8e5f76ee21515840c09287"
integrity sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==

is-plain-object@^2.0.3, is-plain-object@^2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-2.0.4.tgz#2c163b3fafb1b606d9d17928f05c2a1c38e07677"
Expand Down Expand Up @@ -6586,13 +6574,6 @@ meow@^8.0.0:
type-fest "^0.18.0"
yargs-parser "^20.2.3"

merge-options@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/merge-options/-/merge-options-3.0.4.tgz#84709c2aa2a4b24c1981f66c179fe5565cc6dbb7"
integrity sha512-2Sug1+knBjkaMsMgf1ctR1Ujx+Ayku4EdJN4Z+C2+JzoeF7A3OZ9KM2GY0CpQS51NR61LTurMJrRKPhSs3ZRTQ==
dependencies:
is-plain-obj "^2.1.0"

merge-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
Expand Down