1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/dashboard-ui/cordova/ios/backgroundfetch.js

106 lines
2.6 KiB
JavaScript
Raw Normal View History

2015-09-21 21:05:33 -04:00
(function () {
2015-09-24 15:30:25 -04:00
var lastStart = 0;
2015-09-21 21:05:33 -04:00
function onDeviceReady() {
2015-10-02 02:14:04 -04:00
//var fetcher = window.BackgroundFetch;
2015-09-21 21:05:33 -04:00
2015-10-02 02:14:04 -04:00
//fetcher.configure(onBackgroundFetch, onBackgroundFetchFailed, {
// stopOnTerminate: false // <-- false is default
//});
2015-09-21 21:05:33 -04: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 23:35:50 -04:00
function startSync(reportToFetcher, syncOptions) {
2015-09-24 15:30:25 -04:00
lastStart = new Date().getTime();
2015-09-21 21:05:33 -04:00
require(['localsync'], function () {
if (LocalSync.getSyncStatus() == 'Syncing') {
onSyncFinish();
return;
}
2015-09-28 23:35:50 -04:00
var promise = LocalSync.sync(syncOptions);
2015-09-21 21:05:33 -04:00
2015-09-25 12:08:13 -04:00
if (reportToFetcher) {
promise.done(onSyncFinish).fail(onSyncFail);
}
2015-09-21 21:05:33 -04:00
});
}
2015-09-24 15:30:25 -04:00
function onBackgroundFetch() {
Logger.log('BackgroundFetch initiated');
2015-09-28 23:35:50 -04:00
startSync(true, {
uploadPhotos: false,
enableNewDownloads: true
});
2015-09-24 15:30:25 -04:00
}
2015-09-21 21:05:33 -04:00
function onBackgroundFetchFailed() {
Logger.log('- BackgroundFetch failed');
}
2015-10-01 12:28:24 -04:00
var syncInterval = 900000;
var photoUploadInterval = 21600000;
var offlineUserSyncInterval = 43200000;
2015-09-28 23:35:50 -04:00
function startIntervalSync() {
startSync(false, {
uploadPhotos: true,
2015-10-01 12:28:24 -04:00
enableNewDownloads: true
2015-09-28 23:35:50 -04:00
});
}
function normalizeSyncOptions(options) {
options.enableBackgroundTransfer = true;
2015-10-01 12:28:24 -04:00
options.uploadPhotos = (new Date().getTime() - lastStart) >= photoUploadInterval;
options.syncOfflineUsers = (new Date().getTime() - lastStart) >= offlineUserSyncInterval;
2015-09-28 23:35:50 -04:00
}
Dashboard.ready(function () {
require(['localsync'], function () {
LocalSync.normalizeSyncOptions = normalizeSyncOptions;
});
2015-10-01 12:28:24 -04:00
});
2015-10-02 02:14:04 -04:00
pageClassOn('pageshow', "libraryPage", function () {
2015-10-01 12:28:24 -04:00
if (!Dashboard.getCurrentUserId()) {
return;
}
if ((new Date().getTime() - lastStart) >= syncInterval) {
setTimeout(function () {
startIntervalSync();
}, 10000);
}
2015-09-28 23:35:50 -04:00
});
2015-09-24 15:30:25 -04:00
2015-09-21 21:05:33 -04:00
onDeviceReady();
})();