Skip to content

Commit b8a17e9

Browse files
authored
Merge pull request #81 from mateosilguero/master
Type Callback Function
2 parents 578238e + f16daec commit b8a17e9

File tree

3 files changed

+147
-41
lines changed

3 files changed

+147
-41
lines changed

example/App.tsx

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import React, {useEffect, useState} from 'react';
2+
import {
3+
SafeAreaView,
4+
StyleSheet,
5+
ScrollView,
6+
Text,
7+
StatusBar,
8+
} from 'react-native';
9+
import MMKV from 'react-native-mmkv-storage';
10+
11+
const MMKVStorage = new MMKV.Loader().withEncryption().initialize();
12+
13+
interface StoredMap {
14+
name: string;
15+
date: number;
16+
}
17+
18+
const App = () => {
19+
const [stringValue, setStringValue] = useState('');
20+
const [arrayValue, setArrayValue] = useState<Array<undefined>>([]);
21+
const [mapValue, setMapValue] = useState<StoredMap>({
22+
name: '',
23+
date: Date.now(),
24+
});
25+
26+
useEffect(() => {
27+
const map = MMKVStorage.getMap<StoredMap>('map');
28+
map && setMapValue(map);
29+
MMKVStorage.removeItem('map');
30+
MMKVStorage.setMap('map', {
31+
name: 'AAA',
32+
date: Date.now(),
33+
});
34+
const randomString = MMKVStorage.getString('random-string');
35+
randomString && setStringValue(randomString);
36+
MMKVStorage.setString(
37+
'random-string',
38+
Math.random().toString(36).substring(7),
39+
);
40+
const array = MMKVStorage.getArray<undefined>('array');
41+
array && setArrayValue(array);
42+
MMKVStorage.setArray('array', new Array(16).fill(undefined));
43+
}, []);
44+
45+
return (
46+
<>
47+
<StatusBar barStyle="dark-content" />
48+
<SafeAreaView>
49+
<ScrollView
50+
contentInsetAdjustmentBehavior="automatic"
51+
style={styles.scrollView}>
52+
<Text>Previous value: {stringValue}</Text>
53+
<Text>
54+
Map value: {mapValue?.name} {mapValue?.date}
55+
</Text>
56+
<Text>Array Length: {arrayValue?.length}</Text>
57+
</ScrollView>
58+
</SafeAreaView>
59+
</>
60+
);
61+
};
62+
63+
const styles = StyleSheet.create({
64+
scrollView: {
65+
padding: 32,
66+
},
67+
});
68+
69+
export default App;

example/tsconfig.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"allowSyntheticDefaultImports": true,
5+
"esModuleInterop": true,
6+
"isolatedModules": true,
7+
"jsx": "react",
8+
"lib": [
9+
"es6"
10+
],
11+
"moduleResolution": "node",
12+
"noEmit": true,
13+
"strict": true,
14+
"target": "esnext",
15+
"paths": {
16+
"react-native-mmkv-storage": [
17+
"../index.d.ts"
18+
]
19+
}
20+
},
21+
"exclude": [
22+
"node_modules",
23+
"babel.config.js",
24+
"metro.config.js",
25+
"jest.config.js"
26+
]
27+
}

index.d.ts

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ type MODES = {
1717
MULTI_PROCESS: number;
1818
};
1919

20+
type Callback<T> = (error: Error | null, result: T | null) => void;
21+
2022
declare module MMKVStorage {
2123
export const MODES: MODES;
2224

@@ -25,10 +27,8 @@ declare module MMKVStorage {
2527
const myVar: string;
2628

2729
class API {
28-
29-
30-
/**
31-
* Set a string value to storage for a given key.
30+
/**
31+
* Set a string value to storage for a given key.
3232
* This method is added for redux-persist support. It is similar to setStringAsync()
3333
*
3434
* @param {String} key
@@ -43,7 +43,6 @@ declare module MMKVStorage {
4343
*/
4444
getItem(key: string): Promise<string>;
4545

46-
4746
/**
4847
* Set a string value to storag for a given key.
4948
*
@@ -101,7 +100,7 @@ declare module MMKVStorage {
101100
* Get an Object from storage for a given key.
102101
* @param {String} key
103102
*/
104-
getMapAsync(key: string): Promise<object | null>;
103+
getMapAsync<T extends object>(key: string): Promise<T | null>;
105104
/**
106105
* Set an array to the db.
107106
* @param {String} key
@@ -113,7 +112,7 @@ declare module MMKVStorage {
113112
* @param {String} key
114113
*/
115114

116-
getArrayAsync(key: string): Promise<Array<any> | null>;
115+
getArrayAsync<T extends any>(key: string): Promise<Array<T> | null>;
117116
/**
118117
* Retrieve multiple Objects for a given array of keys. Currently will work only if data for all keys is an Object.
119118
* Arrays will also be returned but wrappen in a object.
@@ -122,7 +121,9 @@ declare module MMKVStorage {
122121
*
123122
* @param {Array} keys
124123
*/
125-
getMultipleItemsAsync(keys: Array<string>): Promise<Array<object>>;
124+
getMultipleItemsAsync<T extends object>(
125+
keys: Array<string>
126+
): Promise<Array<T>>;
126127

127128
clearStore(): Promise<boolean>;
128129
/**
@@ -139,87 +140,96 @@ declare module MMKVStorage {
139140
*
140141
* @param {String} key
141142
* @param {String} value
142-
* @param {Function} callback
143+
* @param {Callback<boolean>} callback
143144
*/
144-
setString(key: string, value: string, callback?: Function): null;
145+
setString(key: string, value: string, callback?: Callback<boolean>): null;
145146
/**
146147
* Get a string value for a given key.
147148
* @param {String} key
148-
* @param {Function} callback
149+
* @param {Callback<string>} callback
149150
*/
150-
getString(key: string, callback?: Function): string | null;
151+
getString(key: string, callback?: Callback<string>): string | null;
151152

152153
/**
153154
* Set a number value to storage for a given key.
154155
*
155156
* @param {String} key
156157
* @param {number} value
157-
* @param {Function} callback
158+
* @param {Callback<boolean>} callback
158159
*/
159-
setInt(key: string, value: number, callback?: Function): null;
160+
setInt(key: string, value: number, callback?: Callback<boolean>): null;
160161

161162
/**
162163
* Get a number value for a given key
163164
* @param {String} key
164-
* @param {Function} callback
165+
* @param {Callback<number>} callback
165166
*/
166-
getInt(key: string, callback?: Function): number | null;
167+
getInt(key: string, callback?: Callback<number>): number | null;
167168

168169
/**
169170
* Set a boolean value to storag for a given key.
170171
*
171172
* @param {String} key
172173
* @param {boolean} value
173-
* @param {Function} callback
174+
* @param {Callback<boolean>} callback
174175
*/
175-
setBool(key: string, value: boolean, callback?: Function): null;
176+
setBool(key: string, value: boolean, callback?: Callback<boolean>): null;
176177

177178
/**
178179
* Get a boolean value for a given key.
179180
* @param {String} key
180-
* @param {Function} callback
181+
* @param {Callback<boolean>} callback
181182
*/
182-
getBool(key: string, callback?: Function): boolean | null;
183+
getBool(key: string, callback?: Callback<boolean>): boolean | null;
183184

184185
/**
185186
* Set an Object to storage for a given key.
186187
*
187188
* @param {String} key
188189
* @param {Object} value
189-
* @param {Function} callback
190+
* @param {Callback<boolean>} callback
190191
*/
191192

192-
setMap(key: string, value: object, callback?: Function): null;
193+
setMap(key: string, value: object, callback?: Callback<boolean>): null;
193194
/**
194195
* Get an Object from storage for a given key.
195196
* @param {String} key
196-
* @param {Function} callback
197+
* @param {Callback<object>} callback
197198
*/
198-
getMap(key: string, callback?: Function): object | null;
199+
getMap<T extends object>(key: string, callback?: Callback<T>): T | null;
199200
/**
200201
* Set an array to the db.
201202
* @param {String} key
202203
* @param {Array} array
203-
* @param {Function} callback
204+
* @param {Callback<boolean>} callback
204205
*/
205-
setArray(key: string, value: Array<any>, callback?: Function): null;
206+
setArray(
207+
key: string,
208+
value: Array<any>,
209+
callback?: Callback<boolean>
210+
): null;
206211
/**
207212
* get an array from the storage for give key.
208213
* @param {String} key
209-
* @param {Function} callback
214+
* @param {Array<any>} callback
210215
*/
211-
212-
getArray(key: string, callback?: Function): Array<any> | null;
216+
getArray<T extends any>(
217+
key: string,
218+
callback?: Callback<Array<T>>
219+
): Array<T> | null;
213220
/**
214221
* Retrieve multiple Objects for a given array of keys. Currently will work only if data for all keys is an Object.
215222
* Arrays will also be returned but wrappen in a object.
216223
*
217224
* **Will not work if a key as a String value.**
218225
*
219226
* @param {Array} keys
220-
* @param {Function} callback
227+
* @param {Array<object>} callback
221228
*/
222-
getMultipleItems(keys: Array<string>, callback?: Function): Array<object>;
229+
getMultipleItems<T extends object>(
230+
keys: Array<string>,
231+
callback?: Callback<Array<T>>
232+
): Array<T>;
223233

224234
/**
225235
*
@@ -378,7 +388,7 @@ declare module MMKVStorage {
378388
key: string,
379389
secureKeyStorage: boolean,
380390
alias: string,
381-
accessibleMode:ACCESSIBLE
391+
accessibleMode: ACCESSIBLE
382392
): Promise<boolean>;
383393

384394
/**
@@ -400,7 +410,7 @@ declare module MMKVStorage {
400410
key: string,
401411
secureKeyStorage: boolean,
402412
alias: string,
403-
accessibleMode:ACCESSIBLE
413+
accessibleMode: ACCESSIBLE
404414
): Promise<boolean>;
405415
}
406416

@@ -429,14 +439,14 @@ declare module MMKVStorage {
429439
setAccessibleIOS(accessible: ACCESSIBLE): this;
430440

431441
/**
432-
* Provide a custom key to encrypt the storage. Use this if you dont want to generate the key automatically.
433-
* You must call withEncryption() to use this.
434-
* To store your key for later use call withSecureKeyStorage() too.
435-
*
436-
* @param {string} key the key to encrypt the storage with
437-
* @param {boolean} secureKeyStorage Should the key be stored securely.
438-
* @param {string} alias Provide an alias for key storage. Default alias is aliasPrefix + instanceID
439-
*/
442+
* Provide a custom key to encrypt the storage. Use this if you dont want to generate the key automatically.
443+
* You must call withEncryption() to use this.
444+
* To store your key for later use call withSecureKeyStorage() too.
445+
*
446+
* @param {string} key the key to encrypt the storage with
447+
* @param {boolean} secureKeyStorage Should the key be stored securely.
448+
* @param {string} alias Provide an alias for key storage. Default alias is aliasPrefix + instanceID
449+
*/
440450

441451
encryptWithCustomKey(
442452
key: string,

0 commit comments

Comments
 (0)