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

112 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() {
var fetcher = window.BackgroundFetch;
fetcher.configure(onBackgroundFetch, onBackgroundFetchFailed, {
2015-09-27 17:02:39 -04:00
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,
enableBackgroundTransfer: true,
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-09-27 11:00:57 -04:00
var syncInterval = 1800000;
2015-09-24 15:30:25 -04:00
function restartInterval() {
2015-09-25 08:53:38 -04:00
setInterval(function () {
2015-09-24 15:30:25 -04:00
2015-09-28 23:35:50 -04:00
startIntervalSync();
2015-09-24 15:30:25 -04:00
2015-09-25 08:53:38 -04:00
}, syncInterval);
2015-09-25 01:15:29 -04:00
2015-09-25 22:31:13 -04:00
if (lastStart > 0 && (new Date().getTime() - lastStart) >= syncInterval) {
2015-09-25 12:08:13 -04:00
setTimeout(function () {
2015-09-28 23:35:50 -04:00
startIntervalSync();
2015-09-25 22:31:13 -04:00
}, 5000);
2015-09-25 08:53:38 -04:00
}
2015-09-24 15:30:25 -04:00
}
2015-09-28 23:35:50 -04:00
function startIntervalSync() {
startSync(false, {
uploadPhotos: true,
enableNewDownloads: false,
enableBackgroundTransfer: true
});
}
function normalizeSyncOptions(options) {
options.enableBackgroundTransfer = true;
if (options.enableNewDownloads == null) {
options.enableNewDownloads = false;
}
}
Dashboard.ready(function () {
require(['localsync'], function () {
LocalSync.normalizeSyncOptions = normalizeSyncOptions;
});
restartInterval();
});
2015-09-24 15:30:25 -04:00
document.addEventListener("resume", restartInterval, false);
2015-09-21 21:05:33 -04:00
onDeviceReady();
})();