Skip to content

Commit

Permalink
feat: load external search index
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Sep 24, 2020
1 parent 9730c4e commit 346b10f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/services/SearchStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ export class SearchStore<T> {
load(state: any) {
this.searchWorker.load(state);
}

fromExternalJS(path?: string, exportName?: string) {
if (path && exportName) {
this.searchWorker.fromExternalJS(path, exportName)
}
}
}
14 changes: 14 additions & 0 deletions src/services/SearchWorker.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class Worker {
toJS = toJS;
load = load;
dispose = dispose;
fromExternalJS = fromExternalJS;
}

export interface SearchDocument {
Expand Down Expand Up @@ -72,6 +73,19 @@ export async function toJS() {
};
}

export async function fromExternalJS(path: string, exportName: string) {
try {
importScripts(path);
if (!self[exportName]) {
throw new Error('Broken index file format');
}

load(self[exportName]);
} catch (e) {
console.error('Failed to load search index: ' + e.message);
}
}

export async function load(state: any) {
store = state.store;
resolveIndex(lunr.Index.load(state.index));
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"es2015",
"es2016",
"es2017",
"dom"
"dom",
"WebWorker.ImportScripts"
],
"jsx": "react",
"types": [
Expand Down

0 comments on commit 346b10f

Please sign in to comment.