Skip to content

Commit

Permalink
fix: checking for updates after the client is reopened shows an `obje…
Browse files Browse the repository at this point in the history
…ct destroy` error

Signed-off-by: The1111mp <[email protected]>
  • Loading branch information
1111mp committed Feb 2, 2024
1 parent d80ae1c commit ce67bde
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 51 deletions.
2 changes: 1 addition & 1 deletion electron.vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default defineConfig({
}
}
},
plugins: [del({ targets: "release/app/dist" }), externalizeDepsPlugin()]
plugins: [del({ targets: ["release/app/dist", "release/build"] }), externalizeDepsPlugin()]
},
preload: {
build: {
Expand Down
7 changes: 6 additions & 1 deletion src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Closer, Themes } from "../types";
import type { MenuItemConstructorOptions } from "electron";

let mainWindow: BrowserWindow | null = null,
updater: AppUpdater | null = null,
tray: Tray | null = null,
locale: I18n.Locale,
menuBuilder: MenuBuilder,
Expand Down Expand Up @@ -151,7 +152,7 @@ const createWindow = async (code?: number) => {
// windows only
// Remove this if your app does not use auto updates
// eslint-disable-next-line
new AppUpdater(mainWindow);
updater = new AppUpdater(mainWindow);
}
};

Expand All @@ -166,6 +167,10 @@ app.on("window-all-closed", () => {

if (setting.closer === Closer.Close) {
app.quit();
} else {
updater?.clearMainBindings();

updater = null;
}
});

Expand Down
74 changes: 39 additions & 35 deletions src/main/updater.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,68 @@
import { ipcMain } from 'electron'
import updater from 'electron-updater'
import log from 'electron-log'
import { ipcMain } from "electron";
import updater from "electron-updater";
import log from "electron-log";

const { autoUpdater } = updater
const { autoUpdater } = updater;

import type { BrowserWindow } from 'electron'
import type { BrowserWindow } from "electron";

export class AppUpdater {
constructor(private readonly mainWindow: BrowserWindow) {
log.transports.file.level = 'info'
autoUpdater.logger = log
autoUpdater.autoDownload = false
log.transports.file.level = "info";
autoUpdater.logger = log;
autoUpdater.autoDownload = false;

autoUpdater.on('update-available', (info) => {
autoUpdater.on("update-available", (info) => {
// had updates
this.mainWindow?.webContents.send('update-available', info)
})
this.mainWindow?.webContents.send("update-available", info);
});

autoUpdater.on('update-not-available', () => {
autoUpdater.on("update-not-available", () => {
// there are no updates
this.mainWindow?.webContents.send('update-not-available', 'update-not-available')
})
this.mainWindow?.webContents.send("update-not-available", "update-not-available");
});

autoUpdater.on('download-progress', (progress) => {
autoUpdater.on("download-progress", (progress) => {
// download progress
mainWindow?.webContents.send('download-progress', progress)
})
mainWindow?.webContents.send("download-progress", progress);
});

autoUpdater.on('update-downloaded', (_evt) => {
autoUpdater.on("update-downloaded", (_evt) => {
// update downloaded
ipcMain.once('make-update-now', () => {
autoUpdater.quitAndInstall()
})
})
ipcMain.once("make-update-now", () => {
autoUpdater.quitAndInstall();
});
});

autoUpdater.on('error', (error) => {
autoUpdater.on("error", (error) => {
// update error
mainWindow?.webContents.send('update-error', error)
})
mainWindow?.webContents.send("update-error", error);
});

// initial
this.checkForUpdates()
this.comfirmUpdate()
this.checkForUpdates();
this.comfirmUpdate();
}

checkForUpdates() {
ipcMain.handle('check-for-updates', async () => {
ipcMain.handle("check-for-updates", async () => {
try {
const result = await autoUpdater.checkForUpdates()
const result = await autoUpdater.checkForUpdates();

return result && result.updateInfo ? result?.updateInfo : result
return result && result.updateInfo ? result?.updateInfo : result;
} catch (err) {
return Promise.reject(err)
return Promise.reject(err);
}
})
});
}

comfirmUpdate() {
ipcMain.handle('confirm-update', () => {
return autoUpdater.downloadUpdate()
})
ipcMain.handle("confirm-update", () => {
return autoUpdater.downloadUpdate();
});
}

clearMainBindings() {
autoUpdater.removeAllListeners();
}
}
25 changes: 13 additions & 12 deletions src/renderer/src/components/ui/command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MagnifyingGlassIcon } from "@radix-ui/react-icons";
import { Command as CommandPrimitive } from "cmdk";

import { cn } from "@renderer/lib/utils";
import { Dialog, DialogContent } from "./dialog";
// import { Dialog, DialogContent } from "src/renderer/src/components/ui/dialog";

const Command = React.forwardRef<
Expand All @@ -23,17 +24,17 @@ Command.displayName = CommandPrimitive.displayName;

interface CommandDialogProps extends DialogProps {}

// const CommandDialog = ({ children, ...props }: CommandDialogProps) => {
// return (
// <Dialog {...props}>
// <DialogContent className="overflow-hidden p-0">
// <Command className="[&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-group]]:px-2 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5">
// {children}
// </Command>
// </DialogContent>
// </Dialog>
// );
// };
const CommandDialog = ({ children, ...props }: CommandDialogProps) => {
return (
<Dialog {...props}>
<DialogContent className="overflow-hidden p-0">
<Command className="[&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-group]]:px-2 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5">
{children}
</Command>
</DialogContent>
</Dialog>
);
};

const CommandInput = React.forwardRef<
React.ElementRef<typeof CommandPrimitive.Input>,
Expand Down Expand Up @@ -132,7 +133,7 @@ CommandShortcut.displayName = "CommandShortcut";

export {
Command,
// CommandDialog,
CommandDialog,
CommandInput,
CommandList,
CommandEmpty,
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/src/components/ui/data-dnd-table.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useState } from "react";
import { useState } from "react";
import {
ColumnDef,
flexRender,
Expand Down Expand Up @@ -45,7 +45,7 @@ function DraggableRow<TData>({ row, reorderRow }: DraggableRowProps<TData>) {
drop: (draggedRow: Row<TData>) => reorderRow(draggedRow.index, row.index)
});

const [{ isDragging }, dragRef, previewRef] = useDrag({
const [, dragRef, previewRef] = useDrag({
collect: (monitor) => ({
isDragging: monitor.isDragging()
}),
Expand Down

0 comments on commit ce67bde

Please sign in to comment.