SingleInstanceStateController.js 5.7 KB

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