Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Optimize search index logic & add error and timeout handling. #891

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions svelte/src/libs/search-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import log from "$libs/logger";
let packagesIndex: Fuse<GUIPackage>;
let ready = false;

// Set search timeout (milliseconds)
const SEARCH_TIMEOUT_MS = 10000;

export function indexPackages(packages: GUIPackage[]) {
try {
packagesIndex = new Fuse(packages, {
Expand Down Expand Up @@ -46,22 +49,36 @@ export function indexPackages(packages: GUIPackage[]) {
export async function searchPackages(term: string, limit = 5): Promise<GUIPackage[]> {
await isIndexReady();
if (!term || !packagesIndex) return [];
// TODO: if online, use algolia else use Fuse
const res = packagesIndex.search(term, { limit });
const matchingPackages: GUIPackage[] = res.map((v) => v.item);
return matchingPackages;

// Use try-catch to catch exceptions in asynchronous operations
try {
// Set timeout using Promise.race
const matchingPackages = await Promise.race([
packagesIndex.search(term, { limit }), // TODO: if online, use algolia else use Fuse
new Promise((resolve) => setTimeout(resolve, SEARCH_TIMEOUT_MS))
]);

// Return matching packages
return matchingPackages.map((v) => v.item);
} catch (error) {
console.error("Error searching packages:", error);
return [];
}
}

export async function isIndexReady(): Promise<boolean> {
if (ready) return true;
return new Promise((resolve) => {
const intervalCancel = setInterval(() => {
export async function isIndexReady(): Promise<void> {
// If the index is ready, return immediately
if (ready) return;

// Set a timer to check whether the index is ready every second
await new Promise((resolve) => {
const interval = setInterval(() => {
if (packagesIndex) {
const [grep] = packagesIndex.search("grep");
if (grep) {
clearInterval(intervalCancel);
clearInterval(interval);
ready = true;
resolve(true);
resolve();
}
}
}, 1000);
Expand Down