SingleInstanceStateController.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * Copyright (c) 2015-present, Parse, LLC.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. * @flow
  10. */
  11. import * as ObjectStateMutations from './ObjectStateMutations';
  12. /*:: import type { Op } from './ParseOp';*/
  13. /*:: import type { AttributeMap, ObjectCache, OpsMap, State } from './ObjectStateMutations';*/
  14. /*:: type ObjectIdentifier = {
  15. className: string;
  16. id: string
  17. };*/
  18. let objectState
  19. /*: {
  20. [className: string]: {
  21. [id: string]: State
  22. }
  23. }*/
  24. = {};
  25. export function getState(obj
  26. /*: ObjectIdentifier*/
  27. )
  28. /*: ?State*/
  29. {
  30. const classData = objectState[obj.className];
  31. if (classData) {
  32. return classData[obj.id] || null;
  33. }
  34. return null;
  35. }
  36. export function initializeState(obj
  37. /*: ObjectIdentifier*/
  38. , initial
  39. /*:: ?: State*/
  40. )
  41. /*: State*/
  42. {
  43. let state = getState(obj);
  44. if (state) {
  45. return state;
  46. }
  47. if (!objectState[obj.className]) {
  48. objectState[obj.className] = {};
  49. }
  50. if (!initial) {
  51. initial = ObjectStateMutations.defaultState();
  52. }
  53. state = objectState[obj.className][obj.id] = initial;
  54. return state;
  55. }
  56. export function removeState(obj
  57. /*: ObjectIdentifier*/
  58. )
  59. /*: ?State*/
  60. {
  61. const state = getState(obj);
  62. if (state === null) {
  63. return null;
  64. }
  65. delete objectState[obj.className][obj.id];
  66. return state;
  67. }
  68. export function getServerData(obj
  69. /*: ObjectIdentifier*/
  70. )
  71. /*: AttributeMap*/
  72. {
  73. const state = getState(obj);
  74. if (state) {
  75. return state.serverData;
  76. }
  77. return {};
  78. }
  79. export function setServerData(obj
  80. /*: ObjectIdentifier*/
  81. , attributes
  82. /*: AttributeMap*/
  83. ) {
  84. const serverData = initializeState(obj).serverData;
  85. ObjectStateMutations.setServerData(serverData, attributes);
  86. }
  87. export function getPendingOps(obj
  88. /*: ObjectIdentifier*/
  89. )
  90. /*: Array<OpsMap>*/
  91. {
  92. const state = getState(obj);
  93. if (state) {
  94. return state.pendingOps;
  95. }
  96. return [{}];
  97. }
  98. export function setPendingOp(obj
  99. /*: ObjectIdentifier*/
  100. , attr
  101. /*: string*/
  102. , op
  103. /*: ?Op*/
  104. ) {
  105. const pendingOps = initializeState(obj).pendingOps;
  106. ObjectStateMutations.setPendingOp(pendingOps, attr, op);
  107. }
  108. export function pushPendingState(obj
  109. /*: ObjectIdentifier*/
  110. ) {
  111. const pendingOps = initializeState(obj).pendingOps;
  112. ObjectStateMutations.pushPendingState(pendingOps);
  113. }
  114. export function popPendingState(obj
  115. /*: ObjectIdentifier*/
  116. )
  117. /*: OpsMap*/
  118. {
  119. const pendingOps = initializeState(obj).pendingOps;
  120. return ObjectStateMutations.popPendingState(pendingOps);
  121. }
  122. export function mergeFirstPendingState(obj
  123. /*: ObjectIdentifier*/
  124. ) {
  125. const pendingOps = getPendingOps(obj);
  126. ObjectStateMutations.mergeFirstPendingState(pendingOps);
  127. }
  128. export function getObjectCache(obj
  129. /*: ObjectIdentifier*/
  130. )
  131. /*: ObjectCache*/
  132. {
  133. const state = getState(obj);
  134. if (state) {
  135. return state.objectCache;
  136. }
  137. return {};
  138. }
  139. export function estimateAttribute(obj
  140. /*: ObjectIdentifier*/
  141. , attr
  142. /*: string*/
  143. )
  144. /*: mixed*/
  145. {
  146. const serverData = getServerData(obj);
  147. const pendingOps = getPendingOps(obj);
  148. return ObjectStateMutations.estimateAttribute(serverData, pendingOps, obj.className, obj.id, attr);
  149. }
  150. export function estimateAttributes(obj
  151. /*: ObjectIdentifier*/
  152. )
  153. /*: AttributeMap*/
  154. {
  155. const serverData = getServerData(obj);
  156. const pendingOps = getPendingOps(obj);
  157. return ObjectStateMutations.estimateAttributes(serverData, pendingOps, obj.className, obj.id);
  158. }
  159. export function commitServerChanges(obj
  160. /*: ObjectIdentifier*/
  161. , changes
  162. /*: AttributeMap*/
  163. ) {
  164. const state = initializeState(obj);
  165. ObjectStateMutations.commitServerChanges(state.serverData, state.objectCache, changes);
  166. }
  167. export function enqueueTask(obj
  168. /*: ObjectIdentifier*/
  169. , task
  170. /*: () => Promise*/
  171. )
  172. /*: Promise*/
  173. {
  174. const state = initializeState(obj);
  175. return state.tasks.enqueue(task);
  176. }
  177. export function clearAllState() {
  178. objectState = {};
  179. }
  180. export function duplicateState(source
  181. /*: {id: string}*/
  182. , dest
  183. /*: {id: string}*/
  184. ) {
  185. dest.id = source.id;
  186. }