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"); const log = Debug.get().join("\r\n");
if (log != '') 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(); Neutralino.app.exit();
}; };

View file

@ -2,13 +2,17 @@ import '../i18n';
import Debug from '../ts/core/Debug'; import Debug from '../ts/core/Debug';
import App from '../settings.svelte'; import App from '../settings.svelte';
import IPC from '../ts/core/IPC';
declare const Neutralino; declare const Neutralino;
Neutralino.init(); Neutralino.init();
Neutralino.events.on('windowClose', async () => { 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(); Neutralino.app.exit();
}); });

View file

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

View file

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