Auto-sync after setting change

This commit is contained in:
Dan Stillman 2021-02-20 04:25:28 -05:00
parent f52c50f055
commit 73ba5f9ffe
2 changed files with 38 additions and 9 deletions

View file

@ -124,24 +124,39 @@ Zotero.Sync.EventListeners.AutoSyncListener = {
}
// Only trigger sync for certain types
//
// TODO: settings, full text
if (!Zotero.DataObjectUtilities.getTypes().includes(type)) {
// TODO: full text
if (![...Zotero.DataObjectUtilities.getTypes(), 'setting'].includes(type)) {
return;
}
// Determine affected libraries so only those can be synced
let libraries = [];
let objectsClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType(type);
ids.forEach(id => {
let lk = objectsClass.getLibraryAndKeyFromID(id);
if (lk) {
let library = Zotero.Libraries.get(lk.libraryID);
if (type == 'setting') {
for (let id of ids) {
// E.g., '1/lastPageIndex_u_ABCD2345'
let libraryID = parseInt(id.split('/')[0]);
let library = Zotero.Libraries.get(libraryID);
if (library.syncable) {
libraries.push(library);
}
}
});
}
else if (Zotero.DataObjectUtilities.getTypes().includes(type)) {
let objectsClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType(type);
ids.forEach(id => {
let lk = objectsClass.getLibraryAndKeyFromID(id);
if (lk) {
let library = Zotero.Libraries.get(lk.libraryID);
if (library.syncable) {
libraries.push(library);
}
}
});
}
else {
return;
}
libraries = Zotero.Sync.Data.Local.filterSkippedLibraries(libraries);
if (!libraries.length) {

View file

@ -77,5 +77,19 @@ describe("Zotero.Sync.EventListeners", function () {
yield Zotero.Promise.delay(10);
mock.verify();
});
it("should auto-sync after settings change", async function () {
var attachment = await importFileAttachment('test.pdf');
var mock = sinon.mock(Zotero.Sync.Runner);
var expectation = mock.expects("setSyncTimeout").once();
// Create setting (e.g., lastPageIndex_u_ABCD2345)
await attachment.setAttachmentLastPageIndex(1);
await Zotero.Promise.delay(10);
mock.verify();
assert.sameMembers(expectation.getCall(0).args[2].libraries, [Zotero.Libraries.userLibraryID]);
});
});
});