2016-04-23 01:14:43 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
describe("Zotero.Notifier", function () {
|
|
|
|
describe("#trigger()", function () {
|
|
|
|
it("should trigger add events before modify events", function* () {
|
|
|
|
var deferred = Zotero.Promise.defer();
|
|
|
|
var events = [];
|
|
|
|
var observer = {
|
|
|
|
notify: (action, type, ids) => {
|
|
|
|
events.push(action);
|
|
|
|
if (events.length == 2) {
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var id = Zotero.Notifier.registerObserver(observer, null, 'test_trigger');
|
|
|
|
|
|
|
|
yield Zotero.DB.executeTransaction(function* () {
|
|
|
|
var item = new Zotero.Item('book');
|
|
|
|
item.setField('title', 'A');
|
|
|
|
yield item.save();
|
|
|
|
item.setField('title', 'B');
|
|
|
|
yield item.save();
|
|
|
|
|
|
|
|
Zotero.Notifier.queue('unknown', 'item', item.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.isTrue(deferred.promise.isResolved());
|
|
|
|
assert.lengthOf(events, 3);
|
|
|
|
assert.equal(events[0], 'add');
|
|
|
|
assert.equal(events[1], 'modify');
|
|
|
|
assert.equal(events[2], 'unknown');
|
|
|
|
|
|
|
|
Zotero.Notifier.unregisterObserver(id);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should add events to passed queue", function* () {
|
|
|
|
var collection = yield createDataObject('collection');
|
|
|
|
|
|
|
|
var deferred = Zotero.Promise.defer();
|
|
|
|
var observer = {
|
|
|
|
notify: () => deferred.resolve()
|
|
|
|
};
|
|
|
|
var id = Zotero.Notifier.registerObserver(observer, null, 'test_trigger');
|
|
|
|
|
|
|
|
var queue = new Zotero.Notifier.Queue;
|
|
|
|
var item = createUnsavedDataObject('item');
|
|
|
|
item.setCollections([collection.id]);
|
|
|
|
yield item.saveTx({
|
|
|
|
notifierQueue: queue
|
|
|
|
});
|
|
|
|
assert.isTrue(deferred.promise.isPending());
|
|
|
|
assert.equal(queue.size, 2);
|
|
|
|
yield Zotero.Notifier.commit(queue);
|
|
|
|
assert.isTrue(deferred.promise.isResolved());
|
|
|
|
|
|
|
|
Zotero.Notifier.unregisterObserver(id);
|
|
|
|
});
|
|
|
|
});
|
2018-02-08 07:07:44 +00:00
|
|
|
|
|
|
|
describe("#queue", function () {
|
|
|
|
it("should handle notification after DB timeout from another transaction", async function () {
|
|
|
|
var promise1 = Zotero.DB.executeTransaction(async function () {
|
|
|
|
var item = createUnsavedDataObject('item');
|
|
|
|
await item.save();
|
|
|
|
|
|
|
|
await Zotero.Promise.delay(2000);
|
|
|
|
|
|
|
|
Zotero.Notifier.queue('refresh', 'item', item.id);
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
var promise2 = Zotero.DB.executeTransaction(async function () {
|
|
|
|
var item = createUnsavedDataObject('item');
|
|
|
|
await item.save();
|
|
|
|
}.bind(this), { waitTimeout: 1000 });
|
|
|
|
|
|
|
|
await promise1;
|
2022-02-22 00:20:56 +00:00
|
|
|
assert.ok(await getPromiseError(promise2));
|
2018-02-08 07:07:44 +00:00
|
|
|
});
|
|
|
|
});
|
2021-05-14 07:26:14 +00:00
|
|
|
|
|
|
|
describe("Queue", function () {
|
|
|
|
describe("#commit()", function () {
|
|
|
|
it("should add options from queue to extraData", async function () {
|
|
|
|
var called = false;
|
|
|
|
var data;
|
|
|
|
|
|
|
|
var notifierID = Zotero.Notifier.registerObserver({
|
|
|
|
notify: (event, type, ids, extraData) => {
|
|
|
|
called = true;
|
|
|
|
data = extraData;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var notifierQueue = new Zotero.Notifier.Queue({
|
|
|
|
skipAutoSync: true
|
|
|
|
});
|
|
|
|
|
|
|
|
var item = createUnsavedDataObject('item');
|
|
|
|
await item.saveTx({
|
|
|
|
notifierQueue
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.isFalse(called);
|
|
|
|
|
|
|
|
await Zotero.Notifier.commit(notifierQueue);
|
|
|
|
|
|
|
|
assert.isTrue(called);
|
|
|
|
assert.propertyVal(data, 'skipAutoSync', true);
|
|
|
|
|
|
|
|
Zotero.Notifier.unregisterObserver(notifierID);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-04-23 01:14:43 +00:00
|
|
|
});
|