Run SQL initialize in parallel with createWindow

This commit is contained in:
Fedor Indutny 2021-04-08 16:27:41 -07:00 committed by Josh Perez
parent af9e038add
commit 207d05fd05
5 changed files with 123 additions and 30 deletions

View file

@ -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');

View file

@ -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 });
}