Skip to content

Commit 2ada903

Browse files
[FSSDK-10936] improvement
1 parent 0e63b09 commit 2ada903

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

lib/plugins/key_value_cache/reactNativeAsyncStorageCache.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,28 @@
1515
*/
1616

1717
import PersistentKeyValueCache from './persistentKeyValueCache';
18-
import { asyncStorage } from '../../utils/import.react_native/@react-native-async-storage/async-storage';
18+
import { getDefaultAsyncStorage } from '../../utils/import.react_native/@react-native-async-storage/async-storage';
1919

2020
export default class ReactNativeAsyncStorageCache implements PersistentKeyValueCache {
21+
private asyncStorage = getDefaultAsyncStorage();
22+
2123
async contains(key: string): Promise<boolean> {
22-
return (await asyncStorage.getItem(key)) !== null;
24+
return (await this.asyncStorage.getItem(key)) !== null;
2325
}
2426

2527
async get(key: string): Promise<string | undefined> {
26-
return (await asyncStorage.getItem(key)) || undefined;
28+
return (await this.asyncStorage.getItem(key)) || undefined;
2729
}
2830

2931
async remove(key: string): Promise<boolean> {
3032
if (await this.contains(key)) {
31-
await asyncStorage.removeItem(key);
33+
await this.asyncStorage.removeItem(key);
3234
return true;
3335
}
3436
return false;
3537
}
3638

3739
set(key: string, val: string): Promise<void> {
38-
return asyncStorage.setItem(key, val);
40+
return this.asyncStorage.setItem(key, val);
3941
}
4042
}

lib/utils/cache/async_storage_cache.react_native.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,35 @@
1616

1717
import { Maybe } from "../type";
1818
import { AsyncCache } from "./cache";
19-
import { asyncStorage } from "../import.react_native/@react-native-async-storage/async-storage";
19+
import { getDefaultAsyncStorage } from "../import.react_native/@react-native-async-storage/async-storage";
2020

2121
export class AsyncStorageCache<V> implements AsyncCache<V> {
2222
public readonly operation = 'async';
23+
private asyncStorage = getDefaultAsyncStorage();
2324

2425
async get(key: string): Promise<V | undefined> {
25-
const value = await asyncStorage.getItem(key);
26+
const value = await this.asyncStorage.getItem(key);
2627
return value ? JSON.parse(value) : undefined;
2728
}
2829

2930
async remove(key: string): Promise<unknown> {
30-
return asyncStorage.removeItem(key);
31+
return this.asyncStorage.removeItem(key);
3132
}
3233

3334
async set(key: string, val: V): Promise<unknown> {
34-
return asyncStorage.setItem(key, JSON.stringify(val));
35+
return this.asyncStorage.setItem(key, JSON.stringify(val));
3536
}
3637

3738
async clear(): Promise<unknown> {
38-
return asyncStorage.clear();
39+
return this.asyncStorage.clear();
3940
}
4041

4142
async getKeys(): Promise<string[]> {
42-
return [... await asyncStorage.getAllKeys()];
43+
return [... await this.asyncStorage.getAllKeys()];
4344
}
4445

4546
async getBatched(keys: string[]): Promise<Maybe<V>[]> {
46-
const items = await asyncStorage.multiGet(keys);
47+
const items = await this.asyncStorage.multiGet(keys);
4748
return items.map(([key, value]) => value ? JSON.parse(value) : undefined);
4849
}
4950
}

lib/utils/import.react_native/@react-native-async-storage/async-storage.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616

1717
import type { AsyncStorageStatic } from '@react-native-async-storage/async-storage'
1818

19-
const requireAsyncStorage = () => {
19+
export const getDefaultAsyncStorage = (): AsyncStorageStatic => {
2020
try {
2121
// eslint-disable-next-line @typescript-eslint/no-var-requires
22-
return require('@react-native-async-storage/async-storage').default as AsyncStorageStatic;
22+
return require('@react-native-async-storage/async-storage').default;
2323
} catch (e) {
24+
// Better error message than unknown module not found
2425
throw new Error('@react-native-async-storage/async-storage is not available');
2526
}
2627
};
27-
28-
const asyncStorage = requireAsyncStorage();
29-
30-
export { asyncStorage }

0 commit comments

Comments
 (0)