Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ts): add MaybePromise wrapper to make APIs shorter to read #333

Merged
merged 2 commits into from
Oct 12, 2020
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
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