Electron 使用 electron-updater 设计自

2024-07-18  本文已影响0人  申_9a33

1. 时序图

自动更新时序图.png

2. 流程图

自动更新流程图.png

3. 对electron-updater进行封装

import { autoUpdater, type CancellationToken } from 'electron-updater';
import log from 'electron-log/main';

export type AutoUpdaterCallback = ({
  code,
  progress,
  error,
}: {
  code: -1 | 0 | 1 | 2;
  progress?: {
    speed: number;
    percent: number;
    downloaded: string;
  };
  error?: Error;
}) => void;

class AutoUpdater {
  private callBack: AutoUpdaterCallback | undefined;

  private cancellationToken: CancellationToken | undefined;

  private isUpdate: boolean; // 下载完成后是否立刻更新

  constructor(callBack: AutoUpdaterCallback | undefined) {
    this.callBack = callBack;
    this.isUpdate = false;
    this.cancellationToken = undefined;

    autoUpdater.logger = log;
    autoUpdater.autoInstallOnAppQuit = false;

    // 更新报错
    autoUpdater.on('error', (err) => {
      this.callBack?.({ code: -1, error: err });
      console.error('[AutoUpdater] error', err);
    });

    // 检测中
    autoUpdater.on('checking-for-update', () => {
      this.callBack?.({ code: 1 });
      console.info('[AutoUpdater] checking-for-update');
    });

    // 有更新包
    autoUpdater.on('update-available', (info) => {
      this.callBack?.({ code: 1 });
      console.info('[AutoUpdater] update-available', info);
    });

    // 无更新包
    autoUpdater.on('update-not-available', (info) => {
      this.callBack?.({ code: 0 });
      console.info('[AutoUpdater] update-not-available', info);
    });

    // 更新下载进度
    autoUpdater.on('download-progress', (progress) => {
      const result = {
        speed: progress.bytesPerSecond,
        percent: progress.percent,
        downloaded: `${(progress.transferred / 1048576).toFixed(2)}MB / ${(progress.total / 1048576).toFixed(2)}MB`,
      };

      this.callBack?.({ code: 1, progress: result });
      console.info('[AutoUpdater] download-progress', result);
    });

    // 包下载完成
    autoUpdater.on('update-downloaded', () => {
      console.info('[AutoUpdater] update-downloaded', this.isUpdate);

      if (this.isUpdate) {
        // 检测完成需要更新才更新
        if (process.platform === 'darwin') {
          autoUpdater.quitAndInstall();
        } else {
          autoUpdater.quitAndInstall(true, true);
        }
      } else {
        this.callBack?.({ code: 2 });
      }
    });
  }

  // 检查一次版本更新
  async checkForUpdates(feedURL: string, isUpdate: boolean) {
    this.isUpdate = isUpdate;
    if (feedURL) autoUpdater.setFeedURL(feedURL);

    const res = await autoUpdater.checkForUpdates();

    if (res?.cancellationToken) {
      this.cancellationToken = res?.cancellationToken;
    }

    return {
      currentVersion: autoUpdater.currentVersion.version,
      remoteVersion: res?.updateInfo.version,
    };
  }

  // 取消下载
  cnacel() {
    if (this.cancellationToken) {
      this.cancellationToken.cancel();
      this.cancellationToken = undefined;
    }
  }
}

const autoUpdaterInstance = new AutoUpdater(undefined);

export default autoUpdaterInstance;


上一篇 下一篇

猜你喜欢

热点阅读