|
|
|
@ -17,24 +17,42 @@ export class UpdateManager {
|
|
|
|
|
/**
|
|
|
|
|
* checks wether an update is needed
|
|
|
|
|
*/
|
|
|
|
|
private readonly MAX_CACHE_AGE = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
|
|
|
|
|
private readonly MIN_CHECK_INTERVAL = 100000; // 100 seconds in milliseconds
|
|
|
|
|
private lastCacheTimestamp: number = 0;
|
|
|
|
|
|
|
|
|
|
public async checkUpdate(cacheManager: CacheManager): Promise<boolean> {
|
|
|
|
|
const lswVersionInfoKey = 'versionInfo';
|
|
|
|
|
const cacheTimestampKey = 'cacheTimestamp';
|
|
|
|
|
|
|
|
|
|
// Initialize or load version info
|
|
|
|
|
if (!this.lastVersionInfo && !(await this.serviceworkerRef.store.check(lswVersionInfoKey))) {
|
|
|
|
|
this.lastVersionInfo = {
|
|
|
|
|
appHash: '',
|
|
|
|
|
appSemVer: 'v0.0.0',
|
|
|
|
|
};
|
|
|
|
|
} else if (
|
|
|
|
|
!this.lastVersionInfo &&
|
|
|
|
|
(await this.serviceworkerRef.store.check(lswVersionInfoKey))
|
|
|
|
|
) {
|
|
|
|
|
} else if (!this.lastVersionInfo && (await this.serviceworkerRef.store.check(lswVersionInfoKey))) {
|
|
|
|
|
this.lastVersionInfo = await this.serviceworkerRef.store.get(lswVersionInfoKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load or initialize cache timestamp
|
|
|
|
|
if (await this.serviceworkerRef.store.check(cacheTimestampKey)) {
|
|
|
|
|
this.lastCacheTimestamp = await this.serviceworkerRef.store.get(cacheTimestampKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
const millisSinceLastCheck = now - this.lastUpdateCheck;
|
|
|
|
|
if (millisSinceLastCheck < 100000) {
|
|
|
|
|
// TODO account for being offline
|
|
|
|
|
const cacheAge = now - this.lastCacheTimestamp;
|
|
|
|
|
|
|
|
|
|
// Force update if cache is too old
|
|
|
|
|
if (cacheAge > this.MAX_CACHE_AGE) {
|
|
|
|
|
logger.log('info', `Cache is older than ${this.MAX_CACHE_AGE}ms, forcing update...`);
|
|
|
|
|
await this.forceUpdate(cacheManager);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Regular update check interval
|
|
|
|
|
if (millisSinceLastCheck < this.MIN_CHECK_INTERVAL && cacheAge < this.MAX_CACHE_AGE) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
logger.log('info', 'checking for update of the app by comparing app hashes...');
|
|
|
|
@ -49,9 +67,17 @@ export class UpdateManager {
|
|
|
|
|
this.performAsyncUpdateDebouncedTask.trigger();
|
|
|
|
|
this.lastVersionInfo = currentVersionInfo;
|
|
|
|
|
await this.serviceworkerRef.store.set(lswVersionInfoKey, this.lastVersionInfo);
|
|
|
|
|
|
|
|
|
|
// Update cache timestamp
|
|
|
|
|
this.lastCacheTimestamp = now;
|
|
|
|
|
await this.serviceworkerRef.store.set('cacheTimestamp', now);
|
|
|
|
|
} else {
|
|
|
|
|
logger.log('ok', 'caches are still valid, performing revalidation in a bit...');
|
|
|
|
|
this.performAsyncCacheRevalidationDebouncedTask.trigger();
|
|
|
|
|
|
|
|
|
|
// Update cache timestamp after successful revalidation
|
|
|
|
|
this.lastCacheTimestamp = now;
|
|
|
|
|
await this.serviceworkerRef.store.set('cacheTimestamp', now);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -70,6 +96,17 @@ export class UpdateManager {
|
|
|
|
|
/**
|
|
|
|
|
* this task is executed once we know that there is a new version available
|
|
|
|
|
*/
|
|
|
|
|
private async forceUpdate(cacheManager: CacheManager) {
|
|
|
|
|
logger.log('info', 'Forcing cache update due to staleness');
|
|
|
|
|
await this.serviceworkerRef.cacheManager.cleanCaches('Cache is stale, forcing update.');
|
|
|
|
|
const currentVersionInfo = await this.getVersionInfoFromServer();
|
|
|
|
|
this.lastVersionInfo = currentVersionInfo;
|
|
|
|
|
await this.serviceworkerRef.store.set('versionInfo', this.lastVersionInfo);
|
|
|
|
|
this.lastCacheTimestamp = Date.now();
|
|
|
|
|
await this.serviceworkerRef.store.set('cacheTimestamp', this.lastCacheTimestamp);
|
|
|
|
|
await this.serviceworkerRef.leleServiceWorkerBackend.triggerReloadAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public performAsyncUpdateDebouncedTask = new plugins.taskbuffer.TaskDebounced({
|
|
|
|
|
name: 'performAsyncUpdate',
|
|
|
|
|
taskFunction: async () => {
|
|
|
|
|