unsavedChildren.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. function unsavedChildren(obj
  29. /*: ParseObject*/
  30. , allowDeepUnsaved
  31. /*:: ?: boolean*/
  32. )
  33. /*: Array<ParseFile | ParseObject>*/
  34. {
  35. const encountered = {
  36. objects: {},
  37. files: []
  38. };
  39. const identifier = obj.className + ':' + obj._getId();
  40. encountered.objects[identifier] = obj.dirty() ? obj : true;
  41. const attributes = obj.attributes;
  42. for (const attr in attributes) {
  43. if (typeof attributes[attr] === 'object') {
  44. traverse(attributes[attr], encountered, false, !!allowDeepUnsaved);
  45. }
  46. }
  47. const unsaved = [];
  48. for (const id in encountered.objects) {
  49. if (id !== identifier && encountered.objects[id] !== true) {
  50. unsaved.push(encountered.objects[id]);
  51. }
  52. }
  53. return unsaved.concat(encountered.files);
  54. }
  55. function traverse(obj
  56. /*: ParseObject*/
  57. , encountered
  58. /*: EncounterMap*/
  59. , shouldThrow
  60. /*: boolean*/
  61. , allowDeepUnsaved
  62. /*: boolean*/
  63. ) {
  64. if (obj instanceof _ParseObject.default) {
  65. if (!obj.id && shouldThrow) {
  66. throw new Error('Cannot create a pointer to an unsaved Object.');
  67. }
  68. const identifier = obj.className + ':' + obj._getId();
  69. if (!encountered.objects[identifier]) {
  70. encountered.objects[identifier] = obj.dirty() ? obj : true;
  71. const attributes = obj.attributes;
  72. for (const attr in attributes) {
  73. if (typeof attributes[attr] === 'object') {
  74. traverse(attributes[attr], encountered, !allowDeepUnsaved, allowDeepUnsaved);
  75. }
  76. }
  77. }
  78. return;
  79. }
  80. if (obj instanceof _ParseFile.default) {
  81. if (!obj.url() && encountered.files.indexOf(obj) < 0) {
  82. encountered.files.push(obj);
  83. }
  84. return;
  85. }
  86. if (obj instanceof _ParseRelation.default) {
  87. return;
  88. }
  89. if (Array.isArray(obj)) {
  90. obj.forEach(el => {
  91. if (typeof el === 'object') {
  92. traverse(el, encountered, shouldThrow, allowDeepUnsaved);
  93. }
  94. });
  95. }
  96. for (const k in obj) {
  97. if (typeof obj[k] === 'object') {
  98. traverse(obj[k], encountered, shouldThrow, allowDeepUnsaved);
  99. }
  100. }
  101. }