From 4b69f9e4d0d56fa2b858fa36ed58c3e8e102d9da Mon Sep 17 00:00:00 2001 From: Observer KRypt0n_ Date: Sat, 1 Jan 2022 23:40:26 +0200 Subject: [PATCH] API improvements - made `IPC` class to make custom IPC implementation instead of the Neutralino's Storage API --- src/pages/index.ts | 2 +- src/pages/settings.ts | 6 ++- src/splash.svelte | 16 +++++--- src/ts/Launcher.ts | 14 +++---- src/ts/core/IPC.ts | 80 ++++++++++++++++++++++++++++++++++++++++ src/ts/launcher/State.ts | 3 +- 6 files changed, 106 insertions(+), 15 deletions(-) create mode 100644 src/ts/core/IPC.ts diff --git a/src/pages/index.ts b/src/pages/index.ts index 8334840..98d1b3d 100644 --- a/src/pages/index.ts +++ b/src/pages/index.ts @@ -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(); }; diff --git a/src/pages/settings.ts b/src/pages/settings.ts index 53ce02b..c999e7f 100644 --- a/src/pages/settings.ts +++ b/src/pages/settings.ts @@ -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(); }); diff --git a/src/splash.svelte b/src/splash.svelte index 00eef4f..184e44f 100644 --- a/src/splash.svelte +++ b/src/splash.svelte @@ -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)); diff --git a/src/ts/Launcher.ts b/src/ts/Launcher.ts index 967c6a4..42a1d8e 100644 --- a/src/ts/Launcher.ts +++ b/src/ts/Launcher.ts @@ -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(); }) diff --git a/src/ts/core/IPC.ts b/src/ts/core/IPC.ts new file mode 100644 index 0000000..795ad8c --- /dev/null +++ b/src/ts/core/IPC.ts @@ -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 + { + 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 + { + 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 + { + 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 }; diff --git a/src/ts/launcher/State.ts b/src/ts/launcher/State.ts index dda9960..30584a4 100644 --- a/src/ts/launcher/State.ts +++ b/src/ts/launcher/State.ts @@ -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(); });