UniqueInstanceStateController.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * @flow
  70. */
  71. /*:: import type { Op } from './ParseOp';*/
  72. /*:: import type ParseObject from './ParseObject';*/
  73. /*:: import type { AttributeMap, ObjectCache, OpsMap, State } from './ObjectStateMutations';*/
  74. let objectState = new WeakMap();
  75. function getState(obj /*: ParseObject*/) /*: ?State*/{
  76. const classData = objectState.get(obj);
  77. return classData || null;
  78. }
  79. function initializeState(obj /*: ParseObject*/, initial /*:: ?: State*/) /*: State*/{
  80. let state = getState(obj);
  81. if (state) {
  82. return state;
  83. }
  84. if (!initial) {
  85. initial = {
  86. serverData: {},
  87. pendingOps: [{}],
  88. objectCache: {},
  89. tasks: new _TaskQueue.default(),
  90. existed: false
  91. };
  92. }
  93. state = initial;
  94. objectState.set(obj, state);
  95. return state;
  96. }
  97. function removeState(obj /*: ParseObject*/) /*: ?State*/{
  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 /*: ParseObject*/) /*: AttributeMap*/{
  106. const state = getState(obj);
  107. if (state) {
  108. return state.serverData;
  109. }
  110. return {};
  111. }
  112. function setServerData(obj /*: ParseObject*/, attributes /*: AttributeMap*/) {
  113. const serverData = initializeState(obj).serverData;
  114. ObjectStateMutations.setServerData(serverData, attributes);
  115. }
  116. function getPendingOps(obj /*: ParseObject*/) /*: Array<OpsMap>*/{
  117. const state = getState(obj);
  118. if (state) {
  119. return state.pendingOps;
  120. }
  121. return [{}];
  122. }
  123. function setPendingOp(obj /*: ParseObject*/, attr /*: string*/, op /*: ?Op*/) {
  124. const pendingOps = initializeState(obj).pendingOps;
  125. ObjectStateMutations.setPendingOp(pendingOps, attr, op);
  126. }
  127. function pushPendingState(obj /*: ParseObject*/) {
  128. const pendingOps = initializeState(obj).pendingOps;
  129. ObjectStateMutations.pushPendingState(pendingOps);
  130. }
  131. function popPendingState(obj /*: ParseObject*/) /*: OpsMap*/{
  132. const pendingOps = initializeState(obj).pendingOps;
  133. return ObjectStateMutations.popPendingState(pendingOps);
  134. }
  135. function mergeFirstPendingState(obj /*: ParseObject*/) {
  136. const pendingOps = getPendingOps(obj);
  137. ObjectStateMutations.mergeFirstPendingState(pendingOps);
  138. }
  139. function getObjectCache(obj /*: ParseObject*/) /*: ObjectCache*/{
  140. const state = getState(obj);
  141. if (state) {
  142. return state.objectCache;
  143. }
  144. return {};
  145. }
  146. function estimateAttribute(obj /*: ParseObject*/, attr /*: string*/) /*: mixed*/{
  147. const serverData = getServerData(obj);
  148. const pendingOps = getPendingOps(obj);
  149. return ObjectStateMutations.estimateAttribute(serverData, pendingOps, obj.className, obj.id, attr);
  150. }
  151. function estimateAttributes(obj /*: ParseObject*/) /*: AttributeMap*/{
  152. const serverData = getServerData(obj);
  153. const pendingOps = getPendingOps(obj);
  154. return ObjectStateMutations.estimateAttributes(serverData, pendingOps, obj.className, obj.id);
  155. }
  156. function commitServerChanges(obj /*: ParseObject*/, changes /*: AttributeMap*/) {
  157. const state = initializeState(obj);
  158. ObjectStateMutations.commitServerChanges(state.serverData, state.objectCache, changes);
  159. }
  160. function enqueueTask(obj /*: ParseObject*/, task /*: () => Promise*/) /*: Promise*/{
  161. const state = initializeState(obj);
  162. return state.tasks.enqueue(task);
  163. }
  164. function duplicateState(source /*: ParseObject*/, dest /*: ParseObject*/) /*: void*/{
  165. const oldState = initializeState(source);
  166. const newState = initializeState(dest);
  167. for (const key in oldState.serverData) {
  168. newState.serverData[key] = oldState.serverData[key];
  169. }
  170. for (let index = 0; index < oldState.pendingOps.length; index++) {
  171. for (const key in oldState.pendingOps[index]) {
  172. newState.pendingOps[index][key] = oldState.pendingOps[index][key];
  173. }
  174. }
  175. for (const key in oldState.objectCache) {
  176. newState.objectCache[key] = oldState.objectCache[key];
  177. }
  178. newState.existed = oldState.existed;
  179. }
  180. function clearAllState() {
  181. objectState = new WeakMap();
  182. }