Skip to content

Commit

Permalink
refactor(core): extract state enhancers from store (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour authored Oct 12, 2020
1 parent f62c084 commit a20887d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
12 changes: 12 additions & 0 deletions packages/autocomplete-core/src/completionStateEnhancer.ts
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 }),
};
}
3 changes: 2 additions & 1 deletion packages/autocomplete-core/src/createAutocomplete.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { completionStateEnhancer } from './completionStateEnhancer';
import { createStore } from './createStore';
import { getAutocompleteSetters } from './getAutocompleteSetters';
import { getDefaultProps } from './getDefaultProps';
Expand All @@ -15,7 +16,7 @@ export function createAutocomplete<
options: AutocompleteOptions<TItem>
): AutocompleteApi<TItem, TEvent, TMouseEvent, TKeyboardEvent> {
const props = getDefaultProps(options);
const store = createStore(stateReducer, props);
const store = createStore(stateReducer, props, [completionStateEnhancer]);

const {
setHighlightedIndex,
Expand Down
31 changes: 14 additions & 17 deletions packages/autocomplete-core/src/createStore.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
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> {
let state = props.initialState;
function enhanceState(state: AutocompleteState<TItem>) {
return stateEnhancers.reduce(
(nextState, stateEnhancer) => stateEnhancer(nextState, props),
state
);
}

let state = enhanceState(props.initialState);

return {
getState() {
return state;
},
send(action, payload) {
state = withCompletion(
state = enhanceState(
reducer(state, {
type: action,
props,
payload,
}),
props
})
);

props.onStateChange({ state });
},
};
}

function withCompletion<TItem>(
state: AutocompleteState<TItem>,
props: InternalAutocompleteOptions<TItem>
) {
return {
...state,
completion: getCompletion({ state, props }),
};
}
5 changes: 5 additions & 0 deletions packages/autocomplete-core/src/types/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ type ActionType =
| 'mousemove'
| 'mouseleave'
| 'click';

export type StateEnhancer<TItem> = (
state: AutocompleteState<TItem>,
props: InternalAutocompleteOptions<TItem>
) => AutocompleteState<TItem>;

0 comments on commit a20887d

Please sign in to comment.