UniqueInstanceStateController.js 5.6 KB

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