From 9568b4b3f9d009649619db371dbade1b73d494c8 Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Thu, 15 Feb 2024 02:43:10 -0800 Subject: [PATCH] update web worker so its actually efficient --- src/app/db/loaders.ts | 12 +++++++----- src/app/db/workers/documentLoader.worker.ts | 4 +--- 2 files changed, 8 insertions(+), 8 deletions(-) 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]) }