UNPKG

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