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

81 lines
1.9 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-25 22:31:13 -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-25 12:08:13 -04:00
function startSync(reportToFetcher) {
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-25 12:08:13 -04:00
var promise = LocalSync.sync();
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-25 08:53:38 -04:00
startSync(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-25 22:31:13 -04:00
var syncInterval = 3600000;
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-25 12:08:13 -04:00
startSync();
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 () {
startSync();
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
}
Dashboard.ready(restartInterval);
document.addEventListener("resume", restartInterval, false);
2015-09-21 21:05:33 -04:00
onDeviceReady();
})();