SingleInstanceStateController.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. "use strict";
  2. var _typeof = require("@babel/runtime-corejs3/helpers/typeof");
  3. var _WeakMap = require("@babel/runtime-corejs3/core-js-stable/weak-map");
  4. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  5. var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor");
  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 _initializeState = initializeState(obj),
  142. serverData = _initializeState.serverData;
  143. ObjectStateMutations.setServerData(serverData, attributes);
  144. }
  145. function getPendingOps(obj
  146. /*: ObjectIdentifier*/
  147. )
  148. /*: Array<OpsMap>*/
  149. {
  150. var state = getState(obj);
  151. if (state) {
  152. return state.pendingOps;
  153. }
  154. return [{}];
  155. }
  156. function setPendingOp(obj
  157. /*: ObjectIdentifier*/
  158. , attr
  159. /*: string*/
  160. , op
  161. /*: ?Op*/
  162. ) {
  163. var _initializeState2 = initializeState(obj),
  164. pendingOps = _initializeState2.pendingOps;
  165. ObjectStateMutations.setPendingOp(pendingOps, attr, op);
  166. }
  167. function pushPendingState(obj
  168. /*: ObjectIdentifier*/
  169. ) {
  170. var _initializeState3 = initializeState(obj),
  171. pendingOps = _initializeState3.pendingOps;
  172. ObjectStateMutations.pushPendingState(pendingOps);
  173. }
  174. function popPendingState(obj
  175. /*: ObjectIdentifier*/
  176. )
  177. /*: OpsMap*/
  178. {
  179. var _initializeState4 = initializeState(obj),
  180. pendingOps = _initializeState4.pendingOps;
  181. return ObjectStateMutations.popPendingState(pendingOps);
  182. }
  183. function mergeFirstPendingState(obj
  184. /*: ObjectIdentifier*/
  185. ) {
  186. var pendingOps = getPendingOps(obj);
  187. ObjectStateMutations.mergeFirstPendingState(pendingOps);
  188. }
  189. function getObjectCache(obj
  190. /*: ObjectIdentifier*/
  191. )
  192. /*: ObjectCache*/
  193. {
  194. var state = getState(obj);
  195. if (state) {
  196. return state.objectCache;
  197. }
  198. return {};
  199. }
  200. function estimateAttribute(obj
  201. /*: ObjectIdentifier*/
  202. , attr
  203. /*: string*/
  204. )
  205. /*: mixed*/
  206. {
  207. var serverData = getServerData(obj);
  208. var pendingOps = getPendingOps(obj);
  209. return ObjectStateMutations.estimateAttribute(serverData, pendingOps, obj.className, obj.id, attr);
  210. }
  211. function estimateAttributes(obj
  212. /*: ObjectIdentifier*/
  213. )
  214. /*: AttributeMap*/
  215. {
  216. var serverData = getServerData(obj);
  217. var pendingOps = getPendingOps(obj);
  218. return ObjectStateMutations.estimateAttributes(serverData, pendingOps, obj.className, obj.id);
  219. }
  220. function commitServerChanges(obj
  221. /*: ObjectIdentifier*/
  222. , changes
  223. /*: AttributeMap*/
  224. ) {
  225. var state = initializeState(obj);
  226. ObjectStateMutations.commitServerChanges(state.serverData, state.objectCache, changes);
  227. }
  228. function enqueueTask(obj
  229. /*: ObjectIdentifier*/
  230. , task
  231. /*: () => Promise*/
  232. )
  233. /*: Promise*/
  234. {
  235. var state = initializeState(obj);
  236. return state.tasks.enqueue(task);
  237. }
  238. function clearAllState() {
  239. objectState = {};
  240. }
  241. function duplicateState(source
  242. /*: { id: string }*/
  243. , dest
  244. /*: { id: string }*/
  245. ) {
  246. dest.id = source.id;
  247. }