API improvements

- made `IPC` class to make custom IPC implementation
  instead of the Neutralino's Storage API
This commit is contained in:
Observer KRypt0n_ 2022-01-01 23:40:26 +02:00
parent 1fe18818dc
commit 4b69f9e4d0
No known key found for this signature in database
GPG key ID: DC5D4EC1303465DA
6 changed files with 106 additions and 15 deletions

View file

@ -30,7 +30,7 @@ Neutralino.events.on('windowClose', () => {
const log = Debug.get().join("\r\n");
if (log != '')
await Neutralino.filesystem.writeFile(`${path}/logs/${time.getDay()}-${time.getMonth()}-${time.getFullYear()}-${time.getHours()}-${time.getMinutes()}-${time.getSeconds()}.log`, log);
await Neutralino.filesystem.writeFile(`${path}/logs/${time.getDate()}-${time.getMonth() + 1}-${time.getFullYear()}-${time.getHours()}-${time.getMinutes()}-${time.getSeconds()}.log`, log);
Neutralino.app.exit();
};

View file

@ -2,13 +2,17 @@ import '../i18n';
import Debug from '../ts/core/Debug';
import App from '../settings.svelte';
import IPC from '../ts/core/IPC';
declare const Neutralino;
Neutralino.init();
Neutralino.events.on('windowClose', async () => {
await Neutralino.storage.setData('log', JSON.stringify(Debug.getRecords()));
await IPC.write({
type: 'log',
records: Debug.getRecords()
});
Neutralino.app.exit();
});

View file

@ -7,6 +7,7 @@
import { _, locale } from 'svelte-i18n';
import Configs from './ts/Configs';
import IPC from './ts/core/IPC';
import Window from './ts/neutralino/Window';
@ -22,15 +23,20 @@
});
const isLauncherLoaded = () => {
Neutralino.storage.getData('launcherLoaded')
.then(() => {
Neutralino.storage.setData('launcherLoaded', undefined);
IPC.read().then((records) => {
const launcherLoaded = records.filter((record) => record.data === 'launcher-loaded');
if (launcherLoaded.length > 0)
{
launcherLoaded.forEach((record) => record.pop());
Window.current.hide();
Neutralino.app.exit();
})
.catch(() => setTimeout(isLauncherLoaded, 1000));
}
else setTimeout(isLauncherLoaded, 1000);
});
};
Neutralino.events.on('ready', () => setTimeout(isLauncherLoaded, 3000));

View file

@ -7,6 +7,7 @@ import Configs from './Configs';
import ProgressBar from './launcher/ProgressBar';
import State from './launcher/State';
import Debug from './core/Debug';
import IPC from './core/IPC';
declare const Neutralino;
@ -50,13 +51,12 @@ export default class Launcher
this.settingsMenu.finish(() => {
this.settingsMenu = undefined;
Neutralino.storage.getData('log')
.then((data) => {
Debug.merge(JSON.parse(data));
Neutralino.storage.setData('log', undefined);
})
.catch(() => {});
IPC.read().then((records) => {
records.forEach((record) => {
if (record.data.type !== undefined && record.data.type === 'log')
Debug.merge(record.pop().data.records);
});
});
Window.current.show();
})

80
src/ts/core/IPC.ts Normal file
View file

@ -0,0 +1,80 @@
import constants from '../Constants';
declare const Neutralino;
class IPCRecord
{
public readonly id: number;
public readonly time: number;
public readonly data: any;
public constructor(id: number, time: number, data: any)
{
this.id = id;
this.time = time;
this.data = data;
}
/**
* Remove the record from the storage
*/
public pop(): IPCRecord
{
IPC.remove(this);
return this;
}
}
export default class IPC
{
/**
* Read records from the "shared inter-process storage"
*/
public static read(): Promise<IPCRecord[]>
{
return new Promise(async (resolve) => {
Neutralino.filesystem.readFile(`${await constants.paths.launcherDir}/.ipc.json`)
.then((data) => resolve(JSON.parse(data)))
.catch(() => resolve([]));
});
}
/**
* Write some data to the "shared inter-process storage"
*/
public static write(data: any): Promise<void>
{
return new Promise(async (resolve) => {
const records = await this.read();
records.push({
id: Math.round(Math.random() * 100000),
time: Date.now(),
data: data
} as IPCRecord);
await Neutralino.filesystem.writeFile(`${await constants.paths.launcherDir}/.ipc.json`, JSON.stringify(records));
resolve();
});
}
/**
* Remove record from the "shared inter-process storage"
*/
public static remove(record: IPCRecord): Promise<void>
{
return new Promise(async (resolve) => {
let records = await this.read();
records = records.filter((item) => item.id !== record.id && item.time !== record.time);
await Neutralino.filesystem.writeFile(`${await constants.paths.launcherDir}/.ipc.json`, JSON.stringify(records));
resolve();
});
}
};
export { IPCRecord };

View file

@ -9,6 +9,7 @@ import Voice from '../Voice';
import Runners from '../core/Runners';
import { DebugThread } from '../core/Debug';
import DXVK from '../core/DXVK';
import IPC from '../core/IPC';
declare const Neutralino;
@ -82,7 +83,7 @@ export default class State
};
this.update().then(() => {
Neutralino.storage.setData('launcherLoaded', 'aboba');
IPC.write('launcher-loaded');
Window.current.show();
});