-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
maps.ts
50 lines (46 loc) · 1.26 KB
/
maps.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { handleActionAsync, handleAction } from '../handlers';
import mmkvJsiModule from '../module';
import { GenericReturnType } from '../types';
const INDEX_TYPE = 'mapIndex';
/**
* Index of all objects stored in storage.
*/
export default class mapsIndex {
instanceID: string;
constructor(id: string) {
this.instanceID = id;
}
/**
*
* Get all keys
*/
async getKeys() {
return await handleActionAsync(mmkvJsiModule.getIndexMMKV, INDEX_TYPE, this.instanceID);
}
/**
* Check if a key exists.
*/
hasKey(key: string) {
let keys = handleAction(mmkvJsiModule.getIndexMMKV, INDEX_TYPE, this.instanceID);
return keys && keys.indexOf(key) > -1;
}
/**
* Get all objects stored in storage.
*/
async getAll<T>() {
return new Promise(resolve => {
let keys = handleAction(mmkvJsiModule.getIndexMMKV, INDEX_TYPE, this.instanceID);
if (!keys) keys = [];
let items: GenericReturnType<T>[] = [];
for (let i = 0; i < keys.length; i++) {
//@ts-ignore
let item: GenericReturnType<T> = [];
item[0] = keys[i];
let map = mmkvJsiModule.getMapMMKV(keys[i], this.instanceID);
item[1] = map ? JSON.parse(map) : null;
items.push(item);
}
resolve(items);
});
}
}