LocalDatastoreController.react-native.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Copyright (c) 2015-present, Parse, LLC.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. * @flow
  10. */
  11. const RNStorage = require('./StorageController.react-native');
  12. import { isLocalDatastoreKey } from './LocalDatastoreUtils';
  13. const LocalDatastoreController = {
  14. async fromPinWithName(name
  15. /*: string*/
  16. )
  17. /*: Promise<Array<Object>>*/
  18. {
  19. const values = await RNStorage.getItemAsync(name);
  20. if (!values) {
  21. return [];
  22. }
  23. const objects = JSON.parse(values);
  24. return objects;
  25. },
  26. async pinWithName(name
  27. /*: string*/
  28. , value
  29. /*: any*/
  30. )
  31. /*: Promise<void>*/
  32. {
  33. try {
  34. const values = JSON.stringify(value);
  35. await RNStorage.setItemAsync(name, values);
  36. } catch (e) {
  37. // Quota exceeded, possibly due to Safari Private Browsing mode
  38. console.error(e.message);
  39. }
  40. },
  41. unPinWithName(name
  42. /*: string*/
  43. )
  44. /*: Promise<void>*/
  45. {
  46. return RNStorage.removeItemAsync(name);
  47. },
  48. async getAllContents()
  49. /*: Promise<Object>*/
  50. {
  51. const keys = await RNStorage.getAllKeys();
  52. const batch = [];
  53. for (let i = 0; i < keys.length; i += 1) {
  54. const key = keys[i];
  55. if (isLocalDatastoreKey(key)) {
  56. batch.push(key);
  57. }
  58. }
  59. const LDS = {};
  60. let results = [];
  61. try {
  62. results = await RNStorage.multiGet(batch);
  63. } catch (error) {
  64. console.error('Error getAllContents: ', error);
  65. return {};
  66. }
  67. results.forEach(pair => {
  68. const [key, value] = pair;
  69. try {
  70. LDS[key] = JSON.parse(value);
  71. } catch (error) {
  72. LDS[key] = null;
  73. }
  74. });
  75. return LDS;
  76. },
  77. async getRawStorage()
  78. /*: Promise<Object>*/
  79. {
  80. const keys = await RNStorage.getAllKeys();
  81. const storage = {};
  82. const results = await RNStorage.multiGet(keys);
  83. results.map(pair => {
  84. const [key, value] = pair;
  85. storage[key] = value;
  86. });
  87. return storage;
  88. },
  89. async clear()
  90. /*: Promise<void>*/
  91. {
  92. const keys = await RNStorage.getAllKeys();
  93. const batch = [];
  94. for (let i = 0; i < keys.length; i += 1) {
  95. const key = keys[i];
  96. if (isLocalDatastoreKey(key)) {
  97. batch.push(key);
  98. }
  99. }
  100. return RNStorage.multiRemove(batch).catch(error => console.error('Error clearing local datastore: ', error));
  101. }
  102. };
  103. module.exports = LocalDatastoreController;