-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'next' into feat/prev-state
- Loading branch information
Showing
13 changed files
with
81 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { getCompletion } from './getCompletion'; | ||
import { AutocompleteState, InternalAutocompleteOptions } from './types'; | ||
|
||
export function completionStateEnhancer<TItem>( | ||
state: AutocompleteState<TItem>, | ||
props: InternalAutocompleteOptions<TItem> | ||
) { | ||
return { | ||
...state, | ||
completion: getCompletion({ state, props }), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,40 @@ | ||
import { getCompletion } from './getCompletion'; | ||
import { | ||
InternalAutocompleteOptions, | ||
AutocompleteState, | ||
AutocompleteStore, | ||
InternalAutocompleteOptions, | ||
Reducer, | ||
StateEnhancer, | ||
} from './types'; | ||
|
||
export function createStore<TItem>( | ||
reducer: Reducer, | ||
props: InternalAutocompleteOptions<TItem> | ||
props: InternalAutocompleteOptions<TItem>, | ||
stateEnhancers: Array<StateEnhancer<TItem>> | ||
): AutocompleteStore<TItem> { | ||
function enhanceState(state: AutocompleteState<TItem>) { | ||
return stateEnhancers.reduce( | ||
(nextState, stateEnhancer) => stateEnhancer(nextState, props), | ||
state | ||
); | ||
} | ||
|
||
let state = enhanceState(props.initialState); | ||
|
||
return { | ||
state: props.initialState, | ||
getState() { | ||
return this.state; | ||
return state; | ||
}, | ||
send(action, payload) { | ||
const prevState = { ...this.state }; | ||
this.state = withCompletion( | ||
reducer({ type: action, value: payload }, this.state, props), | ||
props | ||
const prevState = { ...state }; | ||
state = enhanceState( | ||
reducer(state, { | ||
type: action, | ||
props, | ||
payload, | ||
}) | ||
); | ||
|
||
props.onStateChange({ state: this.state, prevState }); | ||
props.onStateChange({ state, prevState }); | ||
}, | ||
}; | ||
} | ||
|
||
function withCompletion<TItem>( | ||
state: AutocompleteState<TItem>, | ||
props: InternalAutocompleteOptions<TItem> | ||
) { | ||
return { | ||
...state, | ||
completion: getCompletion({ state, props }), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type MaybePromise<TResolution> = Promise<TResolution> | TResolution; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/autocomplete-preset-algolia/src/search/__tests__/search.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
packages/autocomplete-preset-algolia/src/search/getAlgoliaResults.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { MultipleQueriesResponse } from '@algolia/client-search'; | ||
import { SearchResponse } from '@algolia/client-search'; | ||
|
||
import { search, SearchParams } from './search'; | ||
|
||
export function getAlgoliaResults<THit>({ | ||
export function getAlgoliaResults<TRecord>({ | ||
searchClient, | ||
queries, | ||
}: SearchParams): Promise<MultipleQueriesResponse<THit>['results']> { | ||
return search<THit>({ searchClient, queries }).then((response) => { | ||
}: SearchParams): Promise<Array<SearchResponse<TRecord>>> { | ||
return search<TRecord>({ searchClient, queries }).then((response) => { | ||
return response.results; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters