UNPKG

3.47 kBJavaScriptView Raw
1/*!
2 * Copyright 2016 Amazon.com,
3 * Inc. or its affiliates. All Rights Reserved.
4 *
5 * Licensed under the Amazon Software License (the "License").
6 * You may not use this file except in compliance with the
7 * License. A copy of the License is located at
8 *
9 * http://aws.amazon.com/asl/
10 *
11 * or in the "license" file accompanying this file. This file is
12 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
13 * CONDITIONS OF ANY KIND, express or implied. See the License
14 * for the specific language governing permissions and
15 * limitations under the License.
16 */
17import { AsyncStorage } from 'react-native';
18var MEMORY_KEY_PREFIX = '@MemoryStorage:';
19var dataMemory = {};
20/** @class */
21
22var MemoryStorage = /*#__PURE__*/function () {
23 function MemoryStorage() {}
24
25 /**
26 * This is used to set a specific item in storage
27 * @param {string} key - the key for the item
28 * @param {object} value - the value
29 * @returns {string} value that was set
30 */
31 MemoryStorage.setItem = function setItem(key, value) {
32 AsyncStorage.setItem(MEMORY_KEY_PREFIX + key, value);
33 dataMemory[key] = value;
34 return dataMemory[key];
35 }
36 /**
37 * This is used to get a specific key from storage
38 * @param {string} key - the key for the item
39 * This is used to clear the storage
40 * @returns {string} the data item
41 */
42 ;
43
44 MemoryStorage.getItem = function getItem(key) {
45 return Object.prototype.hasOwnProperty.call(dataMemory, key) ? dataMemory[key] : undefined;
46 }
47 /**
48 * This is used to remove an item from storage
49 * @param {string} key - the key being set
50 * @returns {string} value - value that was deleted
51 */
52 ;
53
54 MemoryStorage.removeItem = function removeItem(key) {
55 AsyncStorage.removeItem(MEMORY_KEY_PREFIX + key);
56 return delete dataMemory[key];
57 }
58 /**
59 * This is used to clear the storage
60 * @returns {string} nothing
61 */
62 ;
63
64 MemoryStorage.clear = function clear() {
65 dataMemory = {};
66 return dataMemory;
67 }
68 /**
69 * Will sync the MemoryStorage data from AsyncStorage to storageWindow MemoryStorage
70 * @param {nodeCallback<string>} callback callback with (err, 'SUCCESS')
71 * @returns {void}
72 */
73 ;
74
75 MemoryStorage.sync = function sync(callback) {
76 AsyncStorage.getAllKeys(function (errKeys, keys) {
77 if (errKeys) return callback(errKeys, null);
78 var memoryKeys = keys.filter(function (key) {
79 return key.startsWith(MEMORY_KEY_PREFIX);
80 });
81 AsyncStorage.multiGet(memoryKeys, function (err, stores) {
82 if (err) return callback(err, null);
83 stores.map(function (result, index, store) {
84 var key = store[index][0];
85 var value = store[index][1];
86 var memoryKey = key.replace(MEMORY_KEY_PREFIX, '');
87 dataMemory[memoryKey] = value;
88 return undefined;
89 });
90 callback(null, 'SUCCESS');
91 return undefined;
92 });
93 return undefined;
94 });
95 };
96
97 return MemoryStorage;
98}();
99/** @class */
100
101
102var StorageHelper = /*#__PURE__*/function () {
103 /**
104 * This is used to get a storage object
105 * @returns {object} the storage
106 */
107 function StorageHelper() {
108 this.storageWindow = MemoryStorage;
109 }
110 /**
111 * This is used to return the storage
112 * @returns {object} the storage
113 */
114
115
116 var _proto = StorageHelper.prototype;
117
118 _proto.getStorage = function getStorage() {
119 return this.storageWindow;
120 };
121
122 return StorageHelper;
123}();
124
125export { StorageHelper as default };
\No newline at end of file