Skip to content

Commit

Permalink
chore(crud): Optimise bulk update previews (#5237)
Browse files Browse the repository at this point in the history
* profile what happens when we update the previews

* memoize the Document component

* remove some logging

* remove commented code

* remove profiling

* change assertion

* change assertion

* fix assertions
  • Loading branch information
lerouxb authored Dec 15, 2023
1 parent 78d598a commit e934571
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useContext, createContext } from 'react';
import React, { useState, useContext, createContext, useMemo } from 'react';
import { type Document } from 'bson';
import TypeChecker from 'hadron-type-checker';

Expand Down Expand Up @@ -569,7 +569,7 @@ export function ChangeView({
before: Document;
after: Document;
}) {
const obj = unifyDocuments(before, after);
const obj = useMemo(() => unifyDocuments(before, after), [before, after]);

const darkMode = useDarkMode();

Expand Down
2 changes: 1 addition & 1 deletion packages/compass-crud/src/components/document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ Document.propTypes = {
isExpanded: PropTypes.bool,
};

export default Document;
export default React.memo(Document);
22 changes: 14 additions & 8 deletions packages/compass-crud/src/stores/crud-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1127,14 +1127,6 @@ class CrudStoreImpl
this.state.bulkUpdate.previewAbortController.abort();
}

// immediately persist the state before any other state changes
this.setState({
bulkUpdate: {
...this.state.bulkUpdate,
updateText,
},
});

// Don't try and calculate the update preview if we know it won't work. Just
// see if the update will parse.
if (!this.state.isUpdatePreviewSupported) {
Expand All @@ -1144,6 +1136,7 @@ class CrudStoreImpl
this.setState({
bulkUpdate: {
...this.state.bulkUpdate,
updateText,
preview: {
changes: [],
},
Expand All @@ -1159,6 +1152,7 @@ class CrudStoreImpl
this.setState({
bulkUpdate: {
...this.state.bulkUpdate,
updateText,
preview: {
changes: [],
},
Expand All @@ -1173,6 +1167,8 @@ class CrudStoreImpl

const abortController = new AbortController();

// set the abort controller in the state before we start doing anything so
// that other calls can see it
this.setState({
bulkUpdate: {
...this.state.bulkUpdate,
Expand All @@ -1192,6 +1188,7 @@ class CrudStoreImpl
this.setState({
bulkUpdate: {
...this.state.bulkUpdate,
updateText,
preview: {
changes: [],
},
Expand All @@ -1200,6 +1197,12 @@ class CrudStoreImpl
previewAbortController: undefined,
},
});

return;
}

if (abortController.signal.aborted) {
// don't kick off an expensive query if we're already aborted anyway
return;
}

Expand All @@ -1221,6 +1224,7 @@ class CrudStoreImpl
this.setState({
bulkUpdate: {
...this.state.bulkUpdate,
updateText,
preview: {
changes: [],
},
Expand All @@ -1229,6 +1233,7 @@ class CrudStoreImpl
previewAbortController: undefined,
},
});

return;
}

Expand All @@ -1240,6 +1245,7 @@ class CrudStoreImpl
this.setState({
bulkUpdate: {
...this.state.bulkUpdate,
updateText,
preview,
serverError: undefined,
syntaxError: undefined,
Expand Down
11 changes: 4 additions & 7 deletions packages/compass-e2e-tests/tests/collection-bulk-update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('Bulk Update', () => {
// Check that the modal starts with the default update text
expect(
await browser.getCodemirrorEditorText(Selectors.BulkUpdateUpdate)
).to.equal('{\n $set: {\n\n },\n}');
).to.match(/{\s+\$set:\s+{\s+},?\s+}/);

// Change the update text
await browser.setCodemirrorEditorValue(
Expand Down Expand Up @@ -198,12 +198,9 @@ describe('Bulk Update', () => {
).to.equal('{ i: { $gt: 5 } }');

// Check that the modal starts with the expected update text
expect(await browser.getCodemirrorEditorText(Selectors.BulkUpdateUpdate)).to
.equal(`{
$set: {
k: 0
}
}`);
expect(
await browser.getCodemirrorEditorText(Selectors.BulkUpdateUpdate)
).to.equal(`{ $set: { k: 0 } }`);
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { mount, shallow } from 'enzyme';
import { mount } from 'enzyme';
import { expect } from 'chai';

import DocumentPreview from '.';
Expand All @@ -21,10 +21,10 @@ describe('DocumentPreview [Component]', function () {

context('when document loading state is success', function () {
it('renders a document if there is one present', function () {
const component = shallow(
const component = mount(
<DocumentPreview loadingState="success" document={{}} />
);
expect(component.find('Document')).to.exist;
expect(component.find('HadronDocument')).to.exist;
});

it('renders a no preview text when there is no document', function () {
Expand Down

0 comments on commit e934571

Please sign in to comment.