SingleInstanceStateController.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. * Copyright (c) 2015-present, Parse, LLC.
  64. * All rights reserved.
  65. *
  66. * This source code is licensed under the BSD-style license found in the
  67. * LICENSE file in the root directory of this source tree. An additional grant
  68. * of patent rights can be found in the PATENTS file in the same directory.
  69. *
  70. * @flow
  71. */
  72. let objectState
  73. /*: {
  74. [className: string]: {
  75. [id: string]: State,
  76. },
  77. }*/
  78. = {};
  79. function getState(obj
  80. /*: ObjectIdentifier*/
  81. )
  82. /*: ?State*/
  83. {
  84. const classData = objectState[obj.className];
  85. if (classData) {
  86. return classData[obj.id] || null;
  87. }
  88. return null;
  89. }
  90. function initializeState(obj
  91. /*: ObjectIdentifier*/
  92. , initial
  93. /*:: ?: State*/
  94. )
  95. /*: State*/
  96. {
  97. let state = getState(obj);
  98. if (state) {
  99. return state;
  100. }
  101. if (!objectState[obj.className]) {
  102. objectState[obj.className] = {};
  103. }
  104. if (!initial) {
  105. initial = ObjectStateMutations.defaultState();
  106. }
  107. state = objectState[obj.className][obj.id] = initial;
  108. return state;
  109. }
  110. function removeState(obj
  111. /*: ObjectIdentifier*/
  112. )
  113. /*: ?State*/
  114. {
  115. const state = getState(obj);
  116. if (state === null) {
  117. return null;
  118. }
  119. delete objectState[obj.className][obj.id];
  120. return state;
  121. }
  122. function getServerData(obj
  123. /*: ObjectIdentifier*/
  124. )
  125. /*: AttributeMap*/
  126. {
  127. const state = getState(obj);
  128. if (state) {
  129. return state.serverData;
  130. }
  131. return {};
  132. }
  133. function setServerData(obj
  134. /*: ObjectIdentifier*/
  135. , attributes
  136. /*: AttributeMap*/
  137. ) {
  138. const {
  139. serverData
  140. } = initializeState(obj);
  141. ObjectStateMutations.setServerData(serverData, attributes);
  142. }
  143. function getPendingOps(obj
  144. /*: ObjectIdentifier*/
  145. )
  146. /*: Array<OpsMap>*/
  147. {
  148. const state = getState(obj);
  149. if (state) {
  150. return state.pendingOps;
  151. }
  152. return [{}];
  153. }
  154. function setPendingOp(obj
  155. /*: ObjectIdentifier*/
  156. , attr
  157. /*: string*/
  158. , op
  159. /*: ?Op*/
  160. ) {
  161. const {
  162. pendingOps
  163. } = initializeState(obj);
  164. ObjectStateMutations.setPendingOp(pendingOps, attr, op);
  165. }
  166. function pushPendingState(obj
  167. /*: ObjectIdentifier*/
  168. ) {
  169. const {
  170. pendingOps
  171. } = initializeState(obj);
  172. ObjectStateMutations.pushPendingState(pendingOps);
  173. }
  174. function popPendingState(obj
  175. /*: ObjectIdentifier*/
  176. )
  177. /*: OpsMap*/
  178. {
  179. const {
  180. pendingOps
  181. } = initializeState(obj);
  182. return ObjectStateMutations.popPendingState(pendingOps);
  183. }
  184. function mergeFirstPendingState(obj
  185. /*: ObjectIdentifier*/
  186. ) {
  187. const pendingOps = getPendingOps(obj);
  188. ObjectStateMutations.mergeFirstPendingState(pendingOps);
  189. }
  190. function getObjectCache(obj
  191. /*: ObjectIdentifier*/
  192. )
  193. /*: ObjectCache*/
  194. {
  195. const state = getState(obj);
  196. if (state) {
  197. return state.objectCache;
  198. }
  199. return {};
  200. }
  201. function estimateAttribute(obj
  202. /*: ObjectIdentifier*/
  203. , attr
  204. /*: string*/
  205. )
  206. /*: mixed*/
  207. {
  208. const serverData = getServerData(obj);
  209. const pendingOps = getPendingOps(obj);
  210. return ObjectStateMutations.estimateAttribute(serverData, pendingOps, obj.className, obj.id, attr);
  211. }
  212. function estimateAttributes(obj
  213. /*: ObjectIdentifier*/
  214. )
  215. /*: AttributeMap*/
  216. {
  217. const serverData = getServerData(obj);
  218. const pendingOps = getPendingOps(obj);
  219. return ObjectStateMutations.estimateAttributes(serverData, pendingOps, obj.className, obj.id);
  220. }
  221. function commitServerChanges(obj
  222. /*: ObjectIdentifier*/
  223. , changes
  224. /*: AttributeMap*/
  225. ) {
  226. const state = initializeState(obj);
  227. ObjectStateMutations.commitServerChanges(state.serverData, state.objectCache, changes);
  228. }
  229. function enqueueTask(obj
  230. /*: ObjectIdentifier*/
  231. , task
  232. /*: () => Promise*/
  233. )
  234. /*: Promise*/
  235. {
  236. const state = initializeState(obj);
  237. return state.tasks.enqueue(task);
  238. }
  239. function clearAllState() {
  240. objectState = {};
  241. }
  242. function duplicateState(source
  243. /*: { id: string }*/
  244. , dest
  245. /*: { id: string }*/
  246. ) {
  247. dest.id = source.id;
  248. }