Skip to content

Keep sort position of observed docs consistent with real sorting and only use observed docs to publish changes in sort position #623

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions packages/easysearch:core/lib/core/search-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,17 @@ class SearchCollection {
if (observedDocs.map(d => d.__sortPosition).includes(atIndex)) {
observedDocs = observedDocs.map((doc, docIndex) => {
if (doc.__sortPosition >= atIndex) {
const newSortPosition = doc.__sortPosition + 1;
doc = collectionScope.addCustomFields(doc, {
sortPosition: doc.__sortPosition + 1,
sortPosition: newSortPosition,
});

// do not throw changed event on last doc as it will be removed from cursor
if (docIndex < observedDocs.length) {
this.changed(
collectionName,
collectionScope.generateId(doc),
doc
{__sortPosition: newSortPosition}
);
}
}
Expand All @@ -261,19 +262,35 @@ class SearchCollection {
movedTo: (doc, fromIndex, toIndex, before) => {
doc = collectionScope.engine.config.beforePublish('movedTo', doc, fromIndex, toIndex, before);
doc = updateDocWithCustomFields(doc, toIndex);
this.changed(collectionName, collectionScope.generateId(doc), doc);

let beforeDoc = collectionScope._indexConfiguration.collection.findOne(before);
let start = fromIndex, end = toIndex, sortIncrement = -1;
Copy link
Owner

Choose a reason for hiding this comment

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

please use const

if (toIndex < fromIndex) {
start = toIndex, end = fromIndex, sortIncrement = 1;
Copy link
Owner

Choose a reason for hiding this comment

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

spread this out onto several lines

}

if (beforeDoc) {
beforeDoc = collectionScope.addCustomFields(beforeDoc, {
searchDefinition: definitionString,
searchOptions: optionsString,
sortPosition: fromIndex
});
this.changed(collectionName, collectionScope.generateId(beforeDoc), beforeDoc);
const sortPositions = observedDocs.map(d => d.__sortPosition);
if (!(sortPositions.includes(start) || sortPositions.includes(end))) {
return;
}

this.changed(collectionName, collectionScope.generateId(doc), doc);
observedDocs = observedDocs.map(observedDoc => {
if (observedDoc.__sortPosition >= start && observedDoc.__sortPosition <= end) {
const isMovedDoc = doc._id === observedDoc._id;
const newSortPosition = isMovedDoc ? toIndex : observedDoc.__sortPosition + sortIncrement;
observedDoc = collectionScope.addCustomFields(observedDoc, {
sortPosition: newSortPosition,
});

!isMovedDoc && this.changed(
collectionName,
collectionScope.generateId(observedDoc),
{__sortPosition: newSortPosition}
);
}

return observedDoc;
});
},
removedAt: (doc, atIndex) => {
doc = collectionScope.engine.config.beforePublish('removedAt', doc, atIndex);
Expand Down Expand Up @@ -311,3 +328,4 @@ class SearchCollection {
}

export default SearchCollection;