SingleInstanceStateController.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * @flow
  67. */
  68. /*:: import type { Op } from './ParseOp';*/
  69. /*:: import type { AttributeMap, ObjectCache, OpsMap, State } from './ObjectStateMutations';*/
  70. /*:: type ObjectIdentifier = {
  71. className: string,
  72. id: string,
  73. };*/
  74. var objectState
  75. /*: {
  76. [className: string]: {
  77. [id: string]: State,
  78. },
  79. }*/ = {};
  80. function getState(obj /*: ObjectIdentifier*/) /*: ?State*/{
  81. var classData = objectState[obj.className];
  82. if (classData) {
  83. return classData[obj.id] || null;
  84. }
  85. return null;
  86. }
  87. function initializeState(obj /*: ObjectIdentifier*/, initial /*:: ?: State*/) /*: State*/{
  88. var state = getState(obj);
  89. if (state) {
  90. return state;
  91. }
  92. if (!objectState[obj.className]) {
  93. objectState[obj.className] = {};
  94. }
  95. if (!initial) {
  96. initial = ObjectStateMutations.defaultState();
  97. }
  98. state = objectState[obj.className][obj.id] = initial;
  99. return state;
  100. }
  101. function removeState(obj /*: ObjectIdentifier*/) /*: ?State*/{
  102. var state = getState(obj);
  103. if (state === null) {
  104. return null;
  105. }
  106. delete objectState[obj.className][obj.id];
  107. return state;
  108. }
  109. function getServerData(obj /*: ObjectIdentifier*/) /*: AttributeMap*/{
  110. var state = getState(obj);
  111. if (state) {
  112. return state.serverData;
  113. }
  114. return {};
  115. }
  116. function setServerData(obj /*: ObjectIdentifier*/, attributes /*: AttributeMap*/) {
  117. var serverData = initializeState(obj).serverData;
  118. ObjectStateMutations.setServerData(serverData, attributes);
  119. }
  120. function getPendingOps(obj /*: ObjectIdentifier*/) /*: Array<OpsMap>*/{
  121. var state = getState(obj);
  122. if (state) {
  123. return state.pendingOps;
  124. }
  125. return [{}];
  126. }
  127. function setPendingOp(obj /*: ObjectIdentifier*/, attr /*: string*/, op /*: ?Op*/) {
  128. var pendingOps = initializeState(obj).pendingOps;
  129. ObjectStateMutations.setPendingOp(pendingOps, attr, op);
  130. }
  131. function pushPendingState(obj /*: ObjectIdentifier*/) {
  132. var pendingOps = initializeState(obj).pendingOps;
  133. ObjectStateMutations.pushPendingState(pendingOps);
  134. }
  135. function popPendingState(obj /*: ObjectIdentifier*/) /*: OpsMap*/{
  136. var pendingOps = initializeState(obj).pendingOps;
  137. return ObjectStateMutations.popPendingState(pendingOps);
  138. }
  139. function mergeFirstPendingState(obj /*: ObjectIdentifier*/) {
  140. var pendingOps = getPendingOps(obj);
  141. ObjectStateMutations.mergeFirstPendingState(pendingOps);
  142. }
  143. function getObjectCache(obj /*: ObjectIdentifier*/) /*: ObjectCache*/{
  144. var state = getState(obj);
  145. if (state) {
  146. return state.objectCache;
  147. }
  148. return {};
  149. }
  150. function estimateAttribute(obj /*: ObjectIdentifier*/, attr /*: string*/) /*: mixed*/{
  151. var serverData = getServerData(obj);
  152. var pendingOps = getPendingOps(obj);
  153. return ObjectStateMutations.estimateAttribute(serverData, pendingOps, obj.className, obj.id, attr);
  154. }
  155. function estimateAttributes(obj /*: ObjectIdentifier*/) /*: AttributeMap*/{
  156. var serverData = getServerData(obj);
  157. var pendingOps = getPendingOps(obj);
  158. return ObjectStateMutations.estimateAttributes(serverData, pendingOps, obj.className, obj.id);
  159. }
  160. function commitServerChanges(obj /*: ObjectIdentifier*/, changes /*: AttributeMap*/) {
  161. var state = initializeState(obj);
  162. ObjectStateMutations.commitServerChanges(state.serverData, state.objectCache, changes);
  163. }
  164. function enqueueTask(obj /*: ObjectIdentifier*/, task /*: () => Promise*/) /*: Promise*/{
  165. var state = initializeState(obj);
  166. return state.tasks.enqueue(task);
  167. }
  168. function clearAllState() {
  169. objectState = {};
  170. }
  171. function duplicateState(source /*: { id: string }*/, dest /*: { id: string }*/) {
  172. dest.id = source.id;
  173. }