Changeset View
Changeset View
Standalone View
Standalone View
web/database/database-module-provider.js
Show All 10 Lines | |||||
declare var sqljsFilename: string; | declare var sqljsFilename: string; | ||||
declare var supportSqlite: string; | declare var supportSqlite: string; | ||||
const databaseStatuses = Object.freeze({ | const databaseStatuses = Object.freeze({ | ||||
notSupported: 'NOT_SUPPORTED', | notSupported: 'NOT_SUPPORTED', | ||||
initSuccess: 'INIT_SUCCESS', | initSuccess: 'INIT_SUCCESS', | ||||
initInProgress: 'INIT_IN_PROGRESS', | initInProgress: 'INIT_IN_PROGRESS', | ||||
initError: 'INIT_ERROR', | initError: 'INIT_ERROR', | ||||
deleting: 'DELETING', | |||||
}); | }); | ||||
type DatabaseStatus = $Values<typeof databaseStatuses>; | type DatabaseStatus = $Values<typeof databaseStatuses>; | ||||
class DatabaseModule { | class DatabaseModule { | ||||
worker: SharedWorker; | worker: SharedWorker; | ||||
workerProxy: WorkerConnectionProxy; | workerProxy: WorkerConnectionProxy; | ||||
initPromise: Promise<void>; | initPromise: Promise<void>; | ||||
Show All 29 Lines | this.initPromise = this.workerProxy | ||||
console.error(`Database initialization failure`, error); | console.error(`Database initialization failure`, error); | ||||
}); | }); | ||||
} | } | ||||
onError: (error: Error) => void = error => { | onError: (error: Error) => void = error => { | ||||
console.error(error); | console.error(error); | ||||
}; | }; | ||||
async clearSensitiveData(): Promise<void> { | |||||
this.status = databaseStatuses.deleting; | |||||
await this.workerProxy.scheduleOnWorker({ | |||||
type: workerRequestMessageTypes.CLEAR_SENSITIVE_DATA, | |||||
}); | |||||
tomek: Should we ignore the exception here? | |||||
kamilAuthorUnsubmitted Done Inline Actionsit's caught in call site kamil: it's caught in call site | |||||
const origin = window.location.origin; | |||||
try { | |||||
await this.workerProxy.scheduleOnWorker({ | |||||
type: workerRequestMessageTypes.INIT, | |||||
sqljsFilePath: `${origin}${SQLJS_FILE_PATH}`, | |||||
sqljsFilename, | |||||
}); | |||||
this.status = databaseStatuses.initSuccess; | |||||
} catch (error) { | |||||
this.status = databaseStatuses.initError; | |||||
console.error(`Database initialization failure`, error); | |||||
ashoatUnsubmitted Not Done Inline ActionsDoesn't need to be a template literal ashoat: Doesn't need to be a template literal | |||||
} | |||||
} | |||||
async schedule( | async schedule( | ||||
payload: WorkerRequestMessage, | payload: WorkerRequestMessage, | ||||
): Promise<?WorkerResponseMessage> { | ): Promise<?WorkerResponseMessage> { | ||||
if (this.status === databaseStatuses.notSupported) { | if (this.status === databaseStatuses.notSupported) { | ||||
throw new Error('Database not supported'); | throw new Error('Database not supported'); | ||||
} | } | ||||
if (this.status === databaseStatuses.deleting) { | |||||
throw new Error('Database being deleted'); | |||||
} | |||||
tomekUnsubmitted Not Done Inline ActionsWe can also consider rejecting a promise tomek: We can also consider rejecting a promise | |||||
if (this.status === databaseStatuses.initInProgress) { | if (this.status === databaseStatuses.initInProgress) { | ||||
await this.initPromise; | await this.initPromise; | ||||
} | } | ||||
if (this.status === databaseStatuses.initError) { | if (this.status === databaseStatuses.initError) { | ||||
throw new Error('Database could not be initialized'); | throw new Error('Database could not be initialized'); | ||||
} | } | ||||
return this.workerProxy.scheduleOnWorker(payload); | return this.workerProxy.scheduleOnWorker(payload); | ||||
} | } | ||||
} | } | ||||
const databaseModule: DatabaseModule = new DatabaseModule(); | const databaseModule: DatabaseModule = new DatabaseModule(); | ||||
export { databaseModule }; | export { databaseModule }; |
Should we ignore the exception here?