Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(document): add $clone method #12549

Merged
merged 8 commits into from Oct 24, 2022
Merged

Conversation

lpizzinidev
Copy link
Contributor

closes #11849

Not sure if this is the best approach.
Anyway, open to feedback.

@Uzlopak
Copy link
Collaborator

Uzlopak commented Oct 17, 2022

Well I dont know if we want this feature. So we need feedback from @vkarpov15

But I know for sure, that we have to keep in mind that Object.assign does only a shallow Copy. So what happens with deep nested objects? We need tests for that case.


assert.deepEqual(doc, clonedDoc);
assert.deepEqual(doc._doc, clonedDoc._doc);
assert.deepEqual(doc.$__, clonedDoc.$__);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to add some extra test cases to make sure this $clone() method works. For example:

  1. Modify a field in clonedDoc. Make sure that the field is modified in clonedDoc, but not in doc.
  2. Save clonedDoc. Make sure that doesn't affect modified paths on doc.
  3. Add a value that fails validation to doc. Run await doc.validate(). Then create a new cloned doc clonedDoc = doc.$clone(), fix the invalid value, run await clonedDoc.validate(). Make sure the path is still invalid in clonedDoc.
  4. Set a session on clonedDoc, make sure that doesn't affect the original doc.

lib/document.js Outdated
clonedDoc._doc = clone(this._doc);
}
if (this.$__) {
clonedDoc.$__ = Object.assign(Object.create(Object.getPrototypeOf(this.$__)), this.$__);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would need to do a deep clone on $__, that may contain deeply nested properties.

lib/document.js Outdated
*/

Document.prototype.$clone = function() {
const clonedDoc = Object.assign(Object.create(Object.getPrototypeOf(this)), this);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't like using Object.create() like this, because Object.create() skips running the Document constructor. I'd try just using const Model = this.constructor; const clonedDoc = new Model();

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍 I'll merge this into 6.8

@vkarpov15 vkarpov15 changed the base branch from master to 6.8 October 24, 2022 23:47
@vkarpov15 vkarpov15 merged commit 00aa878 into Automattic:6.8 Oct 24, 2022
@vkarpov15 vkarpov15 added this to the 6.8 milestone Oct 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document.prototype.$clone()
3 participants