Skip to content

Commit

Permalink
fix: search input test and refactor debounce use
Browse files Browse the repository at this point in the history
  • Loading branch information
andreadlgdo committed May 21, 2024
1 parent 3bd4c74 commit 34520bf
Showing 1 changed file with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import { ArrowKey, PropsWithType } from '../../../utils';
import { debounce } from '../../../utils/debounce';
import { DebouncedFunction } from '../../../utils/types';
import { XEventsTypes } from '../../../wiring/events.types';
import { XEvent, XEventsTypes } from '../../../wiring/events.types';
import { WireMetadata } from '../../../wiring/wiring.types';
import { use$x } from '../../../composables/use-$x';
import { useRegisterXModule } from '../../../composables/use-register-x-module';
Expand Down Expand Up @@ -91,7 +91,7 @@
const inputElement = ref<HTMLInputElement>();
const debouncedUserAcceptedAQuery = ref<DebouncedFunction<[string]>>();
let debouncedUserAcceptedAQuery: DebouncedFunction<[string]>;
/**
* Generates the {@link WireMetadata} object omitting the moduleName.
Expand Down Expand Up @@ -128,12 +128,12 @@
const emitDebouncedUserAcceptedAQuery = (query: string): void => {
if (props.instant) {
if (!debouncedUserAcceptedAQuery) {
Object.assign(
debouncedUserAcceptedAQuery,
debounce(emitUserAcceptedAQuery.bind(this), props.instantDebounceInMs)
debouncedUserAcceptedAQuery = debounce(
emitUserAcceptedAQuery,
props.instantDebounceInMs
);
}
debouncedUserAcceptedAQuery.value?.(query);
debouncedUserAcceptedAQuery(query);
}
};
Expand Down Expand Up @@ -248,8 +248,9 @@
*
* @internal
*/
$x.on('UserReachedEmpathizeTop').subscribe(focusInput);
$x.on('UserPressedClearSearchBoxButton').subscribe(focusInput);
['UserReachedEmpathizeTop', 'UserPressedClearSearchBoxButton'].forEach(event =>
$x.on(event as XEvent).subscribe(focusInput)
);
function focusInput(): void {
inputElement.value?.focus();
}
Expand All @@ -261,10 +262,11 @@
*
* @internal
*/
$x.on('UserAcceptedAQuery').subscribe(cancelDebouncedUserAcceptedAQuery);
$x.on('UserClearedQuery').subscribe(cancelDebouncedUserAcceptedAQuery);
['UserAcceptedAQuery', 'UserClearedQuery'].forEach(event =>
$x.on(event as XEvent).subscribe(cancelDebouncedUserAcceptedAQuery)
);
function cancelDebouncedUserAcceptedAQuery(): void {
debouncedUserAcceptedAQuery.value?.cancel();
debouncedUserAcceptedAQuery?.cancel();
}
onMounted(() => {
Expand Down

0 comments on commit 34520bf

Please sign in to comment.