diff --git a/src/app/db/loaders.ts b/src/app/db/loaders.ts index 4e40eec..85c5d2c 100644 --- a/src/app/db/loaders.ts +++ b/src/app/db/loaders.ts @@ -8,13 +8,12 @@ export const loadDocument = (id: string): Promise => { { type: 'module' } ) - worker.onmessage = (e: MessageEvent<{ [key: string]: Document }>) => { + worker.onmessage = (e: MessageEvent) => { const data = e.data - const doc: Document | undefined = data[id] - if (!doc) { + if (!data) { return reject(new Error('404')) } else { - resolve(doc) + resolve(data) } worker.terminate() } @@ -24,7 +23,7 @@ export const loadDocument = (id: string): Promise => { worker.terminate() } - worker.postMessage('LOAD') + worker.postMessage(id) } else { reject( new Error( @@ -34,6 +33,9 @@ export const loadDocument = (id: string): Promise => { } }) } +/** + * @deprecated This function doesn't improve efficiency and shouldn't be used + */ export const loadAllDocuments = (): Promise<{ [key: string]: Document }> => { return new Promise((resolve, reject) => { if (typeof Worker !== 'undefined') { diff --git a/src/app/db/workers/documentLoader.worker.ts b/src/app/db/workers/documentLoader.worker.ts index b263e79..1ea6e60 100644 --- a/src/app/db/workers/documentLoader.worker.ts +++ b/src/app/db/workers/documentLoader.worker.ts @@ -1,7 +1,5 @@ import { documents } from '../data' onmessage = (e) => { - if (e.data === 'LOAD') { - self.postMessage(documents) - } + typeof e.data === 'string' && postMessage(documents[e.data]) }