Basic auto-sync support -- timer reset to 15 seconds after each data operation
This commit is contained in:
parent
e723758346
commit
0e0e4aa2b1
|
@ -367,6 +367,7 @@ Zotero.Sync.EventListener = new function () {
|
||||||
* Methods for syncing with the Zotero Server
|
* Methods for syncing with the Zotero Server
|
||||||
*/
|
*/
|
||||||
Zotero.Sync.Server = new function () {
|
Zotero.Sync.Server = new function () {
|
||||||
|
this.init = init;
|
||||||
this.login = login;
|
this.login = login;
|
||||||
this.sync = sync;
|
this.sync = sync;
|
||||||
this.lock = lock;
|
this.lock = lock;
|
||||||
|
@ -375,6 +376,8 @@ Zotero.Sync.Server = new function () {
|
||||||
this.resetServer = resetServer;
|
this.resetServer = resetServer;
|
||||||
this.resetClient = resetClient;
|
this.resetClient = resetClient;
|
||||||
this.logout = logout;
|
this.logout = logout;
|
||||||
|
this.setSyncTimeout = setSyncTimeout;
|
||||||
|
this.clearSyncTimeout = clearSyncTimeout;
|
||||||
|
|
||||||
this.__defineGetter__('username', function () {
|
this.__defineGetter__('username', function () {
|
||||||
return Zotero.Prefs.get('sync.server.username');
|
return Zotero.Prefs.get('sync.server.username');
|
||||||
|
@ -466,11 +469,17 @@ Zotero.Sync.Server = new function () {
|
||||||
var _attempts = _maxAttempts;
|
var _attempts = _maxAttempts;
|
||||||
var _syncInProgress;
|
var _syncInProgress;
|
||||||
|
|
||||||
|
var _autoSyncTimer;
|
||||||
var _apiVersionComponent = "version=" + this.apiVersion;
|
var _apiVersionComponent = "version=" + this.apiVersion;
|
||||||
var _sessionID;
|
var _sessionID;
|
||||||
var _sessionLock;
|
var _sessionLock;
|
||||||
|
|
||||||
|
|
||||||
|
function init() {
|
||||||
|
this.EventListener.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function login(callback) {
|
function login(callback) {
|
||||||
var url = _serverURL + "login";
|
var url = _serverURL + "login";
|
||||||
|
|
||||||
|
@ -525,13 +534,15 @@ Zotero.Sync.Server = new function () {
|
||||||
|
|
||||||
|
|
||||||
function sync() {
|
function sync() {
|
||||||
|
Zotero.Sync.Server.clearSyncTimeout();
|
||||||
|
|
||||||
if (_attempts < 0) {
|
if (_attempts < 0) {
|
||||||
_error('Too many attempts in Zotero.Sync.Server.sync()');
|
_error('Too many attempts in Zotero.Sync.Server.sync()');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_sessionID) {
|
if (!_sessionID) {
|
||||||
Zotero.debug("Session ID not available -- logging in");
|
Zotero.debug("Session ID not available -- logging in");
|
||||||
this.login(Zotero.Sync.Server.sync);
|
Zotero.Sync.Server.login(Zotero.Sync.Server.sync);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -852,7 +863,7 @@ Zotero.Sync.Server = new function () {
|
||||||
|
|
||||||
if (!_sessionID) {
|
if (!_sessionID) {
|
||||||
Zotero.debug("Session ID not available -- logging in");
|
Zotero.debug("Session ID not available -- logging in");
|
||||||
this.login(Zotero.Sync.Server.clear);
|
Zotero.Sync.Server.login(Zotero.Sync.Server.clear);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -897,7 +908,7 @@ Zotero.Sync.Server = new function () {
|
||||||
|
|
||||||
if (!_sessionID) {
|
if (!_sessionID) {
|
||||||
Zotero.debug("Session ID not available -- logging in");
|
Zotero.debug("Session ID not available -- logging in");
|
||||||
this.login(Zotero.Sync.Server.resetServer);
|
Zotero.Sync.Server.login(Zotero.Sync.Server.resetServer);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -978,6 +989,33 @@ Zotero.Sync.Server = new function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function setSyncTimeout() {
|
||||||
|
// check if server/auto-sync are enabled
|
||||||
|
|
||||||
|
var autoSyncTimeout = 15;
|
||||||
|
Zotero.debug('Setting auto-sync timeout to ' + autoSyncTimeout + ' seconds');
|
||||||
|
|
||||||
|
if (_autoSyncTimer) {
|
||||||
|
_autoSyncTimer.cancel();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_autoSyncTimer = Components.classes["@mozilla.org/timer;1"].
|
||||||
|
createInstance(Components.interfaces.nsITimer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// {} implements nsITimerCallback
|
||||||
|
_autoSyncTimer.initWithCallback({ notify: Zotero.Sync.Server.sync },
|
||||||
|
autoSyncTimeout * 1000, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function clearSyncTimeout() {
|
||||||
|
if (_autoSyncTimer) {
|
||||||
|
_autoSyncTimer.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function _checkResponse(xmlhttp) {
|
function _checkResponse(xmlhttp) {
|
||||||
if (!xmlhttp.responseXML ||
|
if (!xmlhttp.responseXML ||
|
||||||
!xmlhttp.responseXML.childNodes[0] ||
|
!xmlhttp.responseXML.childNodes[0] ||
|
||||||
|
@ -1060,6 +1098,21 @@ Zotero.BufferedInputListener.prototype = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Zotero.Sync.Server.EventListener = {
|
||||||
|
init: function () {
|
||||||
|
Zotero.Notifier.registerObserver(this);
|
||||||
|
},
|
||||||
|
|
||||||
|
notify: function (event, type, ids, extraData) {
|
||||||
|
// TODO: skip others
|
||||||
|
if (type == 'refresh') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Zotero.Sync.Server.setSyncTimeout();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Zotero.Sync.Server.Data = new function() {
|
Zotero.Sync.Server.Data = new function() {
|
||||||
|
@ -1137,10 +1190,10 @@ Zotero.Sync.Server.Data = new function() {
|
||||||
// Merge and store related items, since CR doesn't
|
// Merge and store related items, since CR doesn't
|
||||||
// affect related items
|
// affect related items
|
||||||
if (type == 'item') {
|
if (type == 'item') {
|
||||||
// TODO: skip conflict if only related items changed
|
// Remote
|
||||||
|
|
||||||
var related = xmlNode.related.toString();
|
var related = xmlNode.related.toString();
|
||||||
related = related ? related.split(' ') : [];
|
related = related ? related.split(' ') : [];
|
||||||
|
// Local
|
||||||
for each(var relID in obj.relatedItems) {
|
for each(var relID in obj.relatedItems) {
|
||||||
if (related.indexOf(relID) == -1) {
|
if (related.indexOf(relID) == -1) {
|
||||||
related.push(relID);
|
related.push(relID);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user