Skip to content

Commit

Permalink
refactor: Add better typing support on AppUpdater as an event emitter (
Browse files Browse the repository at this point in the history
  • Loading branch information
Nokel81 committed May 1, 2022
1 parent bfe29a5 commit db07548
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 9 deletions.
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"

This comment has been minimized.

Copy link
@panther7

panther7 May 11, 2022

Contributor

Maybe must be declare in normal dependencies, because in my project is as undefined.

2022-05-11 16 25 10

@Nokel81

This comment has been minimized.

Copy link
@panther7

panther7 May 11, 2022

Contributor

2022-05-11 16 27 16

This comment has been minimized.

Copy link
@Nokel81

Nokel81 May 11, 2022

Author Contributor

Yeah I can create a PR to just do these types manually if you would like.

This comment has been minimized.

Copy link
@Nokel81

Nokel81 May 11, 2022

Author Contributor

Done #6858

},
"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

0 comments on commit db07548

Please sign in to comment.