Skip to content

Commit

Permalink
feat(backup): worker
Browse files Browse the repository at this point in the history
  • Loading branch information
QC2168 committed Feb 26, 2023
1 parent 59d41da commit 8f015e6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 13 deletions.
36 changes: 23 additions & 13 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { release } from 'os';
// } from 'electron-devtools-installer';

const { join } = require('path');
const { Worker } = require('worker_threads');

const workerSrc = join(__dirname, './backup.js');
const preload = join(__dirname, './preload.js');

const mibInstance = new Mib();
Expand Down Expand Up @@ -53,6 +55,7 @@ async function createWindow() {
height: 580,
webPreferences: {
preload,
nodeIntegrationInWorker: true,
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
// Consider using contextBridge.exposeInMainWorld
// Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
Expand Down Expand Up @@ -106,6 +109,15 @@ app.on('activate', () => {
}
});

const runBackupWorker = (workerData) => new Promise((resolve, reject) => {
const worker = new Worker(workerSrc, { workerData });
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0) reject(new Error(`stopped with ${code} exit code`));
});
});

// new window example arg: new windows url
ipcMain.handle('open-win', (event, arg) => {
const childWindow = new BrowserWindow({
Expand Down Expand Up @@ -134,24 +146,22 @@ ipcMain.handle('maximize-win', () => {

ipcMain.handle('mibInstance', () => mibInstance);

ipcMain.handle('setDevice', (event, id:string) => mibInstance.setDevice(id));
ipcMain.handle('setDevice', (event, id: string) => mibInstance.setDevice(id));

ipcMain.handle('getDevices', () => getDevices(mibInstance.adbOpt.adbPath));

ipcMain.handle('backup', (event, id:SaveItemType|SaveItemType[]) => {
ipcMain.handle('backup', async (event, id: SaveItemType | SaveItemType[]) => {
try {
if (Array.isArray(id)) {
for (let i = 0; i < id.length; i += 1) {
mibInstance.start(id[i]);
}
} else {
mibInstance.start(id);
}
win.webContents.send('backupDone', {
msg: '备份任务完成',
result: true,
const result = await runBackupWorker({
task: 'backup',
cfg: {
current: mibInstance.adbOpt.current,
},
params: id,
});
} catch (error) {
win.webContents.send('backupDone', result);
} catch (e) {
console.log(e);
win.webContents.send('backupDone', {
msg: '备份进程出错了',
result: false,
Expand Down
39 changes: 39 additions & 0 deletions electron/worker/backup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Mib from '@qc2168/mib';

const { workerData, parentPort } = require('worker_threads');

const { cfg, task, params } = workerData;

let instance = null;

function post(data) {
parentPort.postMessage(data);
}

if (task === 'backup') {
const {
current,
} = cfg;
instance = new Mib();
instance.setDevice(current);
try {
if (Array.isArray(params)) {
for (let i = 0; i < params.length; i += 1) {
instance.start(params[i]);
}
} else {
instance.start(params);
}
post({
msg: '备份任务完成',
result: true,
});
} catch (error) {
post({
msg: '备份进程出错了',
result: false,
});
} finally {
instance = null;
}
}
6 changes: 6 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export default defineConfig({
options.reload();
},
},
{
entry: 'electron/worker/backup.ts',
onstart(options) {
options.reload();
},
},
]),

Unocss({
Expand Down

0 comments on commit 8f015e6

Please sign in to comment.