Skip to content

Commit

Permalink
feat: Change AsyncStorage lib (#1710)
Browse files Browse the repository at this point in the history
The previously used AsyncStorage lib was deprecated, and was replaces by
the new AsyncStorage lib suggested by the previous maintainers.
  • Loading branch information
gorandalum authored Oct 27, 2021
1 parent d3913ed commit 3da42fe
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 43 deletions.
10 changes: 5 additions & 5 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,8 @@ PODS:
- React-jsi (= 0.63.4)
- RNBootSplash (3.2.4):
- React-Core
- RNCAsyncStorage (2.0.0):
- React
- RNCAsyncStorage (1.15.9):
- React-Core
- RNCClipboard (1.8.4):
- React-Core
- RNCMaskedView (0.1.11):
Expand Down Expand Up @@ -802,7 +802,7 @@ DEPENDENCIES:
- React-RCTVibration (from `../node_modules/react-native/Libraries/Vibration`)
- ReactCommon/turbomodule/core (from `../node_modules/react-native/ReactCommon`)
- RNBootSplash (from `../node_modules/react-native-bootsplash`)
- "RNCAsyncStorage (from `../node_modules/@react-native-community/async-storage-backend-legacy`)"
- "RNCAsyncStorage (from `../node_modules/@react-native-async-storage/async-storage`)"
- "RNCClipboard (from `../node_modules/@react-native-clipboard/clipboard`)"
- "RNCMaskedView (from `../node_modules/@react-native-community/masked-view`)"
- "RNDateTimePicker (from `../node_modules/@react-native-community/datetimepicker`)"
Expand Down Expand Up @@ -933,7 +933,7 @@ EXTERNAL SOURCES:
RNBootSplash:
:path: "../node_modules/react-native-bootsplash"
RNCAsyncStorage:
:path: "../node_modules/@react-native-community/async-storage-backend-legacy"
:path: "../node_modules/@react-native-async-storage/async-storage"
RNCClipboard:
:path: "../node_modules/@react-native-clipboard/clipboard"
RNCMaskedView:
Expand Down Expand Up @@ -1041,7 +1041,7 @@ SPEC CHECKSUMS:
React-RCTVibration: ae4f914cfe8de7d4de95ae1ea6cc8f6315d73d9d
ReactCommon: 73d79c7039f473b76db6ff7c6b159c478acbbb3b
RNBootSplash: 4844706cbb56a3270556c9b94e59dedadccd47e4
RNCAsyncStorage: b5de28ebf07f83fc83fe718cb17793c5cfe55181
RNCAsyncStorage: 26f25150da507524a7815f2ada06ca0967f65633
RNCClipboard: ddd4d291537f1667209c9c405aaa4307297e252e
RNCMaskedView: 0e1bc4bfa8365eba5fbbb71e07fbdc0555249489
RNDateTimePicker: 7658208086d86d09e1627b5c34ba0cf237c60140
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
"@entur/sdk": "^3.4.0",
"@leile/lobo-t": "^1.0.5",
"@mapbox/polyline": "^1.1.1",
"@react-native-async-storage/async-storage": "^1.15.9",
"@react-native-clipboard/clipboard": "^1.8.4",
"@react-native-community/async-storage": "^2.0.0-rc.1",
"@react-native-community/async-storage-backend-legacy": "^2.0.0-rc.1",
"@react-native-community/datetimepicker": "^3.5.2",
"@react-native-community/masked-view": "^0.1.11",
"@react-native-firebase/analytics": "10.1.0",
Expand Down
16 changes: 5 additions & 11 deletions src/screens/Profile/DebugInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@ export default function DebugInfo() {
run();
}, [user]);

const [storedValues, setStoredValues] = useState<any | undefined>(undefined);
const [storedValues, setStoredValues] = useState<
[string, string | null][] | null
>(null);

useEffect(() => {
async function run() {
const keys = await storage.getKeys();
const values = await storage.getMultiple(keys);
setStoredValues(values);
}

run();
storage.getAll().then(setStoredValues);
}, []);

function copyFirestoreLink() {
Expand Down Expand Up @@ -108,9 +104,7 @@ export default function DebugInfo() {
<Sections.HeaderItem text="Storage" />
{storedValues && (
<Sections.GenericItem>
{Object.entries(storedValues).map(([key, value]) =>
mapEntry(key, value),
)}
{storedValues.map(([key, value]) => mapEntry(key, value))}
</Sections.GenericItem>
)}
</Sections.Section>
Expand Down
47 changes: 34 additions & 13 deletions src/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import LegacyStorage from '@react-native-community/async-storage-backend-legacy';
import AsyncStorageFactory from '@react-native-community/async-storage';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Bugsnag from '@bugsnag/react-native';

export type StorageModel = {
Expand All @@ -18,17 +17,39 @@ export type StorageModel = {

export type StorageModelTypes = keyof StorageModel;

const legacyStorage = new LegacyStorage();
const errorHandler = (error: any) => {
Bugsnag.notify(typeof error === 'string' ? new Error(error) : error);
return Promise.resolve(null);
};

const leaveBreadCrumb = (
action: 'read-single' | 'save-single' | 'read-all',
key?: string,
value?: string | null,
) => {
Bugsnag.leaveBreadcrumb('storage_action', {
action,
key,
value: __DEV__ ? value : undefined,
});
};

const storage = AsyncStorageFactory.create<StorageModel>(legacyStorage, {
errorHandler: (error) =>
Bugsnag.notify(typeof error === 'string' ? new Error(error) : error),
logger: (action) =>
Bugsnag.leaveBreadcrumb('storage_action', {
action: action.action,
key: Array.isArray(action.key) ? action.key.join(',') : action.key,
value: __DEV__ ? action.value : undefined,
}),
});
const storage = {
get: async (key: string) => {
const value = await AsyncStorage.getItem(key).catch(errorHandler);
leaveBreadCrumb('read-single', key, value);
return value;
},
set: async (key: string, value: string) => {
leaveBreadCrumb('save-single', key, value);
return AsyncStorage.setItem(key, value).catch(errorHandler);
},
getAll: async () => {
leaveBreadCrumb('read-all');
return AsyncStorage.getAllKeys()
.then((keys) => AsyncStorage.multiGet(keys))
.catch(errorHandler);
},
};

export default storage;
26 changes: 14 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1646,23 +1646,18 @@
"@nodelib/fs.scandir" "2.1.4"
fastq "^1.6.0"

"@react-native-async-storage/async-storage@^1.15.9":
version "1.15.9"
resolved "https://registry.yarnpkg.com/@react-native-async-storage/async-storage/-/async-storage-1.15.9.tgz#744ecd566f108e86b6b59e617c23fdb4b8d53358"
integrity sha512-LrVPfhKqodRiDWCgZp7J2X55JQqOhdQUxbl17RrktIGCZ0ud2XHdNoTIvyI1VccqHoF/CZK6v+G0IoX5NYZ1JA==
dependencies:
merge-options "^3.0.4"

"@react-native-clipboard/clipboard@^1.8.4":
version "1.8.4"
resolved "https://registry.yarnpkg.com/@react-native-clipboard/clipboard/-/clipboard-1.8.4.tgz#4bc1fb00643688e489d8220cd635844ab5c066f9"
integrity sha512-poFq3RvXzkbXcqoQNssbZ+aNbCRzBFAWkR9QL7u9xNMgsyWZtk7d16JQoaBo8D2E+kKi+/9JOiVQzA5w+9N67w==

"@react-native-community/async-storage-backend-legacy@^2.0.0-rc.1":
version "2.0.0-rc.3"
resolved "https://registry.yarnpkg.com/@react-native-community/async-storage-backend-legacy/-/async-storage-backend-legacy-2.0.0-rc.3.tgz#4f905bcfa4168918b61835bcd80ab16c68e2f5a5"
integrity sha512-nWH8GDMxQ93Ayx1ZhQWZkH70yz8e2mGOeiF7xY7h1/GNxl0taWJrjaWRaHb9FNcC7xe42c33hIjJBRZe0cMlFQ==
dependencies:
"@react-native-community/async-storage" "^2.0.0-rc.3"

"@react-native-community/async-storage@^2.0.0-rc.1", "@react-native-community/async-storage@^2.0.0-rc.3":
version "2.0.0-rc.3"
resolved "https://registry.yarnpkg.com/@react-native-community/async-storage/-/async-storage-2.0.0-rc.3.tgz#e2ad162ef5eff1a32005753a7c054d463307b38f"
integrity sha512-inAIetMy5HwKA9vZGKrVzXA+Uxr7EzKy/IG+DsWTISEqQMwl0b3JWufRZHk3Xj7MqMSn8PdBDM/mmk5tY15qLg==

"@react-native-community/cli-debugger-ui@^4.13.1":
version "4.13.1"
resolved "https://registry.yarnpkg.com/@react-native-community/cli-debugger-ui/-/cli-debugger-ui-4.13.1.tgz#07de6d4dab80ec49231de1f1fbf658b4ad39b32c"
Expand Down Expand Up @@ -6953,6 +6948,13 @@ 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@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-1.0.1.tgz#4041202d508a342ba00174008df0c251b8c135e1"
Expand Down

0 comments on commit 3da42fe

Please sign in to comment.