TeaWeb/shared/js/update/index.ts

55 lines
1.6 KiB
TypeScript
Raw Normal View History

import {Updater} from "./Updater";
import {ChangeLog} from "tc-shared/update/ChangeLog";
import {spawnUpdatedModal} from "tc-shared/ui/modal/whats-new/Controller";
let updaterUi: Updater;
let updaterNative: Updater;
export function setUIUpdater(updater: Updater) {
if(typeof updaterUi !== "undefined") {
throw tr("An UI updater has already been registered");
}
updaterUi = updater;
}
export function setNativeUpdater(updater: Updater) {
if(typeof updaterNative !== "undefined") {
throw tr("An native updater has already been registered");
}
updaterNative = updater;
}
function getChangedChangeLog(updater: Updater) : ChangeLog | undefined {
if(updater.getCurrentVersion() === updater.getLastUsedVersion())
return undefined;
let changes = {
changes: [],
currentVersion: updater.getCurrentVersion()
} as ChangeLog;
let usedVersion = updater.getLastUsedVersion();
for(const change of updater.getChangeLog().changes) {
if(change.timestamp === usedVersion)
break;
changes.changes.push(change);
}
return changes.changes.length > 0 ? changes : undefined;
}
export function checkForUpdatedApp() {
let changesUI = updaterUi ? getChangedChangeLog(updaterUi) : undefined;
let changesNative = updaterNative ? getChangedChangeLog(updaterNative) : undefined;
if(changesUI !== undefined || changesNative !== undefined) {
spawnUpdatedModal({
changesUI: changesUI,
changesClient: changesNative
});
updaterUi?.updateUsedVersion();
updaterNative?.updateUsedVersion();
}
}