ObjectStateMutations.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.commitServerChanges = commitServerChanges;
  6. exports.defaultState = defaultState;
  7. exports.estimateAttribute = estimateAttribute;
  8. exports.estimateAttributes = estimateAttributes;
  9. exports.mergeFirstPendingState = mergeFirstPendingState;
  10. exports.popPendingState = popPendingState;
  11. exports.pushPendingState = pushPendingState;
  12. exports.setPendingOp = setPendingOp;
  13. exports.setServerData = setServerData;
  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. function ownKeys(object, enumerableOnly) {
  26. var keys = Object.keys(object);
  27. if (Object.getOwnPropertySymbols) {
  28. var symbols = Object.getOwnPropertySymbols(object);
  29. enumerableOnly && (symbols = symbols.filter(function (sym) {
  30. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  31. })), keys.push.apply(keys, symbols);
  32. }
  33. return keys;
  34. }
  35. function _objectSpread(target) {
  36. for (var i = 1; i < arguments.length; i++) {
  37. var source = null != arguments[i] ? arguments[i] : {};
  38. i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
  39. _defineProperty(target, key, source[key]);
  40. }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
  41. Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
  42. });
  43. }
  44. return target;
  45. }
  46. function _defineProperty(obj, key, value) {
  47. if (key in obj) {
  48. Object.defineProperty(obj, key, {
  49. value: value,
  50. enumerable: true,
  51. configurable: true,
  52. writable: true
  53. });
  54. } else {
  55. obj[key] = value;
  56. }
  57. return obj;
  58. }
  59. function defaultState()
  60. /*: State*/
  61. {
  62. return {
  63. serverData: {},
  64. pendingOps: [{}],
  65. objectCache: {},
  66. tasks: new _TaskQueue.default(),
  67. existed: false
  68. };
  69. }
  70. function setServerData(serverData
  71. /*: AttributeMap*/
  72. , attributes
  73. /*: AttributeMap*/
  74. ) {
  75. for (const attr in attributes) {
  76. if (typeof attributes[attr] !== 'undefined') {
  77. serverData[attr] = attributes[attr];
  78. } else {
  79. delete serverData[attr];
  80. }
  81. }
  82. }
  83. function setPendingOp(pendingOps
  84. /*: Array<OpsMap>*/
  85. , attr
  86. /*: string*/
  87. , op
  88. /*: ?Op*/
  89. ) {
  90. const last = pendingOps.length - 1;
  91. if (op) {
  92. pendingOps[last][attr] = op;
  93. } else {
  94. delete pendingOps[last][attr];
  95. }
  96. }
  97. function pushPendingState(pendingOps
  98. /*: Array<OpsMap>*/
  99. ) {
  100. pendingOps.push({});
  101. }
  102. function popPendingState(pendingOps
  103. /*: Array<OpsMap>*/
  104. )
  105. /*: OpsMap*/
  106. {
  107. const first = pendingOps.shift();
  108. if (!pendingOps.length) {
  109. pendingOps[0] = {};
  110. }
  111. return first;
  112. }
  113. function mergeFirstPendingState(pendingOps
  114. /*: Array<OpsMap>*/
  115. ) {
  116. const first = popPendingState(pendingOps);
  117. const next = pendingOps[0];
  118. for (const attr in first) {
  119. if (next[attr] && first[attr]) {
  120. const merged = next[attr].mergeWith(first[attr]);
  121. if (merged) {
  122. next[attr] = merged;
  123. }
  124. } else {
  125. next[attr] = first[attr];
  126. }
  127. }
  128. }
  129. function estimateAttribute(serverData
  130. /*: AttributeMap*/
  131. , pendingOps
  132. /*: Array<OpsMap>*/
  133. , className
  134. /*: string*/
  135. , id
  136. /*: ?string*/
  137. , attr
  138. /*: string*/
  139. )
  140. /*: mixed*/
  141. {
  142. let value = serverData[attr];
  143. for (let i = 0; i < pendingOps.length; i++) {
  144. if (pendingOps[i][attr]) {
  145. if (pendingOps[i][attr] instanceof _ParseOp.RelationOp) {
  146. if (id) {
  147. value = pendingOps[i][attr].applyTo(value, {
  148. className: className,
  149. id: id
  150. }, attr);
  151. }
  152. } else {
  153. value = pendingOps[i][attr].applyTo(value);
  154. }
  155. }
  156. }
  157. return value;
  158. }
  159. function estimateAttributes(serverData
  160. /*: AttributeMap*/
  161. , pendingOps
  162. /*: Array<OpsMap>*/
  163. , className
  164. /*: string*/
  165. , id
  166. /*: ?string*/
  167. )
  168. /*: AttributeMap*/
  169. {
  170. const data = {};
  171. for (var attr in serverData) {
  172. data[attr] = serverData[attr];
  173. }
  174. for (let i = 0; i < pendingOps.length; i++) {
  175. for (attr in pendingOps[i]) {
  176. if (pendingOps[i][attr] instanceof _ParseOp.RelationOp) {
  177. if (id) {
  178. data[attr] = pendingOps[i][attr].applyTo(data[attr], {
  179. className: className,
  180. id: id
  181. }, attr);
  182. }
  183. } else {
  184. if (attr.includes('.')) {
  185. // convert a.b.c into { a: { b: { c: value } } }
  186. const fields = attr.split('.');
  187. const first = fields[0];
  188. const last = fields[fields.length - 1];
  189. data[first] = _objectSpread({}, serverData[first]);
  190. let object = _objectSpread({}, data);
  191. for (let i = 0; i < fields.length - 1; i++) {
  192. object = object[fields[i]];
  193. }
  194. object[last] = pendingOps[i][attr].applyTo(object[last]);
  195. } else {
  196. data[attr] = pendingOps[i][attr].applyTo(data[attr]);
  197. }
  198. }
  199. }
  200. }
  201. return data;
  202. }
  203. function commitServerChanges(serverData
  204. /*: AttributeMap*/
  205. , objectCache
  206. /*: ObjectCache*/
  207. , changes
  208. /*: AttributeMap*/
  209. ) {
  210. for (const attr in changes) {
  211. const val = changes[attr];
  212. serverData[attr] = val;
  213. if (val && typeof val === 'object' && !(val instanceof _ParseObject.default) && !(val instanceof _ParseFile.default) && !(val instanceof _ParseRelation.default)) {
  214. const json = (0, _encode.default)(val, false, true);
  215. objectCache[attr] = JSON.stringify(json);
  216. }
  217. }
  218. }