|
| 1 | +/** |
| 2 | + * Copyright (c) Nicolas Gallagher. |
| 3 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + * |
| 8 | + * @flow |
| 9 | + */ |
| 10 | + |
| 11 | +import merge from 'deep-assign'; |
| 12 | + |
| 13 | +const mergeLocalStorageItem = (key, value) => { |
| 14 | + const oldValue = window.localStorage.getItem(key); |
| 15 | + const oldObject = JSON.parse(oldValue); |
| 16 | + const newObject = JSON.parse(value); |
| 17 | + const nextValue = JSON.stringify(merge({}, oldObject, newObject)); |
| 18 | + window.localStorage.setItem(key, nextValue); |
| 19 | +}; |
| 20 | + |
| 21 | +const createPromise = (getValue, callback): Promise<*> => { |
| 22 | + return new Promise((resolve, reject) => { |
| 23 | + try { |
| 24 | + const value = getValue(); |
| 25 | + if (callback) { |
| 26 | + callback(null, value); |
| 27 | + } |
| 28 | + resolve(value); |
| 29 | + } catch (err) { |
| 30 | + if (callback) { |
| 31 | + callback(err); |
| 32 | + } |
| 33 | + reject(err); |
| 34 | + } |
| 35 | + }); |
| 36 | +}; |
| 37 | + |
| 38 | +const createPromiseAll = (promises, callback, processResult): Promise<*> => { |
| 39 | + return Promise.all(promises).then( |
| 40 | + result => { |
| 41 | + const value = processResult ? processResult(result) : null; |
| 42 | + callback && callback(null, value); |
| 43 | + return Promise.resolve(value); |
| 44 | + }, |
| 45 | + errors => { |
| 46 | + callback && callback(errors); |
| 47 | + return Promise.reject(errors); |
| 48 | + } |
| 49 | + ); |
| 50 | +}; |
| 51 | + |
| 52 | +export default class AsyncStorage { |
| 53 | + |
| 54 | + /** |
| 55 | + * Fetches `key` value. |
| 56 | + */ |
| 57 | + static getItem(key: string, callback?: Function): Promise<*> { |
| 58 | + return createPromise(() => { |
| 59 | + return window.localStorage.getItem(key); |
| 60 | + }, callback); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Sets `value` for `key`. |
| 65 | + */ |
| 66 | + static setItem(key: string, value: string, callback?: Function): Promise<*> { |
| 67 | + return createPromise(() => { |
| 68 | + window.localStorage.setItem(key, value); |
| 69 | + }, callback); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Removes a `key` |
| 74 | + */ |
| 75 | + static removeItem(key: string, callback?: Function): Promise<*> { |
| 76 | + return createPromise(() => { |
| 77 | + return window.localStorage.removeItem(key); |
| 78 | + }, callback); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Merges existing value with input value, assuming they are stringified JSON. |
| 83 | + */ |
| 84 | + static mergeItem(key: string, value: string, callback?: Function): Promise<*> { |
| 85 | + return createPromise(() => { |
| 86 | + mergeLocalStorageItem(key, value); |
| 87 | + }, callback); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Erases *all* AsyncStorage for the domain. |
| 92 | + */ |
| 93 | + static clear(callback?: Function): Promise<*> { |
| 94 | + return createPromise(() => { |
| 95 | + window.localStorage.clear(); |
| 96 | + }, callback); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Gets *all* keys known to the app, for all callers, libraries, etc. |
| 101 | + */ |
| 102 | + static getAllKeys(callback?: Function): Promise<*> { |
| 103 | + return createPromise(() => { |
| 104 | + const numberOfKeys = window.localStorage.length; |
| 105 | + const keys = []; |
| 106 | + for (let i = 0; i < numberOfKeys; i += 1) { |
| 107 | + const key = window.localStorage.key(i); |
| 108 | + keys.push(key); |
| 109 | + } |
| 110 | + return keys; |
| 111 | + }, callback); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * (stub) Flushes any pending requests using a single batch call to get the data. |
| 116 | + */ |
| 117 | + static flushGetRequests() {} |
| 118 | + |
| 119 | + /** |
| 120 | + * multiGet resolves to an array of key-value pair arrays that matches the |
| 121 | + * input format of multiSet. |
| 122 | + * |
| 123 | + * multiGet(['k1', 'k2']) -> [['k1', 'val1'], ['k2', 'val2']] |
| 124 | + */ |
| 125 | + static multiGet(keys: Array<string>, callback?: Function): Promise<*> { |
| 126 | + const promises = keys.map(key => AsyncStorage.getItem(key)); |
| 127 | + const processResult = result => result.map((value, i) => [keys[i], value]); |
| 128 | + return createPromiseAll(promises, callback, processResult); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Takes an array of key-value array pairs. |
| 133 | + * multiSet([['k1', 'val1'], ['k2', 'val2']]) |
| 134 | + */ |
| 135 | + static multiSet(keyValuePairs: Array<Array<string>>, callback?: Function): Promise<*> { |
| 136 | + const promises = keyValuePairs.map(item => AsyncStorage.setItem(item[0], item[1])); |
| 137 | + return createPromiseAll(promises, callback); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Delete all the keys in the `keys` array. |
| 142 | + */ |
| 143 | + static multiRemove(keys: Array<string>, callback?: Function): Promise<*> { |
| 144 | + const promises = keys.map(key => AsyncStorage.removeItem(key)); |
| 145 | + return createPromiseAll(promises, callback); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Takes an array of key-value array pairs and merges them with existing |
| 150 | + * values, assuming they are stringified JSON. |
| 151 | + * |
| 152 | + * multiMerge([['k1', 'val1'], ['k2', 'val2']]) |
| 153 | + */ |
| 154 | + static multiMerge(keyValuePairs: Array<Array<string>>, callback?: Function): Promise<*> { |
| 155 | + const promises = keyValuePairs.map(item => AsyncStorage.mergeItem(item[0], item[1])); |
| 156 | + return createPromiseAll(promises, callback); |
| 157 | + } |
| 158 | +} |
0 commit comments