LocalDatastoreController.weapp.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. var _LocalDatastoreUtils = require("./LocalDatastoreUtils");
  3. /**
  4. * Copyright (c) 2015-present, Parse, LLC.
  5. * All rights reserved.
  6. *
  7. * This source code is licensed under the BSD-style license found in the
  8. * LICENSE file in the root directory of this source tree. An additional grant
  9. * of patent rights can be found in the PATENTS file in the same directory.
  10. *
  11. * @flow
  12. */
  13. const LocalDatastoreController = {
  14. fromPinWithName(name
  15. /*: string*/
  16. )
  17. /*: Array<Object>*/
  18. {
  19. const values = wx.getStorageSync(name);
  20. if (!values) {
  21. return [];
  22. }
  23. return values;
  24. },
  25. pinWithName(name
  26. /*: string*/
  27. , value
  28. /*: any*/
  29. ) {
  30. try {
  31. wx.setStorageSync(name, value);
  32. } catch (e) {// Quota exceeded
  33. }
  34. },
  35. unPinWithName(name
  36. /*: string*/
  37. ) {
  38. wx.removeStorageSync(name);
  39. },
  40. getAllContents()
  41. /*: Object*/
  42. {
  43. const res = wx.getStorageInfoSync();
  44. const keys = res.keys;
  45. const LDS = {};
  46. for (const key of keys) {
  47. if ((0, _LocalDatastoreUtils.isLocalDatastoreKey)(key)) {
  48. LDS[key] = wx.getStorageSync(key);
  49. }
  50. }
  51. return LDS;
  52. },
  53. getRawStorage()
  54. /*: Object*/
  55. {
  56. const res = wx.getStorageInfoSync();
  57. const keys = res.keys;
  58. const storage = {};
  59. for (const key of keys) {
  60. storage[key] = wx.getStorageSync(key);
  61. }
  62. return storage;
  63. },
  64. clear()
  65. /*: Promise*/
  66. {
  67. const res = wx.getStorageInfoSync();
  68. const keys = res.keys;
  69. const toRemove = [];
  70. for (const key of keys) {
  71. if ((0, _LocalDatastoreUtils.isLocalDatastoreKey)(key)) {
  72. toRemove.push(key);
  73. }
  74. }
  75. const promises = toRemove.map(this.unPinWithName);
  76. return Promise.all(promises);
  77. }
  78. };
  79. module.exports = LocalDatastoreController;