unsavedChildren.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = unsavedChildren;
  6. var _ParseFile = _interopRequireDefault(require("./ParseFile"));
  7. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  8. var _ParseRelation = _interopRequireDefault(require("./ParseRelation"));
  9. function _interopRequireDefault(obj) {
  10. return obj && obj.__esModule ? obj : {
  11. default: obj
  12. };
  13. }
  14. /**
  15. * Copyright (c) 2015-present, Parse, LLC.
  16. * All rights reserved.
  17. *
  18. * This source code is licensed under the BSD-style license found in the
  19. * LICENSE file in the root directory of this source tree. An additional grant
  20. * of patent rights can be found in the PATENTS file in the same directory.
  21. *
  22. * @flow
  23. */
  24. /**
  25. * Return an array of unsaved children, which are either Parse Objects or Files.
  26. * If it encounters any dirty Objects without Ids, it will throw an exception.
  27. *
  28. * @param {Parse.Object} obj
  29. * @param {boolean} allowDeepUnsaved
  30. * @returns {Array}
  31. */
  32. function unsavedChildren(obj
  33. /*: ParseObject*/
  34. , allowDeepUnsaved
  35. /*:: ?: boolean*/
  36. )
  37. /*: Array<ParseFile | ParseObject>*/
  38. {
  39. const encountered = {
  40. objects: {},
  41. files: []
  42. };
  43. const identifier = obj.className + ':' + obj._getId();
  44. encountered.objects[identifier] = obj.dirty() ? obj : true;
  45. const attributes = obj.attributes;
  46. for (const attr in attributes) {
  47. if (typeof attributes[attr] === 'object') {
  48. traverse(attributes[attr], encountered, false, !!allowDeepUnsaved);
  49. }
  50. }
  51. const unsaved = [];
  52. for (const id in encountered.objects) {
  53. if (id !== identifier && encountered.objects[id] !== true) {
  54. unsaved.push(encountered.objects[id]);
  55. }
  56. }
  57. return unsaved.concat(encountered.files);
  58. }
  59. function traverse(obj
  60. /*: ParseObject*/
  61. , encountered
  62. /*: EncounterMap*/
  63. , shouldThrow
  64. /*: boolean*/
  65. , allowDeepUnsaved
  66. /*: boolean*/
  67. ) {
  68. if (obj instanceof _ParseObject.default) {
  69. if (!obj.id && shouldThrow) {
  70. throw new Error('Cannot create a pointer to an unsaved Object.');
  71. }
  72. const identifier = obj.className + ':' + obj._getId();
  73. if (!encountered.objects[identifier]) {
  74. encountered.objects[identifier] = obj.dirty() ? obj : true;
  75. const attributes = obj.attributes;
  76. for (const attr in attributes) {
  77. if (typeof attributes[attr] === 'object') {
  78. traverse(attributes[attr], encountered, !allowDeepUnsaved, allowDeepUnsaved);
  79. }
  80. }
  81. }
  82. return;
  83. }
  84. if (obj instanceof _ParseFile.default) {
  85. if (!obj.url() && encountered.files.indexOf(obj) < 0) {
  86. encountered.files.push(obj);
  87. }
  88. return;
  89. }
  90. if (obj instanceof _ParseRelation.default) {
  91. return;
  92. }
  93. if (Array.isArray(obj)) {
  94. obj.forEach(el => {
  95. if (typeof el === 'object') {
  96. traverse(el, encountered, shouldThrow, allowDeepUnsaved);
  97. }
  98. });
  99. }
  100. for (const k in obj) {
  101. if (typeof obj[k] === 'object') {
  102. traverse(obj[k], encountered, shouldThrow, allowDeepUnsaved);
  103. }
  104. }
  105. }