44 | Press n or j to go to the next uncovered block, b, p or k for the previous block. 45 |
46 || File | 53 |54 | | Statements | 55 |56 | | Branches | 57 |58 | | Functions | 59 |60 | | Lines | 61 |62 | |
|---|
| File | 50 |51 | | Statements | 52 |53 | | Branches | 54 |55 | | Functions | 56 |57 | | Lines | 58 |59 | |
|---|---|---|---|---|---|---|---|---|---|
| index.js | 63 |0% | 65 |0/81 | 66 |0% | 67 |0/36 | 68 |0% | 69 |0/19 | 70 |0% | 71 |0/26 | 72 |
| File | 50 |51 | | Statements | 52 |53 | | Branches | 54 |55 | | Functions | 56 |57 | | Lines | 58 |59 | |
|---|---|---|---|---|---|---|---|---|---|
| handleError.js | 63 |0% | 65 |0/8 | 66 |0% | 67 |0/2 | 68 |0% | 69 |0/1 | 70 |0% | 71 |0/8 | 72 |
| File | 50 |51 | | Statements | 52 |53 | | Branches | 54 |55 | | Functions | 56 |57 | | Lines | 58 |59 | |
|---|---|---|---|---|---|---|---|---|---|
| react-native_vx.x.x.js | 63 |100% | 65 |0/0 | 66 |100% | 67 |0/0 | 68 |100% | 69 |0/0 | 70 |100% | 71 |0/0 | 72 |
| 1 47 | 2 48 | 3 49 | 4 50 | 5 51 | 6 52 | 7 53 | 8 54 | 9 55 | 10 56 | 11 57 | 12 58 | 13 59 | 14 60 | 15 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | | // @flow 75 | 76 | function handleError(func: string, param: ?string): Promise<string> { 77 | let message; 78 | if (!param) { 79 | message = func; 80 | } else { 81 | message = `${func}() requires at least ${param} as its first parameter.`; 82 | } 83 | console.warn(message); // eslint-disable-line no-console 84 | return Promise.reject(message); 85 | } 86 | 87 | export default handleError; 88 | |
| 1 47 | 2 48 | 3 49 | 4 50 | 5 51 | 6 52 | 7 53 | 8 54 | 9 55 | 10 56 | 11 57 | 12 58 | 13 59 | 14 60 | 15 61 | 16 62 | 17 63 | 18 64 | 19 65 | 20 66 | 21 67 | 22 68 | 23 69 | 24 70 | 25 71 | 26 72 | 27 73 | 28 74 | 29 75 | 30 76 | 31 77 | 32 78 | 33 79 | 34 80 | 35 81 | 36 82 | 37 83 | 38 84 | 39 85 | 40 86 | 41 87 | 42 88 | 43 89 | 44 90 | 45 91 | 46 92 | 47 93 | 48 94 | 49 95 | 50 96 | 51 97 | 52 98 | 53 99 | 54 100 | 55 101 | 56 102 | 57 103 | 58 104 | 59 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | | import AsyncStorage from '@react-native-community/async-storage'; 163 | 164 | import handleError from './helpers/handleError'; 165 | 166 | type KeyType = string; 167 | 168 | class SyncStorage { 169 | data: Map<*, *> = new Map(); 170 | loading: boolean = true; 171 | 172 | init(): Promise<Array<*>> { 173 | return AsyncStorage.getAllKeys().then((keys: Array<KeyType>) => 174 | AsyncStorage.multiGet(keys).then((data: Array<Array<KeyType>>): Array<*> => { 175 | data.forEach(this.saveItem.bind(this)); 176 | 177 | return [...this.data]; 178 | })); 179 | } 180 | 181 | get(key: KeyType): any { 182 | return this.data.get(key); 183 | } 184 | 185 | set(key: KeyType, value: any): Promise<*> { 186 | if (!key) return handleError('set', 'a key'); 187 | 188 | this.data.set(key, value); 189 | return AsyncStorage.setItem(key, JSON.stringify(value)); 190 | } 191 | 192 | remove(key: KeyType): Promise<*> { 193 | if (!key) return handleError('remove', 'a key'); 194 | 195 | this.data.delete(key); 196 | return AsyncStorage.removeItem(key); 197 | } 198 | 199 | saveItem(item: Array<KeyType>) { 200 | let value; 201 | 202 | try { 203 | value = JSON.parse(item[1]); 204 | } catch (e) { 205 | [, value] = item; 206 | } 207 | 208 | this.data.set(item[0], value); 209 | this.loading = false; 210 | } 211 | 212 | getAllKeys(): Array<*> { 213 | return Array.from(this.data.keys()); 214 | } 215 | } 216 | 217 | const syncStorage = new SyncStorage(); 218 | 219 | export default syncStorage; 220 | |