Skip to content

Commit

Permalink
fix: consistently use $__parent to store subdoc parent as a propert…
Browse files Browse the repository at this point in the history
…y, and `$parent()` as a getter function

Re: #10584
Re: #10414
  • Loading branch information
vkarpov15 committed Aug 24, 2021
1 parent 0921b59 commit efb3a7c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 27 deletions.
6 changes: 3 additions & 3 deletions lib/document.js
Expand Up @@ -3066,12 +3066,12 @@ Document.prototype.$__reset = function reset() {
}).
forEach(function(doc) {
doc.$__reset();
if (doc.$__parent() === _this) {
if (doc.$parent() === _this) {
_this.$__.activePaths.init(doc.$basePath);
} else if (doc.$__parent() != null && doc.$__parent().ownerDocument) {
} else if (doc.$parent() != null && doc.$parent().ownerDocument) {
// If map path underneath subdocument, may end up with a case where
// map path is modified but parent still needs to be reset. See gh-10295
doc.$__parent().$__reset();
doc.$parent().$__reset();
}
});

Expand Down
2 changes: 1 addition & 1 deletion lib/schema/SubdocumentPath.js
Expand Up @@ -63,7 +63,7 @@ function _createConstructor(schema, baseClass) {
const _embedded = function SingleNested(value, path, parent) {
const _this = this;

this.$parent = parent;
this.$__parent = parent;
Subdocument.apply(this, arguments);

this.$session(this.ownerDocument().$session());
Expand Down
2 changes: 1 addition & 1 deletion lib/schematype.js
Expand Up @@ -1068,7 +1068,7 @@ SchemaType.prototype.getDefault = function(scope, init) {

const casted = this.applySetters(ret, scope, init);
if (casted && casted.$isSingleNested) {
casted.$parent = scope;
casted.$__parent = scope;
}
return casted;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/types/ArraySubdocument.js
Expand Up @@ -32,7 +32,7 @@ function ArraySubdocument(obj, parentArr, skipId, fields, index) {
}
this.$setIndex(index);
this.$isDocumentArrayElement = true;
this.$parent = this[documentArrayParent];
this.$__parent = this[documentArrayParent];

// Document.call(this, obj, fields, skipId);
Subdocument.call(this, obj, fields, this[documentArrayParent], void 0, { isNew: true });
Expand Down Expand Up @@ -149,7 +149,7 @@ ArraySubdocument.prototype.$__pathRelativeToParent = function(path, skipIndex) {
* Returns this sub-documents parent document.
*/

ArraySubdocument.prototype.$__parent = function() {
ArraySubdocument.prototype.$parent = function() {
return this[documentArrayParent];
};

Expand Down
30 changes: 11 additions & 19 deletions lib/types/subdocument.js
Expand Up @@ -119,7 +119,7 @@ Subdocument.prototype.$__save = function(fn) {
*/

Subdocument.prototype.$isValid = function(path) {
const parent = this.$__parent();
const parent = this.$parent();
const fullPath = this.$__pathRelativeToParent(path);
if (parent != null && fullPath != null) {
return parent.$isValid(fullPath);
Expand All @@ -133,7 +133,7 @@ Subdocument.prototype.$isValid = function(path) {

Subdocument.prototype.markModified = function(path) {
Document.prototype.markModified.call(this, path);
const parent = this.$__parent();
const parent = this.$parent();
const fullPath = this.$__pathRelativeToParent(path);

if (parent == null || fullPath == null) {
Expand All @@ -144,15 +144,15 @@ Subdocument.prototype.markModified = function(path) {
if (parent.isDirectModified(myPath) || this.isNew) {
return;
}
this.$parent.markModified(fullPath, this);
this.$__parent.markModified(fullPath, this);
};

/*!
* ignore
*/

Subdocument.prototype.isModified = function(paths, modifiedPaths) {
const parent = this.$__parent();
const parent = this.$parent();
if (parent != null) {
if (Array.isArray(paths) || typeof paths === 'string') {
paths = (Array.isArray(paths) ? paths : paths.split(' '));
Expand All @@ -178,7 +178,7 @@ Subdocument.prototype.isModified = function(paths, modifiedPaths) {

Subdocument.prototype.$markValid = function(path) {
Document.prototype.$markValid.call(this, path);
const parent = this.$__parent();
const parent = this.$parent();
const fullPath = this.$__pathRelativeToParent(path);
if (parent != null && fullPath != null) {
parent.$markValid(fullPath);
Expand All @@ -192,7 +192,7 @@ Subdocument.prototype.$markValid = function(path) {
Subdocument.prototype.invalidate = function(path, err, val) {
Document.prototype.invalidate.call(this, path, err, val);

const parent = this.$__parent();
const parent = this.$parent();
const fullPath = this.$__pathRelativeToParent(path);
if (parent != null && fullPath != null) {
parent.invalidate(fullPath, err, val);
Expand All @@ -209,7 +209,7 @@ Subdocument.prototype.invalidate = function(path, err, val) {

Subdocument.prototype.$ignore = function(path) {
Document.prototype.$ignore.call(this, path);
const parent = this.$__parent();
const parent = this.$parent();
const fullPath = this.$__pathRelativeToParent(path);
if (parent != null && fullPath != null) {
parent.$ignore(fullPath);
Expand All @@ -232,11 +232,11 @@ Subdocument.prototype.ownerDocument = function() {
const paths = [];

while (true) {
if (typeof parent.$__parent !== 'function') {
if (typeof parent.$__pathRelativeToParent !== 'function') {
break;
}
paths.unshift(parent.$__pathRelativeToParent(void 0, true));
const _parent = parent.$__parent();
const _parent = parent.$parent();
if (_parent == null) {
break;
}
Expand All @@ -256,15 +256,7 @@ Subdocument.prototype.ownerDocument = function() {
*/

Subdocument.prototype.parent = function() {
return this.$__parent();
};

/*!
* Returns this sub-documents parent document.
*/

Subdocument.prototype.$__parent = function() {
return this.$parent;
return this.$__parent;
};

/**
Expand All @@ -287,7 +279,7 @@ Subdocument.prototype.$__remove = function(cb) {
};

Subdocument.prototype.$__removeFromParent = function() {
this.$parent.set(this.$basePath, null);
this.$__parent.set(this.$basePath, null);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion test/document.test.js
Expand Up @@ -3211,7 +3211,7 @@ describe('document', function() {
const Parent = db.model('Parent', ParentSchema);

const p = new Parent();
assert.equal(p.child.$parent, p);
assert.equal(p.child.$parent(), p);
});

it('removing parent doc calls remove hooks on subdocs (gh-2348) (gh-4566)', function(done) {
Expand Down

0 comments on commit efb3a7c

Please sign in to comment.