mirror of
https://github.com/Redocly/redoc.git
synced 2025-02-27 23:52:46 +03:00
fix: proper search-index dispose
This commit is contained in:
parent
8d1d4c82e1
commit
9dd129d90b
|
@ -42,6 +42,7 @@ export class SearchStore<T> {
|
||||||
|
|
||||||
dispose() {
|
dispose() {
|
||||||
(this.searchWorker as any).terminate();
|
(this.searchWorker as any).terminate();
|
||||||
|
(this.searchWorker as any).dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
search(q: string) {
|
search(q: string) {
|
||||||
|
|
|
@ -14,6 +14,7 @@ export default class Worker {
|
||||||
search: typeof search = search;
|
search: typeof search = search;
|
||||||
toJS = toJS;
|
toJS = toJS;
|
||||||
load = load;
|
load = load;
|
||||||
|
dispose = dispose;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SearchDocument {
|
export interface SearchDocument {
|
||||||
|
@ -29,22 +30,28 @@ export interface SearchResult<T = string> {
|
||||||
|
|
||||||
let store: any[] = [];
|
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+/;
|
lunr.tokenizer.separator = /\s+/;
|
||||||
|
|
||||||
const builder = new lunr.Builder();
|
let builder: lunr.Builder;
|
||||||
builder.field('title');
|
|
||||||
builder.field('description');
|
|
||||||
builder.ref('ref');
|
|
||||||
|
|
||||||
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, {})) + '*';
|
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));
|
resolveIndex(lunr.Index.load(state.index));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function dispose() {
|
||||||
|
store = [];
|
||||||
|
initEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
export async function search<Meta = string>(
|
export async function search<Meta = string>(
|
||||||
q: string,
|
q: string,
|
||||||
limit = 0,
|
limit = 0,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user