SingleInstanceStateController.js 6.0 KB

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