LocalDatastoreController.default.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 memMap = {};
  13. const LocalDatastoreController = {
  14. fromPinWithName(name
  15. /*: string*/
  16. )
  17. /*: Array<Object>*/
  18. {
  19. if (!memMap.hasOwnProperty(name)) {
  20. return [];
  21. }
  22. const objects = JSON.parse(memMap[name]);
  23. return objects;
  24. },
  25. pinWithName(name
  26. /*: string*/
  27. , value
  28. /*: any*/
  29. ) {
  30. const values = JSON.stringify(value);
  31. memMap[name] = values;
  32. },
  33. unPinWithName(name
  34. /*: string*/
  35. ) {
  36. delete memMap[name];
  37. },
  38. getAllContents() {
  39. const LDS = {};
  40. for (const key in memMap) {
  41. if (memMap.hasOwnProperty(key) && isLocalDatastoreKey(key)) {
  42. LDS[key] = JSON.parse(memMap[key]);
  43. }
  44. }
  45. return LDS;
  46. },
  47. getRawStorage() {
  48. return memMap;
  49. },
  50. clear() {
  51. for (const key in memMap) {
  52. if (memMap.hasOwnProperty(key) && isLocalDatastoreKey(key)) {
  53. delete memMap[key];
  54. }
  55. }
  56. }
  57. };
  58. module.exports = LocalDatastoreController;