const searchStore = defineStore(({ query, reactive, effect }) => {
const usersRepo = new UsersRepo();
const search = reactive('');
const userQuery = query<User | null, void, string>((name) =>
usersRepo.getUserByName(name),
);
const debouncedRunQuery = debounce(
(searchValue: string) => {
userQuery.run(searchValue);
},
{ waitMs: 300 },
);
effect(() => {
const searchValue = search.get();
if (searchValue) {
debouncedRunQuery.call(searchValue);
}
});
const onSearch = (value: string) => search.set(value);
return {
search,
userQuery,
onSearch,
};
});