Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Add better typing support on AppUpdater as an event emitter #6825

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thin-toys-report.md
@@ -0,0 +1,5 @@
---
"electron-updater": patch
---

Added types for AppUpdater's events
3 changes: 2 additions & 1 deletion packages/electron-updater/package.json
Expand Up @@ -29,7 +29,8 @@
"@types/fs-extra": "9.0.13",
"@types/js-yaml": "4.0.3",
"@types/lodash.escaperegexp": "4.1.6",
"@types/lodash.isequal": "4.5.5"
"@types/lodash.isequal": "4.5.5",
"typed-emitter": "^2.1.0"
},
"typings": "./out/main.d.ts",
"publishConfig": {
Expand Down
31 changes: 28 additions & 3 deletions packages/electron-updater/src/AppUpdater.ts
@@ -1,4 +1,15 @@
import { AllPublishOptions, asArray, CancellationToken, newError, PublishConfiguration, UpdateInfo, UUID, DownloadOptions, CancellationError } from "builder-util-runtime"
import {
AllPublishOptions,
asArray,
CancellationToken,
newError,
PublishConfiguration,
UpdateInfo,
UUID,
DownloadOptions,
CancellationError,
ProgressInfo,
} from "builder-util-runtime"
import { randomBytes } from "crypto"
import { EventEmitter } from "events"
import { mkdir, outputFile, readFile, rename, unlink } from "fs-extra"
Expand All @@ -10,14 +21,28 @@ import { eq as isVersionsEqual, gt as isVersionGreaterThan, lt as isVersionLessT
import { AppAdapter } from "./AppAdapter"
import { createTempUpdateFile, DownloadedUpdateHelper } from "./DownloadedUpdateHelper"
import { ElectronAppAdapter } from "./ElectronAppAdapter"
import { ElectronHttpExecutor, getNetSession } from "./electronHttpExecutor"
import { ElectronHttpExecutor, getNetSession, LoginCallback } from "./electronHttpExecutor"
import { GenericProvider } from "./providers/GenericProvider"
import { DOWNLOAD_PROGRESS, Logger, Provider, ResolvedUpdateFileInfo, UPDATE_DOWNLOADED, UpdateCheckResult, UpdateDownloadedEvent, UpdaterSignal } from "./main"
import { createClient, isUrlProbablySupportMultiRangeRequests } from "./providerFactory"
import { ProviderPlatform } from "./providers/Provider"
import type TypedEmitter from "typed-emitter"
import Session = Electron.Session
import { AuthInfo } from "electron"

export type AppUpdaterEvents = {
error: (error: Error, message?: string) => void
login: (info: AuthInfo, callback: LoginCallback) => void
"checking-for-update": () => void
"update-not-available": (info: UpdateInfo) => void
"update-available": (info: UpdateInfo) => void
"update-downloaded": (event: UpdateDownloadedEvent) => void
"download-progress": (info: ProgressInfo) => void
"update-cancelled": (info: UpdateInfo) => void
"appimage-filename-updated": (path: string) => void
}

export abstract class AppUpdater extends EventEmitter {
export abstract class AppUpdater extends (EventEmitter as new () => TypedEmitter<AppUpdaterEvents>) {
/**
* Whether to automatically download an update when it is found.
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/electron-updater/src/electronHttpExecutor.ts
@@ -1,4 +1,5 @@
import { DownloadOptions, HttpExecutor, configureRequestOptions, configureRequestUrl } from "builder-util-runtime"
import { AuthInfo } from "electron"
import { RequestOptions } from "http"
import Session = Electron.Session
import ClientRequest = Electron.ClientRequest
Expand All @@ -15,7 +16,7 @@ export function getNetSession(): Session {
export class ElectronHttpExecutor extends HttpExecutor<Electron.ClientRequest> {
private cachedSession: Session | null = null

constructor(private readonly proxyLoginCallback?: (authInfo: any, callback: LoginCallback) => void) {
constructor(private readonly proxyLoginCallback?: (authInfo: AuthInfo, callback: LoginCallback) => void) {
super()
}

Expand Down
4 changes: 2 additions & 2 deletions packages/electron-updater/src/main.ts
Expand Up @@ -56,8 +56,8 @@ export interface UpdateCheckResult {

export type UpdaterEvents = "login" | "checking-for-update" | "update-available" | "update-not-available" | "update-cancelled" | "download-progress" | "update-downloaded" | "error"

export const DOWNLOAD_PROGRESS: UpdaterEvents = "download-progress"
export const UPDATE_DOWNLOADED: UpdaterEvents = "update-downloaded"
export const DOWNLOAD_PROGRESS = "download-progress"
export const UPDATE_DOWNLOADED = "update-downloaded"

export type LoginHandler = (authInfo: any, callback: LoginCallback) => void

Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/src/helpers/updaterTestUtil.ts
Expand Up @@ -82,7 +82,7 @@ export function tuneTestUpdater(updater: AppUpdater, options?: TestOnlyUpdaterOp

export function trackEvents(updater: AppUpdater) {
const actualEvents: Array<string> = []
for (const eventName of ["checking-for-update", "update-available", "update-downloaded", "error"]) {
for (const eventName of ["checking-for-update", "update-available", "update-downloaded", "error"] as const) {
updater.addListener(eventName, () => {
actualEvents.push(eventName)
})
Expand Down
2 changes: 1 addition & 1 deletion test/src/updater/nsisUpdaterTest.ts
Expand Up @@ -26,7 +26,7 @@ test("downgrade (disallowed, beta)", async () => {
})

const actualEvents: Array<string> = []
const expectedEvents = ["checking-for-update", "update-not-available"]
const expectedEvents = ["checking-for-update", "update-not-available"] as const
for (const eventName of expectedEvents) {
updater.addListener(eventName, () => {
actualEvents.push(eventName)
Expand Down