Skip to content

Commit

Permalink
Optimize search index logic & add error and timeout handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
nopapername committed Mar 14, 2024
1 parent 488fb10 commit 7ac2c2d
Showing 1 changed file with 27 additions and 10 deletions.
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

0 comments on commit 7ac2c2d

Please sign in to comment.