fix: proper search-index dispose

This commit is contained in:
Roman Hotsiy 2020-05-25 16:36:09 +03:00
parent 8d1d4c82e1
commit 9dd129d90b
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
2 changed files with 26 additions and 13 deletions

View File

@ -42,6 +42,7 @@ export class SearchStore<T> {
dispose() {
(this.searchWorker as any).terminate();
(this.searchWorker as any).dispose();
}
search(q: string) {

View File

@ -14,6 +14,7 @@ export default class Worker {
search: typeof search = search;
toJS = toJS;
load = load;
dispose = dispose;
}
export interface SearchDocument {
@ -29,22 +30,28 @@ export interface SearchResult<T = string> {
let store: any[] = [];
let resolveIndex: (v: lunr.Index) => void = () => {
throw new Error('Should not be called');
};
const index: Promise<lunr.Index> = new Promise(resolve => {
resolveIndex = resolve;
});
lunr.tokenizer.separator = /\s+/;
const builder = new lunr.Builder();
builder.field('title');
builder.field('description');
builder.ref('ref');
let builder: lunr.Builder;
builder.pipeline.add(lunr.trimmer, lunr.stopWordFilter, lunr.stemmer);
let resolveIndex: (v: lunr.Index) => void;
let index: Promise<lunr.Index>;
function initEmpty() {
builder = new lunr.Builder();
builder.field('title');
builder.field('description');
builder.ref('ref');
builder.pipeline.add(lunr.trimmer, lunr.stopWordFilter, lunr.stemmer);
index = new Promise(resolve => {
resolveIndex = resolve;
});
}
initEmpty();
const expandTerm = term => '*' + lunr.stemmer(new lunr.Token(term, {})) + '*';
@ -70,6 +77,11 @@ export async function load(state: any) {
resolveIndex(lunr.Index.load(state.index));
}
export async function dispose() {
store = [];
initEmpty();
}
export async function search<Meta = string>(
q: string,
limit = 0,