changed fuse search service to not create a new fuse object every time the search method is called

This commit is contained in:
Rohan Sircar 2019-12-11 13:02:49 +05:30
parent 70f0ea8054
commit e40e1a5c7a
2 changed files with 7 additions and 6 deletions

View File

@ -3,7 +3,7 @@ import { SearchService } from "./SearchService";
import Fuse = require('fuse.js'); import Fuse = require('fuse.js');
export class FuseSearchService<T> implements SearchService<T> { export class FuseSearchService<T> implements SearchService<T> {
// private readonly _fuse: Fuse<T, Object>; private readonly _fuse: Fuse<T, Object>;
private readonly _params: Fuse.FuseOptions<T>; private readonly _params: Fuse.FuseOptions<T>;
constructor(keys: string[], params: Fuse.FuseOptions<T> = { constructor(keys: string[], params: Fuse.FuseOptions<T> = {
@ -19,13 +19,14 @@ export class FuseSearchService<T> implements SearchService<T> {
}) { }) {
this._params = params; this._params = params;
this._params.keys = keys; this._params.keys = keys;
// this._fuse = new Fuse(list, this._params); this._fuse = new Fuse([], this._params);
} }
search<T>(list: T[], searchTerm: string): T[] { search(list: T[], searchTerm: string): T[] {
const fuse = new Fuse<T, Fuse.FuseOptions<any>>(list, this._params); // const fuse = new Fuse<T, Fuse.FuseOptions<any>>(list, this._params);
this._fuse.setCollection(list);
// fuse.search(searchTerm); // fuse.search(searchTerm);
return fuse.search(searchTerm) as T[]; return this._fuse.search(searchTerm) as T[];
} }
} }

View File

@ -1,3 +1,3 @@
export interface SearchService<T> { export interface SearchService<T> {
search<T>(list: T[], pattern: string): T[]; search(list: T[], pattern: string): T[];
} }