Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eslint-baseline.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"root": 143,
"root": 44,
"test-app": 0
}
2 changes: 1 addition & 1 deletion src/formly/components/autocomplete/test.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { map } from "rxjs/operators";
export class TestAutocompleteService implements AutocompleteService {
public setFetchMethod() {}

public fetch(val: string): Observable<unknown> {
public fetch(val: string): Observable<unknown[]> {
void val;
return of([
{ key: "MD", value: "Maryland", category: "Places" },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { areEqual } from "../../utilities/are-equal/are-equal";
import { isEqual } from "lodash";

export class Cached {
private contents: any[] = [];
private _lastValue: any[] = [];
export class Cached<T = unknown> {
private contents: T[] = [];
private _lastValue: T[] = [];

public get value() {
public get value(): T[] {
return this.contents;
}

public get lastValue(): any[] {
public get lastValue(): T[] {
return this._lastValue;
}

Expand All @@ -21,7 +21,7 @@ export class Cached {
return Cached.countBytes(this.value);
}

public static countBytes(s: any): number {
public static countBytes(s: unknown): number {
return (
encodeURI(JSON.stringify(s)).split(/%(?:u[0-9A-F]{2})?[0-9A-F]{2}|./)
.length - 1
Expand All @@ -30,12 +30,12 @@ export class Cached {

constructor(
public readonly name: string,
initialValue: any[] = []
initialValue: T[] = []
) {
this.insert(initialValue);
}

public insert(val: any[]): any[] {
public insert(val: T[]): T[] {
const deduped = this.dedupe(val);
this._lastValue = deduped;
this.contents = [...this.contents, ...deduped];
Expand All @@ -47,8 +47,8 @@ export class Cached {
this._lastValue = [];
}

private dedupe(newContents): any[] {
return newContents.filter((item: any) => {
private dedupe(newContents: T[]): T[] {
return newContents.filter((item: T) => {
let foundDupe = false;
for (let i = 0; i < this.value.length; i++) {
if (areEqual(item, this.value[i])) {
Expand All @@ -62,11 +62,14 @@ export class Cached {
}
}

export class AutocompleteCache {
private cached: { [index: string]: Cached } = {};
private default: Cached = new Cached("default");
export class AutocompleteCache<T = unknown> {
private cached: { [index: string]: Cached<T> } = {};
private default: Cached<T> = new Cached("default");
private history: string[] = [];
private historyTuple: Array<string[] | Cached> = [this.default, this.history];
private historyTuple: Array<string[] | Cached<T>> = [
this.default,
this.history,
];
private byteSize: number = 0;

public get totalBytes(): number {
Expand All @@ -82,7 +85,7 @@ export class AutocompleteCache {
}
}

public get lastAdded(): any[] {
public get lastAdded(): T[] {
switch (this.lastSearched) {
case "default":
return this.default.lastValue;
Expand All @@ -91,15 +94,15 @@ export class AutocompleteCache {
}
}

public static arraysEqual(arr1, arr2) {
public static arraysEqual(arr1: unknown, arr2: unknown): boolean {
return isEqual(arr1, arr2);
}

constructor(public readonly maxBytes: number = 250000) {
this.default = new Cached("default");
}

public get(key?: string): any[] {
public get(key?: string): T[] {
if (key) {
if (this.cached[key]) {
return this.cached[key].value;
Expand All @@ -111,7 +114,7 @@ export class AutocompleteCache {
}
}

public insert(value: any[], key?: string): any[] {
public insert(value: T[], key?: string): T[] {
if (key) {
return this.insertIntoCache(value, key);
} else {
Expand All @@ -136,7 +139,7 @@ export class AutocompleteCache {
this.default.clear();
}

private insertIntoCache(value: any, key: string): any[] {
private insertIntoCache(value: T[], key: string): T[] {
if (
this.cached[key] &&
AutocompleteCache.arraysEqual(value, this.cached[key].lastValue)
Expand All @@ -156,7 +159,7 @@ export class AutocompleteCache {
return this.cached[key].value;
}

private updateDefault(value: any): any[] {
private updateDefault(value: T[]): T[] {
if (AutocompleteCache.arraysEqual(value, this.default.lastValue)) {
return this.default.value;
}
Expand Down
Loading
Loading