Skip to content

Commit

Permalink
Type improvements (#1746)
Browse files Browse the repository at this point in the history
* Make array arguments immutable

Type the array component arguments as immutable. The component doesn't actually mutate them, so typing them as readonly allows caller to pass in either mutable or immutable arrays, rather than only mutable ones.

* Use Promise instead of PromiseProxy

A couple of things were typed as `PromiseProxy`s when they only used the `Promise` interface. This prevented callers from, for example, supplying a search function that produces a regular promise, i.e. a plain old async function. So change them to `Promise`.

The `@selected` argument still uses a `PromiseProxy` because the component observes its `content` property, so allowing it to be a `Promise` would be a more involved code change.
  • Loading branch information
bendemboski authored Mar 18, 2024
1 parent 96f5c57 commit 9348552
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions ember-power-select/src/components/power-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ interface SelectActions extends DropdownActions {
export interface Select extends Dropdown {
selected: any;
highlighted: any;
options: any[];
results: any[];
options: readonly any[];
results: readonly any[];
resultsCount: number;
loading: boolean;
isActive: boolean;
Expand All @@ -66,7 +66,7 @@ export interface PowerSelectArgs {
noMatchesMessageComponent?: string | ComponentLike<any>;
matchTriggerWidth?: boolean;
resultCountMessage?: (resultCount: number) => string;
options?: any[] | PromiseProxy<any[]>;
options?: readonly any[] | Promise<readonly any[]>;
selected?: any | PromiseProxy<any>;
destination?: string;
closeOnSelect?: boolean;
Expand Down Expand Up @@ -114,7 +114,10 @@ export interface PowerSelectArgs {
typeAheadOptionMatcher?: MatcherFn;
buildSelection?: (selected: any, select: Select) => any;
onChange: (selection: any, select: Select, event?: Event) => void;
search?: (term: string, select: Select) => any[] | PromiseProxy<any[]>;
search?: (
term: string,
select: Select,
) => readonly any[] | Promise<readonly any[]>;
onOpen?: (select: Select, e: Event) => boolean | undefined;
onClose?: (select: Select, e: Event) => boolean | undefined;
onInput?: (term: string, select: Select, e: Event) => string | false | void;
Expand Down Expand Up @@ -165,11 +168,11 @@ export default class PowerSelectComponent extends Component<PowerSelectSignature
};

// Tracked properties
@tracked private _resolvedOptions?: any[];
@tracked private _resolvedOptions?: readonly any[];
@tracked private _resolvedSelected?: any;
@tracked private _repeatingChar = '';
@tracked private _expirableSearchText = '';
@tracked private _searchResult?: any[];
@tracked private _searchResult?: readonly any[];
@tracked isActive = false;
@tracked loading = false;
@tracked searchText = '';
Expand All @@ -178,9 +181,11 @@ export default class PowerSelectComponent extends Component<PowerSelectSignature
storedAPI!: Select;

private _uid = guidFor(this);
private _lastOptionsPromise?: PromiseProxy<any[]>;
private _lastOptionsPromise?: Promise<readonly any[]>;
private _lastSelectedPromise?: PromiseProxy<any>;
private _lastSearchPromise?: PromiseProxy<any[]> | CancellablePromise<any[]>;
private _lastSearchPromise?:
| Promise<readonly any[]>
| CancellablePromise<readonly any[]>;
private _filterResultsCache: {
results: any[];
options: any[];
Expand Down Expand Up @@ -501,7 +506,7 @@ export default class PowerSelectComponent extends Component<PowerSelectSignature
if (isPromiseLike(this.args.options)) {
if (this._lastOptionsPromise === this.args.options) return; // promise is still the same
const currentOptionsPromise = this.args.options;
this._lastOptionsPromise = currentOptionsPromise as PromiseProxy<any[]>;
this._lastOptionsPromise = currentOptionsPromise;
this.loading = true;
this._lastOptionsPromise
.then((resolvedOptions) => {
Expand Down Expand Up @@ -791,7 +796,7 @@ export default class PowerSelectComponent extends Component<PowerSelectSignature
}

findWithOffset(
options: any[],
options: readonly any[],
term: string,
offset: number,
skipDisabled = false,
Expand Down

0 comments on commit 9348552

Please sign in to comment.