SingleInstanceStateController.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. "use strict";
  2. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.getState = getState;
  7. exports.initializeState = initializeState;
  8. exports.removeState = removeState;
  9. exports.getServerData = getServerData;
  10. exports.setServerData = setServerData;
  11. exports.getPendingOps = getPendingOps;
  12. exports.setPendingOp = setPendingOp;
  13. exports.pushPendingState = pushPendingState;
  14. exports.popPendingState = popPendingState;
  15. exports.mergeFirstPendingState = mergeFirstPendingState;
  16. exports.getObjectCache = getObjectCache;
  17. exports.estimateAttribute = estimateAttribute;
  18. exports.estimateAttributes = estimateAttributes;
  19. exports.commitServerChanges = commitServerChanges;
  20. exports.enqueueTask = enqueueTask;
  21. exports.clearAllState = clearAllState;
  22. exports.duplicateState = duplicateState;
  23. var ObjectStateMutations = _interopRequireWildcard(require("./ObjectStateMutations"));
  24. /**
  25. * Copyright (c) 2015-present, Parse, LLC.
  26. * All rights reserved.
  27. *
  28. * This source code is licensed under the BSD-style license found in the
  29. * LICENSE file in the root directory of this source tree. An additional grant
  30. * of patent rights can be found in the PATENTS file in the same directory.
  31. *
  32. * @flow
  33. */
  34. var objectState
  35. /*: {
  36. [className: string]: {
  37. [id: string]: State
  38. }
  39. }*/
  40. = {};
  41. function getState(obj
  42. /*: ObjectIdentifier*/
  43. )
  44. /*: ?State*/
  45. {
  46. var classData = objectState[obj.className];
  47. if (classData) {
  48. return classData[obj.id] || null;
  49. }
  50. return null;
  51. }
  52. function initializeState(obj
  53. /*: ObjectIdentifier*/
  54. , initial
  55. /*:: ?: State*/
  56. )
  57. /*: State*/
  58. {
  59. var state = getState(obj);
  60. if (state) {
  61. return state;
  62. }
  63. if (!objectState[obj.className]) {
  64. objectState[obj.className] = {};
  65. }
  66. if (!initial) {
  67. initial = ObjectStateMutations.defaultState();
  68. }
  69. state = objectState[obj.className][obj.id] = initial;
  70. return state;
  71. }
  72. function removeState(obj
  73. /*: ObjectIdentifier*/
  74. )
  75. /*: ?State*/
  76. {
  77. var state = getState(obj);
  78. if (state === null) {
  79. return null;
  80. }
  81. delete objectState[obj.className][obj.id];
  82. return state;
  83. }
  84. function getServerData(obj
  85. /*: ObjectIdentifier*/
  86. )
  87. /*: AttributeMap*/
  88. {
  89. var state = getState(obj);
  90. if (state) {
  91. return state.serverData;
  92. }
  93. return {};
  94. }
  95. function setServerData(obj
  96. /*: ObjectIdentifier*/
  97. , attributes
  98. /*: AttributeMap*/
  99. ) {
  100. var serverData = initializeState(obj).serverData;
  101. ObjectStateMutations.setServerData(serverData, attributes);
  102. }
  103. function getPendingOps(obj
  104. /*: ObjectIdentifier*/
  105. )
  106. /*: Array<OpsMap>*/
  107. {
  108. var state = getState(obj);
  109. if (state) {
  110. return state.pendingOps;
  111. }
  112. return [{}];
  113. }
  114. function setPendingOp(obj
  115. /*: ObjectIdentifier*/
  116. , attr
  117. /*: string*/
  118. , op
  119. /*: ?Op*/
  120. ) {
  121. var pendingOps = initializeState(obj).pendingOps;
  122. ObjectStateMutations.setPendingOp(pendingOps, attr, op);
  123. }
  124. function pushPendingState(obj
  125. /*: ObjectIdentifier*/
  126. ) {
  127. var pendingOps = initializeState(obj).pendingOps;
  128. ObjectStateMutations.pushPendingState(pendingOps);
  129. }
  130. function popPendingState(obj
  131. /*: ObjectIdentifier*/
  132. )
  133. /*: OpsMap*/
  134. {
  135. var pendingOps = initializeState(obj).pendingOps;
  136. return ObjectStateMutations.popPendingState(pendingOps);
  137. }
  138. function mergeFirstPendingState(obj
  139. /*: ObjectIdentifier*/
  140. ) {
  141. var pendingOps = getPendingOps(obj);
  142. ObjectStateMutations.mergeFirstPendingState(pendingOps);
  143. }
  144. function getObjectCache(obj
  145. /*: ObjectIdentifier*/
  146. )
  147. /*: ObjectCache*/
  148. {
  149. var state = getState(obj);
  150. if (state) {
  151. return state.objectCache;
  152. }
  153. return {};
  154. }
  155. function estimateAttribute(obj
  156. /*: ObjectIdentifier*/
  157. , attr
  158. /*: string*/
  159. )
  160. /*: mixed*/
  161. {
  162. var serverData = getServerData(obj);
  163. var pendingOps = getPendingOps(obj);
  164. return ObjectStateMutations.estimateAttribute(serverData, pendingOps, obj.className, obj.id, attr);
  165. }
  166. function estimateAttributes(obj
  167. /*: ObjectIdentifier*/
  168. )
  169. /*: AttributeMap*/
  170. {
  171. var serverData = getServerData(obj);
  172. var pendingOps = getPendingOps(obj);
  173. return ObjectStateMutations.estimateAttributes(serverData, pendingOps, obj.className, obj.id);
  174. }
  175. function commitServerChanges(obj
  176. /*: ObjectIdentifier*/
  177. , changes
  178. /*: AttributeMap*/
  179. ) {
  180. var state = initializeState(obj);
  181. ObjectStateMutations.commitServerChanges(state.serverData, state.objectCache, changes);
  182. }
  183. function enqueueTask(obj
  184. /*: ObjectIdentifier*/
  185. , task
  186. /*: () => Promise*/
  187. )
  188. /*: Promise*/
  189. {
  190. var state = initializeState(obj);
  191. return state.tasks.enqueue(task);
  192. }
  193. function clearAllState() {
  194. objectState = {};
  195. }
  196. function duplicateState(source
  197. /*: {id: string}*/
  198. , dest
  199. /*: {id: string}*/
  200. ) {
  201. dest.id = source.id;
  202. }