SingleInstanceStateController.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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(e) {
  24. if ("function" != typeof WeakMap) return null;
  25. var r = new WeakMap(),
  26. t = new WeakMap();
  27. return (_getRequireWildcardCache = function (e) {
  28. return e ? t : r;
  29. })(e);
  30. }
  31. function _interopRequireWildcard(e, r) {
  32. if (!r && e && e.__esModule) return e;
  33. if (null === e || "object" != typeof e && "function" != typeof e) return {
  34. default: e
  35. };
  36. var t = _getRequireWildcardCache(r);
  37. if (t && t.has(e)) return t.get(e);
  38. var n = {
  39. __proto__: null
  40. },
  41. a = Object.defineProperty && Object.getOwnPropertyDescriptor;
  42. for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) {
  43. var i = a ? Object.getOwnPropertyDescriptor(e, u) : null;
  44. i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u];
  45. }
  46. return n.default = e, t && t.set(e, n), n;
  47. }
  48. let objectState = {};
  49. function getState(obj) {
  50. const classData = objectState[obj.className];
  51. if (classData) {
  52. return classData[obj.id] || null;
  53. }
  54. return null;
  55. }
  56. function initializeState(obj, initial) {
  57. let state = getState(obj);
  58. if (state) {
  59. return state;
  60. }
  61. if (!objectState[obj.className]) {
  62. objectState[obj.className] = {};
  63. }
  64. if (!initial) {
  65. initial = ObjectStateMutations.defaultState();
  66. }
  67. state = objectState[obj.className][obj.id] = initial;
  68. return state;
  69. }
  70. function removeState(obj) {
  71. const state = getState(obj);
  72. if (state === null) {
  73. return null;
  74. }
  75. delete objectState[obj.className][obj.id];
  76. return state;
  77. }
  78. function getServerData(obj) {
  79. const state = getState(obj);
  80. if (state) {
  81. return state.serverData;
  82. }
  83. return {};
  84. }
  85. function setServerData(obj, attributes) {
  86. const serverData = initializeState(obj).serverData;
  87. ObjectStateMutations.setServerData(serverData, attributes);
  88. }
  89. function getPendingOps(obj) {
  90. const state = getState(obj);
  91. if (state) {
  92. return state.pendingOps;
  93. }
  94. return [{}];
  95. }
  96. function setPendingOp(obj, attr, op) {
  97. const pendingOps = initializeState(obj).pendingOps;
  98. ObjectStateMutations.setPendingOp(pendingOps, attr, op);
  99. }
  100. function pushPendingState(obj) {
  101. const pendingOps = initializeState(obj).pendingOps;
  102. ObjectStateMutations.pushPendingState(pendingOps);
  103. }
  104. function popPendingState(obj) {
  105. const pendingOps = initializeState(obj).pendingOps;
  106. return ObjectStateMutations.popPendingState(pendingOps);
  107. }
  108. function mergeFirstPendingState(obj) {
  109. const pendingOps = getPendingOps(obj);
  110. ObjectStateMutations.mergeFirstPendingState(pendingOps);
  111. }
  112. function getObjectCache(obj) {
  113. const state = getState(obj);
  114. if (state) {
  115. return state.objectCache;
  116. }
  117. return {};
  118. }
  119. function estimateAttribute(obj, attr) {
  120. const serverData = getServerData(obj);
  121. const pendingOps = getPendingOps(obj);
  122. return ObjectStateMutations.estimateAttribute(serverData, pendingOps, obj, attr);
  123. }
  124. function estimateAttributes(obj) {
  125. const serverData = getServerData(obj);
  126. const pendingOps = getPendingOps(obj);
  127. return ObjectStateMutations.estimateAttributes(serverData, pendingOps, obj);
  128. }
  129. function commitServerChanges(obj, changes) {
  130. const state = initializeState(obj);
  131. ObjectStateMutations.commitServerChanges(state.serverData, state.objectCache, changes);
  132. }
  133. function enqueueTask(obj, task) {
  134. const state = initializeState(obj);
  135. return state.tasks.enqueue(task);
  136. }
  137. function clearAllState() {
  138. objectState = {};
  139. }
  140. function duplicateState(source, dest) {
  141. dest.id = source.id;
  142. }