jellyfin-web/dashboard-ui/cordova/ios/backgroundfetch.js

106 lines
2.6 KiB
JavaScript
Raw Normal View History

2015-09-21 18:05:33 -07:00
(function () {
2015-09-24 12:30:25 -07:00
var lastStart = 0;
2015-09-21 18:05:33 -07:00
function onDeviceReady() {
2015-10-01 23:14:04 -07:00
//var fetcher = window.BackgroundFetch;
2015-09-21 18:05:33 -07:00
2015-10-01 23:14:04 -07:00
//fetcher.configure(onBackgroundFetch, onBackgroundFetchFailed, {
// stopOnTerminate: false // <-- false is default
//});
2015-09-21 18:05:33 -07:00
}
function onSyncFinish() {
Logger.log('BackgroundFetch completed');
var fetcher = window.BackgroundFetch;
fetcher.finish(); // <-- N.B. You MUST called #finish so that native-side can signal completion of the background-thread to the os.
}
function onSyncFail() {
Logger.log('BackgroundFetch completed - sync failed');
var fetcher = window.BackgroundFetch;
fetcher.finish(); // <-- N.B. You MUST called #finish so that native-side can signal completion of the background-thread to the os.
}
2015-09-28 20:35:50 -07:00
function startSync(reportToFetcher, syncOptions) {
2015-09-24 12:30:25 -07:00
lastStart = new Date().getTime();
2015-09-21 18:05:33 -07:00
require(['localsync'], function () {
if (LocalSync.getSyncStatus() == 'Syncing') {
onSyncFinish();
return;
}
2015-09-28 20:35:50 -07:00
var promise = LocalSync.sync(syncOptions);
2015-09-21 18:05:33 -07:00
2015-09-25 09:08:13 -07:00
if (reportToFetcher) {
promise.done(onSyncFinish).fail(onSyncFail);
}
2015-09-21 18:05:33 -07:00
});
}
2015-09-24 12:30:25 -07:00
function onBackgroundFetch() {
Logger.log('BackgroundFetch initiated');
2015-09-28 20:35:50 -07:00
startSync(true, {
uploadPhotos: false,
enableNewDownloads: true
});
2015-09-24 12:30:25 -07:00
}
2015-09-21 18:05:33 -07:00
function onBackgroundFetchFailed() {
Logger.log('- BackgroundFetch failed');
}
2015-10-01 09:28:24 -07:00
var syncInterval = 900000;
var photoUploadInterval = 21600000;
var offlineUserSyncInterval = 43200000;
2015-09-28 20:35:50 -07:00
function startIntervalSync() {
startSync(false, {
uploadPhotos: true,
2015-10-01 09:28:24 -07:00
enableNewDownloads: true
2015-09-28 20:35:50 -07:00
});
}
function normalizeSyncOptions(options) {
options.enableBackgroundTransfer = true;
2015-10-01 09:28:24 -07:00
options.uploadPhotos = (new Date().getTime() - lastStart) >= photoUploadInterval;
options.syncOfflineUsers = (new Date().getTime() - lastStart) >= offlineUserSyncInterval;
2015-09-28 20:35:50 -07:00
}
Dashboard.ready(function () {
require(['localsync'], function () {
LocalSync.normalizeSyncOptions = normalizeSyncOptions;
});
2015-10-01 09:28:24 -07:00
});
2015-10-01 23:14:04 -07:00
pageClassOn('pageshow', "libraryPage", function () {
2015-10-01 09:28:24 -07:00
if (!Dashboard.getCurrentUserId()) {
return;
}
if ((new Date().getTime() - lastStart) >= syncInterval) {
setTimeout(function () {
startIntervalSync();
}, 10000);
}
2015-09-28 20:35:50 -07:00
});
2015-09-24 12:30:25 -07:00
2015-09-21 18:05:33 -07:00
onDeviceReady();
})();