mirror of
https://github.com/antebudimir/feishin.git
synced 2026-01-01 10:23:33 +00:00
Add ability to save/restore queue (#111)
* add ability to save/restore play queue * Add restoreQueue action * Add optional pause param on setQueue --------- Co-authored-by: jeffvli <jeffvictorli@gmail.com>
This commit is contained in:
parent
c1c6ce33e4
commit
106fc90c4a
10 changed files with 180 additions and 7 deletions
|
|
@ -8,7 +8,9 @@
|
|||
* When running `npm run build` or `npm run build:main`, this file is compiled to
|
||||
* `./src/main.js` using webpack. This gives us some performance wins.
|
||||
*/
|
||||
import path from 'path';
|
||||
import { access, constants, readFile, writeFile } from 'fs';
|
||||
import path, { join } from 'path';
|
||||
import { deflate, inflate } from 'zlib';
|
||||
import {
|
||||
app,
|
||||
BrowserWindow,
|
||||
|
|
@ -239,6 +241,36 @@ const createWindow = async () => {
|
|||
disableMediaKeys();
|
||||
});
|
||||
|
||||
ipcMain.on('player-restore-queue', () => {
|
||||
if (store.get('resume')) {
|
||||
const queueLocation = join(app.getPath('userData'), 'queue');
|
||||
|
||||
access(queueLocation, constants.F_OK, (accessError) => {
|
||||
if (accessError) {
|
||||
console.error('unable to access saved queue: ', accessError);
|
||||
return;
|
||||
}
|
||||
|
||||
readFile(queueLocation, (readError, buffer) => {
|
||||
if (readError) {
|
||||
console.error('failed to read saved queue: ', readError);
|
||||
return;
|
||||
}
|
||||
|
||||
inflate(buffer, (decompressError, data) => {
|
||||
if (decompressError) {
|
||||
console.error('failed to decompress queue: ', decompressError);
|
||||
return;
|
||||
}
|
||||
|
||||
const queue = JSON.parse(data.toString());
|
||||
getMainWindow()?.webContents.send('renderer-player-restore-queue', queue);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const globalMediaKeysEnabled = store.get('global_media_hotkeys') as boolean;
|
||||
|
||||
if (globalMediaKeysEnabled) {
|
||||
|
|
@ -263,6 +295,8 @@ const createWindow = async () => {
|
|||
mainWindow = null;
|
||||
});
|
||||
|
||||
let saved = false;
|
||||
|
||||
mainWindow.on('close', (event) => {
|
||||
if (!exitFromTray && store.get('window_exit_to_tray')) {
|
||||
if (isMacOS() && !forceQuit) {
|
||||
|
|
@ -271,6 +305,43 @@ const createWindow = async () => {
|
|||
event.preventDefault();
|
||||
mainWindow?.hide();
|
||||
}
|
||||
|
||||
if (!saved && store.get('resume')) {
|
||||
event.preventDefault();
|
||||
saved = true;
|
||||
|
||||
getMainWindow()?.webContents.send('renderer-player-save-queue');
|
||||
|
||||
ipcMain.once('player-save-queue', async (_event, data: Record<string, any>) => {
|
||||
const queueLocation = join(app.getPath('userData'), 'queue');
|
||||
const serialized = JSON.stringify(data);
|
||||
|
||||
try {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
deflate(serialized, { level: 1 }, (error, deflated) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
writeFile(queueLocation, deflated, (writeError) => {
|
||||
if (writeError) {
|
||||
reject(writeError);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('error saving queue state: ', error);
|
||||
} finally {
|
||||
mainWindow?.close();
|
||||
if (forceQuit) {
|
||||
app.exit();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
mainWindow.on('minimize', (event: any) => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue