Run SQL initialize in parallel with createWindow
This commit is contained in:
parent
af9e038add
commit
207d05fd05
5 changed files with 123 additions and 30 deletions
|
@ -8,7 +8,18 @@ import sql from './Server';
|
|||
|
||||
const getRealPath = pify(fs.realpath);
|
||||
|
||||
export async function initialize(): Promise<void> {
|
||||
// Called from renderer.
|
||||
export async function initialize(isTesting = false): Promise<void> {
|
||||
if (!isTesting) {
|
||||
ipc.send('database-ready');
|
||||
|
||||
await new Promise<void>(resolve => {
|
||||
ipc.once('database-ready', () => {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const configDir = await getRealPath(ipc.sendSync('get-user-data-path'));
|
||||
const key = ipc.sendSync('user-config-key');
|
||||
|
||||
|
|
|
@ -44,6 +44,10 @@ type PromisePair<T> = {
|
|||
export class MainSQL {
|
||||
private readonly worker: Worker;
|
||||
|
||||
private isReady = false;
|
||||
|
||||
private onReady: Promise<void> | undefined;
|
||||
|
||||
private readonly onExit: Promise<void>;
|
||||
|
||||
private seq = 0;
|
||||
|
@ -81,16 +85,37 @@ export class MainSQL {
|
|||
}
|
||||
|
||||
public async initialize(options: InitializeOptions): Promise<void> {
|
||||
return this.send({ type: 'init', options });
|
||||
if (this.isReady || this.onReady) {
|
||||
throw new Error('Already initialized');
|
||||
}
|
||||
|
||||
this.onReady = this.send({ type: 'init', options });
|
||||
|
||||
await this.onReady;
|
||||
|
||||
this.onReady = undefined;
|
||||
this.isReady = true;
|
||||
}
|
||||
|
||||
public async close(): Promise<void> {
|
||||
if (!this.isReady) {
|
||||
throw new Error('Not initialized');
|
||||
}
|
||||
|
||||
await this.send({ type: 'close' });
|
||||
await this.onExit;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
public async sqlCall(method: string, args: ReadonlyArray<any>): Promise<any> {
|
||||
if (this.onReady) {
|
||||
await this.onReady;
|
||||
}
|
||||
|
||||
if (!this.isReady) {
|
||||
throw new Error('Not initialized');
|
||||
}
|
||||
|
||||
return this.send({ type: 'sqlCall', method, args });
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue