ObjectStateMutations.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.defaultState = defaultState;
  6. exports.setServerData = setServerData;
  7. exports.setPendingOp = setPendingOp;
  8. exports.pushPendingState = pushPendingState;
  9. exports.popPendingState = popPendingState;
  10. exports.mergeFirstPendingState = mergeFirstPendingState;
  11. exports.estimateAttribute = estimateAttribute;
  12. exports.estimateAttributes = estimateAttributes;
  13. exports.commitServerChanges = commitServerChanges;
  14. var _encode = _interopRequireDefault(require("./encode"));
  15. var _ParseFile = _interopRequireDefault(require("./ParseFile"));
  16. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  17. var _ParseRelation = _interopRequireDefault(require("./ParseRelation"));
  18. var _TaskQueue = _interopRequireDefault(require("./TaskQueue"));
  19. var _ParseOp = require("./ParseOp");
  20. function _interopRequireDefault(obj) {
  21. return obj && obj.__esModule ? obj : {
  22. default: obj
  23. };
  24. }
  25. /**
  26. * Copyright (c) 2015-present, Parse, LLC.
  27. * All rights reserved.
  28. *
  29. * This source code is licensed under the BSD-style license found in the
  30. * LICENSE file in the root directory of this source tree. An additional grant
  31. * of patent rights can be found in the PATENTS file in the same directory.
  32. *
  33. * @flow
  34. */
  35. function defaultState()
  36. /*: State*/
  37. {
  38. return {
  39. serverData: {},
  40. pendingOps: [{}],
  41. objectCache: {},
  42. tasks: new _TaskQueue.default(),
  43. existed: false
  44. };
  45. }
  46. function setServerData(serverData
  47. /*: AttributeMap*/
  48. , attributes
  49. /*: AttributeMap*/
  50. ) {
  51. for (const attr in attributes) {
  52. if (typeof attributes[attr] !== 'undefined') {
  53. serverData[attr] = attributes[attr];
  54. } else {
  55. delete serverData[attr];
  56. }
  57. }
  58. }
  59. function setPendingOp(pendingOps
  60. /*: Array<OpsMap>*/
  61. , attr
  62. /*: string*/
  63. , op
  64. /*: ?Op*/
  65. ) {
  66. const last = pendingOps.length - 1;
  67. if (op) {
  68. pendingOps[last][attr] = op;
  69. } else {
  70. delete pendingOps[last][attr];
  71. }
  72. }
  73. function pushPendingState(pendingOps
  74. /*: Array<OpsMap>*/
  75. ) {
  76. pendingOps.push({});
  77. }
  78. function popPendingState(pendingOps
  79. /*: Array<OpsMap>*/
  80. )
  81. /*: OpsMap*/
  82. {
  83. const first = pendingOps.shift();
  84. if (!pendingOps.length) {
  85. pendingOps[0] = {};
  86. }
  87. return first;
  88. }
  89. function mergeFirstPendingState(pendingOps
  90. /*: Array<OpsMap>*/
  91. ) {
  92. const first = popPendingState(pendingOps);
  93. const next = pendingOps[0];
  94. for (const attr in first) {
  95. if (next[attr] && first[attr]) {
  96. const merged = next[attr].mergeWith(first[attr]);
  97. if (merged) {
  98. next[attr] = merged;
  99. }
  100. } else {
  101. next[attr] = first[attr];
  102. }
  103. }
  104. }
  105. function estimateAttribute(serverData
  106. /*: AttributeMap*/
  107. , pendingOps
  108. /*: Array<OpsMap>*/
  109. , className
  110. /*: string*/
  111. , id
  112. /*: ?string*/
  113. , attr
  114. /*: string*/
  115. )
  116. /*: mixed*/
  117. {
  118. let value = serverData[attr];
  119. for (let i = 0; i < pendingOps.length; i++) {
  120. if (pendingOps[i][attr]) {
  121. if (pendingOps[i][attr] instanceof _ParseOp.RelationOp) {
  122. if (id) {
  123. value = pendingOps[i][attr].applyTo(value, {
  124. className: className,
  125. id: id
  126. }, attr);
  127. }
  128. } else {
  129. value = pendingOps[i][attr].applyTo(value);
  130. }
  131. }
  132. }
  133. return value;
  134. }
  135. function estimateAttributes(serverData
  136. /*: AttributeMap*/
  137. , pendingOps
  138. /*: Array<OpsMap>*/
  139. , className
  140. /*: string*/
  141. , id
  142. /*: ?string*/
  143. )
  144. /*: AttributeMap*/
  145. {
  146. const data = {};
  147. for (var attr in serverData) {
  148. data[attr] = serverData[attr];
  149. }
  150. for (let i = 0; i < pendingOps.length; i++) {
  151. for (attr in pendingOps[i]) {
  152. if (pendingOps[i][attr] instanceof _ParseOp.RelationOp) {
  153. if (id) {
  154. data[attr] = pendingOps[i][attr].applyTo(data[attr], {
  155. className: className,
  156. id: id
  157. }, attr);
  158. }
  159. } else {
  160. if (attr.includes('.')) {
  161. // convert a.b.c into { a: { b: { c: value } } }
  162. const fields = attr.split('.');
  163. const last = fields[fields.length - 1];
  164. let object = Object.assign({}, data);
  165. for (let i = 0; i < fields.length - 1; i++) {
  166. object = object[fields[i]];
  167. }
  168. object[last] = pendingOps[i][attr].applyTo(object[last]);
  169. } else {
  170. data[attr] = pendingOps[i][attr].applyTo(data[attr]);
  171. }
  172. }
  173. }
  174. }
  175. return data;
  176. }
  177. function commitServerChanges(serverData
  178. /*: AttributeMap*/
  179. , objectCache
  180. /*: ObjectCache*/
  181. , changes
  182. /*: AttributeMap*/
  183. ) {
  184. for (const attr in changes) {
  185. const val = changes[attr];
  186. serverData[attr] = val;
  187. if (val && typeof val === 'object' && !(val instanceof _ParseObject.default) && !(val instanceof _ParseFile.default) && !(val instanceof _ParseRelation.default)) {
  188. const json = (0, _encode.default)(val, false, true);
  189. objectCache[attr] = JSON.stringify(json);
  190. }
  191. }
  192. }