Skip to content

Commit

Permalink
Flatten declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
timowestnosto committed Dec 18, 2024
1 parent ab0f76f commit 382784c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/utils/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DefaultState } from "./state"
type Items = NonNullable<DefaultState["history"]>

export function createHistory(size: number) {
const localStorageKey = "nostoAutocomplete:history"
const localStorageKey = "nosto:autocomplete:history"
let items = get()

function get(): Items {
Expand Down
76 changes: 42 additions & 34 deletions src/utils/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ export type StateActions<State> = {
clearHistory(): PromiseLike<State>
}

export const getStateActions = <State>({
export function getStateActions<State>({
config,
history,
input,
}: {
config: Required<AutocompleteConfig<State>>
history?: History
input: HTMLInputElement
}): StateActions<State> => {
}): StateActions<State> {
let cancellable: Cancellable<State> | undefined

const fetchState = (
Expand Down Expand Up @@ -75,39 +75,47 @@ export const getStateActions = <State>({
})
}

return {
updateState: (inputValue?: string, options?: SearchAutocompleteOptions): PromiseLike<State> => {
cancellable?.cancel()
function updateState(inputValue?: string, options?: SearchAutocompleteOptions): PromiseLike<State> {
cancellable?.cancel()

if (inputValue && inputValue.length >= config.minQueryLength) {
cancellable = makeCancellable(fetchState(inputValue, config, options))
return cancellable.promise
} else if (history) {
return getHistoryState(inputValue ?? "")
}
if (inputValue && inputValue.length >= config.minQueryLength) {
cancellable = makeCancellable(fetchState(inputValue, config, options))
return cancellable.promise
} else if (history) {
return getHistoryState(inputValue ?? "")
}

return (
// @ts-expect-error type mismatch
cancellable?.promise ?? Promise.resolve<State>({})
)
},
addHistoryItem: (item: string) => {
if (history) {
history.add(item)
}
return getHistoryState(input.value)
},
removeHistoryItem: (item: string) => {
if (history) {
history.remove(item)
}
return getHistoryState(input.value)
},
clearHistory: () => {
if (history) {
history.clear()
}
return getHistoryState(input.value)
},
return (
// @ts-expect-error type mismatch
cancellable?.promise ?? Promise.resolve<State>({})
)
}

function addHistoryItem(item: string): PromiseLike<State> {
if (history) {
history.add(item)
}
return getHistoryState(input.value)
}

function removeHistoryItem(item: string): PromiseLike<State> {
if (history) {
history.remove(item)
}
return getHistoryState(input.value)
}

function clearHistory(): PromiseLike<State> {
if (history) {
history.clear()
}
return getHistoryState(input.value)
}

return {
updateState,
addHistoryItem,
removeHistoryItem,
clearHistory,
}
}

0 comments on commit 382784c

Please sign in to comment.