SingleInstanceStateController.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. function _getRequireWildcardCache(nodeInterop) {
  24. if (typeof WeakMap !== "function") return null;
  25. var cacheBabelInterop = new WeakMap();
  26. var cacheNodeInterop = new WeakMap();
  27. return (_getRequireWildcardCache = function (nodeInterop) {
  28. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  29. })(nodeInterop);
  30. }
  31. function _interopRequireWildcard(obj, nodeInterop) {
  32. if (!nodeInterop && obj && obj.__esModule) {
  33. return obj;
  34. }
  35. if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
  36. return {
  37. default: obj
  38. };
  39. }
  40. var cache = _getRequireWildcardCache(nodeInterop);
  41. if (cache && cache.has(obj)) {
  42. return cache.get(obj);
  43. }
  44. var newObj = {};
  45. var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
  46. for (var key in obj) {
  47. if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
  48. var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
  49. if (desc && (desc.get || desc.set)) {
  50. Object.defineProperty(newObj, key, desc);
  51. } else {
  52. newObj[key] = obj[key];
  53. }
  54. }
  55. }
  56. newObj.default = obj;
  57. if (cache) {
  58. cache.set(obj, newObj);
  59. }
  60. return newObj;
  61. }
  62. /**
  63. * @flow
  64. */
  65. /*:: import type { Op } from './ParseOp';*/
  66. /*:: import type { AttributeMap, ObjectCache, OpsMap, State } from './ObjectStateMutations';*/
  67. /*:: type ObjectIdentifier = {
  68. className: string,
  69. id: string,
  70. };*/
  71. let objectState
  72. /*: {
  73. [className: string]: {
  74. [id: string]: State,
  75. },
  76. }*/ = {};
  77. function getState(obj /*: ObjectIdentifier*/) /*: ?State*/{
  78. const classData = objectState[obj.className];
  79. if (classData) {
  80. return classData[obj.id] || null;
  81. }
  82. return null;
  83. }
  84. function initializeState(obj /*: ObjectIdentifier*/, initial /*:: ?: State*/) /*: State*/{
  85. let state = getState(obj);
  86. if (state) {
  87. return state;
  88. }
  89. if (!objectState[obj.className]) {
  90. objectState[obj.className] = {};
  91. }
  92. if (!initial) {
  93. initial = ObjectStateMutations.defaultState();
  94. }
  95. state = objectState[obj.className][obj.id] = initial;
  96. return state;
  97. }
  98. function removeState(obj /*: ObjectIdentifier*/) /*: ?State*/{
  99. const state = getState(obj);
  100. if (state === null) {
  101. return null;
  102. }
  103. delete objectState[obj.className][obj.id];
  104. return state;
  105. }
  106. function getServerData(obj /*: ObjectIdentifier*/) /*: AttributeMap*/{
  107. const state = getState(obj);
  108. if (state) {
  109. return state.serverData;
  110. }
  111. return {};
  112. }
  113. function setServerData(obj /*: ObjectIdentifier*/, attributes /*: AttributeMap*/) {
  114. const serverData = initializeState(obj).serverData;
  115. ObjectStateMutations.setServerData(serverData, attributes);
  116. }
  117. function getPendingOps(obj /*: ObjectIdentifier*/) /*: Array<OpsMap>*/{
  118. const state = getState(obj);
  119. if (state) {
  120. return state.pendingOps;
  121. }
  122. return [{}];
  123. }
  124. function setPendingOp(obj /*: ObjectIdentifier*/, attr /*: string*/, op /*: ?Op*/) {
  125. const pendingOps = initializeState(obj).pendingOps;
  126. ObjectStateMutations.setPendingOp(pendingOps, attr, op);
  127. }
  128. function pushPendingState(obj /*: ObjectIdentifier*/) {
  129. const pendingOps = initializeState(obj).pendingOps;
  130. ObjectStateMutations.pushPendingState(pendingOps);
  131. }
  132. function popPendingState(obj /*: ObjectIdentifier*/) /*: OpsMap*/{
  133. const pendingOps = initializeState(obj).pendingOps;
  134. return ObjectStateMutations.popPendingState(pendingOps);
  135. }
  136. function mergeFirstPendingState(obj /*: ObjectIdentifier*/) {
  137. const pendingOps = getPendingOps(obj);
  138. ObjectStateMutations.mergeFirstPendingState(pendingOps);
  139. }
  140. function getObjectCache(obj /*: ObjectIdentifier*/) /*: ObjectCache*/{
  141. const state = getState(obj);
  142. if (state) {
  143. return state.objectCache;
  144. }
  145. return {};
  146. }
  147. function estimateAttribute(obj /*: ObjectIdentifier*/, attr /*: string*/) /*: mixed*/{
  148. const serverData = getServerData(obj);
  149. const pendingOps = getPendingOps(obj);
  150. return ObjectStateMutations.estimateAttribute(serverData, pendingOps, obj.className, obj.id, attr);
  151. }
  152. function estimateAttributes(obj /*: ObjectIdentifier*/) /*: AttributeMap*/{
  153. const serverData = getServerData(obj);
  154. const pendingOps = getPendingOps(obj);
  155. return ObjectStateMutations.estimateAttributes(serverData, pendingOps, obj.className, obj.id);
  156. }
  157. function commitServerChanges(obj /*: ObjectIdentifier*/, changes /*: AttributeMap*/) {
  158. const state = initializeState(obj);
  159. ObjectStateMutations.commitServerChanges(state.serverData, state.objectCache, changes);
  160. }
  161. function enqueueTask(obj /*: ObjectIdentifier*/, task /*: () => Promise*/) /*: Promise*/{
  162. const state = initializeState(obj);
  163. return state.tasks.enqueue(task);
  164. }
  165. function clearAllState() {
  166. objectState = {};
  167. }
  168. function duplicateState(source /*: { id: string }*/, dest /*: { id: string }*/) {
  169. dest.id = source.id;
  170. }