Skip to content

Commit

Permalink
chore(ts): add MaybePromise wrapper to make APIs shorter to read (#333)
Browse files Browse the repository at this point in the history
* chore(ts): add MaybePromise wrapper to make APIs shorter to read

* Update packages/autocomplete-js/src/types/index.ts
  • Loading branch information
Haroenv authored Oct 12, 2020
1 parent 001243f commit b130b8e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
11 changes: 4 additions & 7 deletions packages/autocomplete-core/src/types/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AutocompleteAccessibilityGetters } from './getters';
import { AutocompleteSetters } from './setters';
import { AutocompleteState } from './state';
import { MaybePromise } from './wrappers';

export interface AutocompleteApi<
TItem,
Expand Down Expand Up @@ -92,7 +93,7 @@ export interface AutocompleteSource<TItem> {
/**
* Function called when the input changes. You can use this function to filter/search the items based on the query.
*/
getSuggestions(params: GetSourcesParams<TItem>): TItem[] | Promise<TItem[]>;
getSuggestions(params: GetSourcesParams<TItem>): MaybePromise<TItem[]>;
/**
* Function called when an item is selected.
*/
Expand Down Expand Up @@ -163,9 +164,7 @@ export type AutocompletePlugin<TItem, TData> = {
*/
getSources?(
params: GetSourcesParams<TItem>
):
| Array<AutocompleteSource<TItem>>
| Promise<Array<AutocompleteSource<TItem>>>;
): MaybePromise<Array<AutocompleteSource<TItem>>>;
/**
* The function called when the autocomplete form is submitted.
*/
Expand Down Expand Up @@ -244,9 +243,7 @@ export interface AutocompleteOptions<TItem> {
*/
getSources(
params: GetSourcesParams<TItem>
):
| Array<AutocompleteSource<TItem>>
| Promise<Array<AutocompleteSource<TItem>>>;
): MaybePromise<Array<AutocompleteSource<TItem>>>;
/**
* The environment from where your JavaScript is running.
* Useful if you're using autocomplete in a different context than
Expand Down
1 change: 1 addition & 0 deletions packages/autocomplete-core/src/types/wrappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type MaybePromise<TResolution> = Promise<TResolution> | TResolution;
1 change: 1 addition & 0 deletions packages/autocomplete-js/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type InternalAutocompleteSource<TItem> = InternalAutocompleteCoreSource<
type GetSources<TItem> = (
params: GetSourcesParams<TItem>
) =>
// TODO: reuse MaybePromise from autocomplete-core when we find a way to share the type
| Array<AutocompleteCoreSource<TItem>>
| Promise<Array<AutocompleteCoreSource<TItem>>>;

Expand Down

0 comments on commit b130b8e

Please sign in to comment.