Skip to content

Commit

Permalink
perf(search-filter): Reuse prev results if were not inclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Malkevich committed Jul 9, 2017
1 parent 05ac2c4 commit c623fd5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-2-dropdown-multiselect",
"version": "1.4.11",
"version": "1.4.12",
"description": "Customizable dropdown multiselect in Angular 2 with bootstrap css.",
"main": "dist/bundles/dropdown.umd.js",
"module": "dist/index.js",
Expand Down
17 changes: 12 additions & 5 deletions src/dropdown/search-filter.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class MultiSelectSearchFilter implements PipeTransform {

private _lastOptions: IMultiSelectOption[];
private _searchCache: { [k: string]: IMultiSelectOption[] } = {};
private _searchCacheInclusive: { [k: string]: boolean } = {};
private _searchCacheInclusive: { [k: string]: boolean | number } = {};

transform(options: Array<IMultiSelectOption>, str: string, limit = 0, renderLimit = 0): Array<IMultiSelectOption> {
str = (str || '').toLowerCase();
Expand All @@ -27,9 +27,16 @@ export class MultiSelectSearchFilter implements PipeTransform {
const prevStr = str.slice(0, -1);
const prevResults = this._searchCache[prevStr];

// If have previous results and it was inclusive, do only subsearch
if (prevResults && this._searchCacheInclusive[prevStr]) {
options = prevResults;
if (prevResults) {
const prevInclusiveOrIdx = this._searchCacheInclusive[prevStr];

if (prevInclusiveOrIdx === true) {
// If have previous results and it was inclusive, do only subsearch
options = prevResults;
} else if (typeof prevInclusiveOrIdx === 'number') {
// Or reuse prev results with unchecked ones
options = [...prevResults, ...options.slice(prevInclusiveOrIdx)];
}
}

const optsLength = options.length;
Expand Down Expand Up @@ -75,7 +82,7 @@ export class MultiSelectSearchFilter implements PipeTransform {
}

this._searchCache[str] = filteredOpts;
this._searchCacheInclusive[str] = i === optsLength;
this._searchCacheInclusive[str] = i === optsLength || i + 1;

return this._limitRenderedItems(filteredOpts, renderLimit);
}
Expand Down

0 comments on commit c623fd5

Please sign in to comment.