11import { readValue } from '../util/util' ;
22
33// Define useful types to avoid any.
4- export type LookupFn < T > = ( query :string ) => Promise < T [ ] >
4+ export type LookupFn < T > = ( query :string ) => Promise < T [ ] > | Promise < T > ;
5+ export type QueryLookupFn < T > = ( query :string ) => Promise < T [ ] > ;
6+ export type ItemLookupFn < T , U > = ( query :string , initial :U ) => Promise < T > ;
7+ export type ItemsLookupFn < T , U > = ( query :string , initial :U [ ] ) => Promise < T [ ] > ;
8+
59type CachedArray < T > = { [ query :string ] :T [ ] } ;
610
7- // T extends JavascriptObject so we can do a recursive search on the object.
811export class SearchService < T > {
912 // Stores the available options.
1013 private _options :T [ ] ;
@@ -36,6 +39,22 @@ export class SearchService<T> {
3639 this . reset ( ) ;
3740 }
3841
42+ public get queryLookup ( ) {
43+ return this . _optionsLookup as QueryLookupFn < T > ;
44+ }
45+
46+ public get hasItemLookup ( ) {
47+ return this . optionsLookup && this . optionsLookup . length == 2 ;
48+ }
49+
50+ public itemLookup < U > ( initial :U ) {
51+ return ( this . _optionsLookup as ItemLookupFn < T , U > ) ( undefined , initial ) ;
52+ }
53+
54+ public itemsLookup < U > ( initial :U [ ] ) {
55+ return ( this . _optionsLookup as ItemsLookupFn < T , U > ) ( undefined , initial ) ;
56+ }
57+
3958 public get optionsField ( ) {
4059 return this . _optionsField
4160 }
@@ -112,7 +131,7 @@ export class SearchService<T> {
112131 if ( this . _optionsLookup ) {
113132 this . _isSearching = true ;
114133
115- this . _optionsLookup ( this . _query )
134+ this . queryLookup ( this . _query )
116135 . then ( results => {
117136 // Unset 'loading' state, and display & cache the results.
118137 this . _isSearching = false ;
@@ -170,12 +189,9 @@ export class SearchService<T> {
170189
171190 // Resets the search back to a pristine state.
172191 private reset ( ) {
173- this . _query = "" ;
174192 this . _results = [ ] ;
175- if ( this . allowEmptyQuery ) {
176- this . _results = this . _options ;
177- }
178193 this . _resultsCache = { } ;
179194 this . _isSearching = false ;
195+ this . updateQuery ( "" ) ;
180196 }
181197}
0 commit comments