Skip to content

Commit

Permalink
perf: avoid registering event listeners on subdocuments to reduce mem…
Browse files Browse the repository at this point in the history
…ory usage

Re: #11541
  • Loading branch information
vkarpov15 committed Apr 27, 2022
1 parent 2119e5a commit 1aa4c4e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3500,6 +3500,7 @@ function _setIsNew(doc, val) {
const subdocs = doc.$getAllSubdocs();
for (const subdoc of subdocs) {
subdoc.$isNew = val;
subdoc.$emit('isNew', val);
}
}

Expand Down
19 changes: 4 additions & 15 deletions lib/schema/SubdocumentPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,14 @@ function _createConstructor(schema, baseClass) {
Subdocument || (Subdocument = require('../types/subdocument'));

const _embedded = function SingleNested(value, path, parent) {
const _this = this;

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

this.$session(this.ownerDocument().$session());

if (parent) {
parent.$on('save', function() {
_this.emit('save', _this);
_this.constructor.emit('save', _this);
});

parent.$on('isNew', function(val) {
_this.isNew = val;
_this.emit('isNew', val);
_this.constructor.emit('isNew', val);
});
const ownerDocument = this.ownerDocument();
if (ownerDocument == null) {
return;
}
this.$session(ownerDocument.$session());
};

schema._preCompile();
Expand Down

0 comments on commit 1aa4c4e

Please sign in to comment.