LocalDatastoreController.weapp.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. import { isLocalDatastoreKey } from './LocalDatastoreUtils';
  12. const LocalDatastoreController = {
  13. fromPinWithName(name
  14. /*: string*/
  15. )
  16. /*: Array<Object>*/
  17. {
  18. const values = wx.getStorageSync(name);
  19. if (!values) {
  20. return [];
  21. }
  22. return values;
  23. },
  24. pinWithName(name
  25. /*: string*/
  26. , value
  27. /*: any*/
  28. ) {
  29. try {
  30. wx.setStorageSync(name, value);
  31. } catch (e) {// Quota exceeded
  32. }
  33. },
  34. unPinWithName(name
  35. /*: string*/
  36. ) {
  37. wx.removeStorageSync(name);
  38. },
  39. getAllContents()
  40. /*: Object*/
  41. {
  42. const res = wx.getStorageInfoSync();
  43. const keys = res.keys;
  44. const LDS = {};
  45. for (const key of keys) {
  46. if (isLocalDatastoreKey(key)) {
  47. LDS[key] = wx.getStorageSync(key);
  48. }
  49. }
  50. return LDS;
  51. },
  52. getRawStorage()
  53. /*: Object*/
  54. {
  55. const res = wx.getStorageInfoSync();
  56. const keys = res.keys;
  57. const storage = {};
  58. for (const key of keys) {
  59. storage[key] = wx.getStorageSync(key);
  60. }
  61. return storage;
  62. },
  63. clear()
  64. /*: Promise*/
  65. {
  66. const res = wx.getStorageInfoSync();
  67. const keys = res.keys;
  68. const toRemove = [];
  69. for (const key of keys) {
  70. if (isLocalDatastoreKey(key)) {
  71. toRemove.push(key);
  72. }
  73. }
  74. const promises = toRemove.map(this.unPinWithName);
  75. return Promise.all(promises);
  76. }
  77. };
  78. module.exports = LocalDatastoreController;