|
| 1 | +import type { DesktopUpdateInstallCheck, DesktopUpdateState } from "@t3tools/contracts"; |
| 2 | +import { compareSemverVersions } from "@t3tools/shared/semver"; |
| 3 | +import * as Clock from "effect/Clock"; |
| 4 | +import * as Duration from "effect/Duration"; |
| 5 | +import * as Effect from "effect/Effect"; |
| 6 | +import * as PubSub from "effect/PubSub"; |
| 7 | + |
| 8 | +import { reduceDesktopUpdateStateOnDownloadComplete } from "./updateMachine.ts"; |
| 9 | + |
| 10 | +/** A check that finished this recently is fresh enough for an install. */ |
| 11 | +const RECENT_CHECK_AGE = Duration.minutes(1); |
| 12 | + |
| 13 | +/** |
| 14 | + * Fork: the version that replaces the downloaded one when a check finds a newer release, or |
| 15 | + * null. The fork releases on every main merge, so a download can be old by the time of the click. |
| 16 | + */ |
| 17 | +export function resolveNewerUpdateVersion( |
| 18 | + downloadedVersion: string, |
| 19 | + state: DesktopUpdateState, |
| 20 | +): string | null { |
| 21 | + const available = state.availableVersion; |
| 22 | + return state.status === "available" && |
| 23 | + available !== null && |
| 24 | + compareSemverVersions(available, downloadedVersion) > 0 |
| 25 | + ? available |
| 26 | + : null; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Fork: checks for a newer release before the user installs a downloaded update. A check drops |
| 31 | + * the download from the state, but electron-updater keeps the downloaded file until the next |
| 32 | + * download. So the state gets the download back, and "Install this one" still installs it. |
| 33 | + */ |
| 34 | +export const makeCheckBeforeInstall = (updater: { |
| 35 | + readonly getState: Effect.Effect<DesktopUpdateState>; |
| 36 | + readonly updateState: ( |
| 37 | + f: (state: DesktopUpdateState) => DesktopUpdateState, |
| 38 | + ) => Effect.Effect<DesktopUpdateState>; |
| 39 | + readonly check: Effect.Effect<boolean>; |
| 40 | + readonly stateChanges: PubSub.PubSub<DesktopUpdateState>; |
| 41 | + readonly checkWait: Duration.Duration; |
| 42 | +}): Effect.Effect<DesktopUpdateInstallCheck> => |
| 43 | + Effect.scoped( |
| 44 | + Effect.gen(function* () { |
| 45 | + // Subscribe first, so the end of a check in flight cannot slip between the read and the wait. |
| 46 | + const changes = yield* PubSub.subscribe(updater.stateChanges); |
| 47 | + const before = yield* updater.getState; |
| 48 | + const downloadedVersion = before.downloadedVersion; |
| 49 | + if (downloadedVersion === null) return { newerVersion: null }; |
| 50 | + |
| 51 | + if (before.status !== "checking") { |
| 52 | + const now = yield* Clock.currentTimeMillis; |
| 53 | + // An unreadable `checkedAt` parses to NaN, which is never recent, so the check runs. |
| 54 | + const checkedAgo = before.checkedAt === null ? NaN : now - Date.parse(before.checkedAt); |
| 55 | + if (!(checkedAgo < Duration.toMillis(RECENT_CHECK_AGE))) yield* updater.check; |
| 56 | + } |
| 57 | + // The updater events that end a check reach the state on their own fiber, after the check |
| 58 | + // returns. So wait until the state leaves "checking", also for a check that ran already. |
| 59 | + if ((yield* updater.getState).status === "checking") { |
| 60 | + yield* PubSub.take(changes).pipe( |
| 61 | + Effect.repeat({ until: (state) => state.status !== "checking" }), |
| 62 | + Effect.timeoutOption(updater.checkWait), |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + const after = yield* updater.getState; |
| 67 | + if (after.status === "available" && after.downloadedVersion === null) { |
| 68 | + // No `checkedAt`: the last check found a newer release, so the next install checks again. |
| 69 | + yield* updater.updateState((current) => |
| 70 | + current.status === "available" && current.downloadedVersion === null |
| 71 | + ? { |
| 72 | + ...reduceDesktopUpdateStateOnDownloadComplete(current, downloadedVersion), |
| 73 | + releaseNotes: before.releaseNotes, |
| 74 | + omittedReleaseCount: before.omittedReleaseCount, |
| 75 | + checkedAt: null, |
| 76 | + } |
| 77 | + : current, |
| 78 | + ); |
| 79 | + } |
| 80 | + return { newerVersion: resolveNewerUpdateVersion(downloadedVersion, after) }; |
| 81 | + }), |
| 82 | + ).pipe(Effect.withSpan("desktop.updates.fork.checkBeforeInstall")); |
0 commit comments