SingleInstanceStateController.js 5.1 KB

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