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 | */
|
17 |
|
18 | import { AsyncStorage } from 'react-native';
|
19 |
|
20 | const MEMORY_KEY_PREFIX = '@MemoryStorage:';
|
21 | let dataMemory = {};
|
22 |
|
23 | /** @class */
|
24 | class MemoryStorage {
|
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 | static setItem(key, value) {
|
32 | AsyncStorage.setItem(MEMORY_KEY_PREFIX + key, value);
|
33 | dataMemory[key] = value;
|
34 | return dataMemory[key];
|
35 | }
|
36 |
|
37 | /**
|
38 | * This is used to get a specific key from storage
|
39 | * @param {string} key - the key for the item
|
40 | * This is used to clear the storage
|
41 | * @returns {string} the data item
|
42 | */
|
43 | static getItem(key) {
|
44 | return Object.prototype.hasOwnProperty.call(dataMemory, key)
|
45 | ? dataMemory[key]
|
46 | : undefined;
|
47 | }
|
48 |
|
49 | /**
|
50 | * This is used to remove an item from storage
|
51 | * @param {string} key - the key being set
|
52 | * @returns {string} value - value that was deleted
|
53 | */
|
54 | static removeItem(key) {
|
55 | AsyncStorage.removeItem(MEMORY_KEY_PREFIX + key);
|
56 | return delete dataMemory[key];
|
57 | }
|
58 |
|
59 | /**
|
60 | * This is used to clear the storage
|
61 | * @returns {string} nothing
|
62 | */
|
63 | static clear() {
|
64 | dataMemory = {};
|
65 | return dataMemory;
|
66 | }
|
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 | static sync(callback) {
|
74 | AsyncStorage.getAllKeys((errKeys, keys) => {
|
75 | if (errKeys) return callback(errKeys, null);
|
76 | const memoryKeys = keys.filter(key => key.startsWith(MEMORY_KEY_PREFIX));
|
77 | AsyncStorage.multiGet(memoryKeys, (err, stores) => {
|
78 | if (err) return callback(err, null);
|
79 | stores.map((result, index, store) => {
|
80 | const key = store[index][0];
|
81 | const value = store[index][1];
|
82 | const memoryKey = key.replace(MEMORY_KEY_PREFIX, '');
|
83 | dataMemory[memoryKey] = value;
|
84 | return undefined;
|
85 | });
|
86 | callback(null, 'SUCCESS');
|
87 | return undefined;
|
88 | });
|
89 | return undefined;
|
90 | });
|
91 | }
|
92 | }
|
93 |
|
94 | /** @class */
|
95 | export default class StorageHelper {
|
96 | /**
|
97 | * This is used to get a storage object
|
98 | * @returns {object} the storage
|
99 | */
|
100 | constructor() {
|
101 | this.storageWindow = MemoryStorage;
|
102 | }
|
103 |
|
104 | /**
|
105 | * This is used to return the storage
|
106 | * @returns {object} the storage
|
107 | */
|
108 | getStorage() {
|
109 | return this.storageWindow;
|
110 | }
|
111 | }
|