UniqueInstanceStateController.js 6.2 KB

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