Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move search to Web Worker #469

Merged
merged 1 commit into from
Nov 27, 2024
Merged

feat: move search to Web Worker #469

merged 1 commit into from
Nov 27, 2024

Conversation

weareoutman
Copy link
Member

@weareoutman weareoutman commented Nov 27, 2024

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a worker-based approach for improved search functionality, enhancing performance and responsiveness.
    • Added new asynchronous functions for fetching indexes and executing searches via a web worker.
  • Bug Fixes

    • Streamlined error handling and loading states for search operations.
  • Chores

    • Updated dependencies to support the new search implementation.
    • Removed outdated test files and functions to clean up the codebase.

These changes aim to provide a more efficient and user-friendly search experience within the application.

Copy link

coderabbitai bot commented Nov 27, 2024

Walkthrough

The changes in this pull request involve significant updates to the docusaurus-search-local package, including the addition of a new dependency, comlink, and the introduction of worker-based search functionality. Key files have been modified or removed, such as the SearchBar and SearchPage components, which now utilize asynchronous methods for fetching indexes and executing searches. New files, searchByWorker.ts and worker.ts, have been created to implement the worker-based architecture, enhancing the search capabilities of the application.

Changes

File Path Change Summary
docusaurus-search-local/package.json Added dependency: "comlink": "^4.4.2" in dependencies.
`docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx Updated to use worker-based methods: replaced fetchIndexes and SearchSourceFactory with fetchIndexesByWorker and searchByWorker. Streamlined error handling and search initialization.
`docusaurus-search-local/src/client/theme/SearchPage/SearchPage.tsx Replaced searchSource with searchByWorker for asynchronous search handling. Updated index fetching to use fetchIndexesByWorker.
`docusaurus-search-local/src/client/theme/SearchBar/fetchIndexes.spec.ts Deleted file containing unit tests for fetchIndexes.
`docusaurus-search-local/src/client/theme/SearchBar/fetchIndexes.ts Deleted file containing fetchIndexes and legacyFetchIndexes functions and related interfaces.
`docusaurus-search-local/src/client/theme/searchByWorker.ts New file introducing worker-based search functionality with fetchIndexesByWorker and searchByWorker methods.
`docusaurus-search-local/src/client/theme/worker.ts New file containing SearchWorker class for managing search operations and indexing with Lunr.js.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Nov 27, 2024

Deploy Preview for easyops-cn-docusaurus-search-local ready!

Name Link
🔨 Latest commit 15b7fba
🔍 Latest deploy log https://app.netlify.com/sites/easyops-cn-docusaurus-search-local/deploys/67470c4b973095000854ab31
😎 Deploy Preview https://deploy-preview-469--easyops-cn-docusaurus-search-local.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 6 out of 8 changed files in this pull request and generated no suggestions.

Files not reviewed (2)
  • docusaurus-search-local/package.json: Language not supported
  • docusaurus-search-local/src/client/theme/SearchBar/fetchIndexes.spec.ts: Evaluated as low risk
Comments skipped due to low confidence (3)

docusaurus-search-local/src/client/theme/worker.ts:37

  • [nitpick] The function name 'lowLevelFetchIndexes' is ambiguous. Consider renaming it to 'fetchIndexesFromCache' to better describe its purpose.
async lowLevelFetchIndexes(

docusaurus-search-local/src/client/theme/worker.ts:130

  • The error message 'Unexpected version url' could be more descriptive. Consider changing it to 'Unexpected version URL: The URL does not match the expected origin.'
throw new Error("Unexpected version url");

docusaurus-search-local/src/client/theme/worker.ts:32

  • The new 'SearchWorker' class and its methods should have corresponding test cases to ensure the new functionality is covered by tests.
export class SearchWorker {
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 11

🧹 Outside diff range and nitpick comments (6)
docusaurus-search-local/src/client/theme/searchByWorker.ts (1)

18-20: Consider improving type safety

The use of as any could be replaced with proper typing to maintain type safety.

-      ) as any;
+      ) as Comlink.Remote<typeof RemoteWorker>;
docusaurus-search-local/package.json (1)

42-42: LGTM! Good choice using Comlink for Worker communication.

The addition of Comlink is appropriate for implementing WebWorker-based search. This architectural change will improve main thread performance by offloading search operations to a worker thread.

Consider documenting the following in your README:

  • Browser compatibility requirements (WebWorker support)
  • Performance benefits of worker-based search
  • Fallback behavior if WebWorkers are not available
docusaurus-search-local/src/client/theme/worker.ts (1)

83-86: Ensure item.document.i is defined to prevent potential errors

In the filter condition, accessing item.document.i.toString() assumes that item.document.i is always defined. If it's undefined or null, this will cause a runtime error. Consider adding a null check to ensure safety.

Apply this diff to add a null check:

  !results.some(
    (item) =>
-     item.document.i.toString() === result.ref
+     item.document.i && item.document.i.toString() === result.ref
  )
docusaurus-search-local/src/client/theme/SearchPage/SearchPage.tsx (1)

96-96: Include updateSearchPath in the dependency array or memoize it.

The function updateSearchPath is used inside the useEffect but is not included in the dependency array. This can lead to stale closures or unexpected behavior. Consider memoizing updateSearchPath using useCallback or including it in the dependencies.

docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx (2)

150-152: Capture all resolved values from Promise.all or adjust the implementation

In the loadIndex function, you're using Promise.all but only destructuring autoComplete:

const [autoComplete] = await Promise.all([
  fetchAutoCompleteJS(),
  fetchIndexesByWorker(versionUrl, searchContext),
]);

This means the result of fetchIndexesByWorker is not captured. If fetchIndexesByWorker returns a value that's needed, consider capturing it. If it's only necessary to ensure the indexes are fetched before proceeding, and the return value isn't needed, you might separate the calls for clarity.

Consider one of the following adjustments:

Option 1: Capture both results

const [autoComplete, indexes] = await Promise.all([
  fetchAutoCompleteJS(),
  fetchIndexesByWorker(versionUrl, searchContext),
]);
// Use 'indexes' if needed

Option 2: Separate the calls if the result isn't needed

const autoComplete = await fetchAutoCompleteJS();
await fetchIndexesByWorker(versionUrl, searchContext);

This makes it explicit that fetchIndexesByWorker is called for its side effects.


261-266: Implement debouncing to improve performance

The source function calls searchByWorker on every input change without debouncing. Rapid input can result in a high number of calls, potentially impacting performance and overwhelming the worker.

Consider implementing a debounce mechanism:

+ import debounce from 'lodash.debounce';
...
          source: debounce(
            async (
              input: string,
              callback: (output: SearchResult[]) => void
            ) => {
              try {
                const result = await searchByWorker(
                  versionUrl,
                  searchContext,
                  input
                );
                callback(result);
              } catch (error) {
                console.error("Error during search:", error);
                callback([]);
              }
            },
+           300 // Debounce delay in milliseconds
          ),

This adds a 300ms delay, reducing the number of calls during rapid typing.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 3f28255 and 15b7fba.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (7)
  • docusaurus-search-local/package.json (1 hunks)
  • docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx (3 hunks)
  • docusaurus-search-local/src/client/theme/SearchBar/fetchIndexes.spec.ts (0 hunks)
  • docusaurus-search-local/src/client/theme/SearchBar/fetchIndexes.ts (0 hunks)
  • docusaurus-search-local/src/client/theme/SearchPage/SearchPage.tsx (4 hunks)
  • docusaurus-search-local/src/client/theme/searchByWorker.ts (1 hunks)
  • docusaurus-search-local/src/client/theme/worker.ts (1 hunks)
💤 Files with no reviewable changes (2)
  • docusaurus-search-local/src/client/theme/SearchBar/fetchIndexes.spec.ts
  • docusaurus-search-local/src/client/theme/SearchBar/fetchIndexes.ts
🔇 Additional comments (7)
docusaurus-search-local/src/client/theme/searchByWorker.ts (2)

1-11: LGTM! Clean interface design

The RemoteWorker interface is well-defined with proper TypeScript types and Promise return values.


19-19: Verify worker implementation

Ensure that the referenced worker file exists and implements the expected interface.

✅ Verification successful

Worker implementation verified successfully

The worker implementation is properly implemented in worker.ts with the expected interface:

  • The SearchWorker class implements both required methods: fetchIndexes and search
  • The worker is correctly exposed using Comlink
  • The implementation in searchByWorker.ts properly wraps and uses the worker
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if worker.js exists and contains the expected implementation
ast-grep --pattern 'class $_ {
  async fetchIndexes($_) {
    $$$
  }
  async search($_) {
    $$$
  }
}'

Length of output: 107


Script:

#!/bin/bash
# Let's first check if worker.js exists in the same directory
fd worker.js

# Then let's examine its content
rg -l "class.*Worker" --type ts
rg "onmessage|postMessage" --type ts -A 5 -B 5

Length of output: 148


Script:

#!/bin/bash
# Let's examine the content of the worker.ts file
cat docusaurus-search-local/src/client/theme/worker.ts

# Also check for any imports in searchByWorker.ts that might be related
cat docusaurus-search-local/src/client/theme/searchByWorker.ts

Length of output: 5762

docusaurus-search-local/package.json (1)

42-42: Verify the impact on bundle size.

Let's analyze the bundle size impact of adding Comlink.

✅ Verification successful

Comlink's bundle impact is minimal and acceptable

The analysis shows that Comlink is very lightweight:

  • Minified size: 4.85KB
  • Gzipped size: 2.03KB
  • Zero dependencies
  • Tree-shakeable (ESM support via dist/esm/comlink.mjs)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check the minified size of comlink to understand bundle impact

# Get the size of comlink's minified bundle
curl -s https://bundlephobia.com/api/size?[email protected] | jq '{
  size: .size,
  gzip: .gzip,
  dependencyCount: .dependencyCount
}'

# Verify if comlink is tree-shakeable
curl -s https://bundlephobia.com/api/size?[email protected] | jq '.hasJSModule'

Length of output: 312

docusaurus-search-local/src/client/theme/SearchPage/SearchPage.tsx (2)

11-11: Import statements updated to include worker-based functions.

The import of fetchIndexesByWorker and searchByWorker correctly integrates the new worker-based search functionality.


200-200: Loading indicator correctly reflects worker readiness.

The conditional rendering ensures the loading spinner is displayed while the search worker is initializing and a search query is present.

docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx (2)

19-19: Confirm the correctness of the new import statement

The import of fetchIndexesByWorker and searchByWorker from "../searchByWorker" appears correct based on the context of migrating to a worker-based approach. Ensure that the searchByWorker module exports these functions properly and that the file path is accurate.


261-266: Ensure worker termination to prevent memory leaks

If searchByWorker creates workers on each call, verify that workers are properly terminated when they're no longer needed. This prevents potential memory leaks, especially with continuous use.

Run the following script to check for worker termination in the codebase:

Ensure that for every Worker instance created, there is a corresponding terminate() call.

✅ Verification successful

Worker management is properly implemented - no memory leak concerns

The implementation uses a singleton pattern with proper caching:

  • A single worker instance is created and reused via getRemoteWorker()
  • The worker is wrapped using Comlink and cached in remoteWorkerPromise
  • The worker maintains its own cache of fetched indexes using cache Map
  • No explicit termination is needed as the worker is kept alive intentionally for reuse

Since the worker is reused throughout the application lifecycle rather than creating new workers for each search, there is no risk of memory leaks from unterminated workers.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for `Worker` instances and ensure that `terminate()` is called.

# Find all TypeScript files
fd -e ts

# Search for Worker creation
rg 'new Worker\(' -g '*.ts'

# Check if terminate() is called on workers
rg 'worker\.terminate\(' -g '*.ts'

Length of output: 4678


Script:

#!/bin/bash
# Let's examine the worker implementation and usage more thoroughly

# First, check the content of searchByWorker.ts to understand worker lifecycle
cat docusaurus-search-local/src/client/theme/searchByWorker.ts

# Also check the worker implementation
cat docusaurus-search-local/src/client/theme/worker.ts

# Look for any worker cleanup in SearchBar component
rg -A 5 -B 5 'searchByWorker' docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx

Length of output: 6580

Comment on lines +37 to +47
export async function searchByWorker(
baseUrl: string,
searchContext: string,
input: string
): Promise<SearchResult[]> {
if (process.env.NODE_ENV === "production") {
const remoteWorker = await getRemoteWorker();
return remoteWorker!.search(baseUrl, searchContext, input);
}
return [];
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for search operation

Similar to fetchIndexesByWorker, this function should handle potential errors gracefully.

 export async function searchByWorker(
   baseUrl: string,
   searchContext: string,
   input: string
 ): Promise<SearchResult[]> {
   if (process.env.NODE_ENV === "production") {
-    const remoteWorker = await getRemoteWorker();
-    return remoteWorker!.search(baseUrl, searchContext, input);
+    try {
+      const remoteWorker = await getRemoteWorker();
+      return await remoteWorker.search(baseUrl, searchContext, input);
+    } catch (error) {
+      console.error('Search operation failed:', error);
+      return [];
+    }
   }
   return [];
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export async function searchByWorker(
baseUrl: string,
searchContext: string,
input: string
): Promise<SearchResult[]> {
if (process.env.NODE_ENV === "production") {
const remoteWorker = await getRemoteWorker();
return remoteWorker!.search(baseUrl, searchContext, input);
}
return [];
}
export async function searchByWorker(
baseUrl: string,
searchContext: string,
input: string
): Promise<SearchResult[]> {
if (process.env.NODE_ENV === "production") {
try {
const remoteWorker = await getRemoteWorker();
return await remoteWorker.search(baseUrl, searchContext, input);
} catch (error) {
console.error('Search operation failed:', error);
return [];
}
}
return [];
}

Comment on lines +15 to +25
function getRemoteWorker() {
if (!remoteWorkerPromise) {
remoteWorkerPromise = (async () => {
const Remote = Comlink.wrap(
new Worker(new URL("./worker.js", import.meta.url))
) as any;
return await new Remote();
})();
}
return remoteWorkerPromise;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for worker initialization

The worker initialization could fail for various reasons (e.g., worker file not found). Consider adding error handling.

 function getRemoteWorker() {
   if (!remoteWorkerPromise) {
     remoteWorkerPromise = (async () => {
-      const Remote = Comlink.wrap(
-        new Worker(new URL("./worker.js", import.meta.url))
-      ) as any;
-      return await new Remote();
+      try {
+        const Remote = Comlink.wrap(
+          new Worker(new URL("./worker.js", import.meta.url))
+        ) as Comlink.Remote<typeof RemoteWorker>;
+        return await new Remote();
+      } catch (error) {
+        console.error('Failed to initialize search worker:', error);
+        throw error;
+      }
     })();
   }
   return remoteWorkerPromise;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function getRemoteWorker() {
if (!remoteWorkerPromise) {
remoteWorkerPromise = (async () => {
const Remote = Comlink.wrap(
new Worker(new URL("./worker.js", import.meta.url))
) as any;
return await new Remote();
})();
}
return remoteWorkerPromise;
}
function getRemoteWorker() {
if (!remoteWorkerPromise) {
remoteWorkerPromise = (async () => {
try {
const Remote = Comlink.wrap(
new Worker(new URL("./worker.js", import.meta.url))
) as Comlink.Remote<typeof RemoteWorker>;
return await new Remote();
} catch (error) {
console.error('Failed to initialize search worker:', error);
throw error;
}
})();
}
return remoteWorkerPromise;
}

Comment on lines +27 to +35
export async function fetchIndexesByWorker(
baseUrl: string,
searchContext: string
): Promise<void> {
if (process.env.NODE_ENV === "production") {
const remoteWorker = await getRemoteWorker();
await remoteWorker!.fetchIndexes(baseUrl, searchContext);
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling and return type consistency

The function could benefit from proper error handling and consistent return types.

 export async function fetchIndexesByWorker(
   baseUrl: string,
   searchContext: string
 ): Promise<void> {
   if (process.env.NODE_ENV === "production") {
-    const remoteWorker = await getRemoteWorker();
-    await remoteWorker!.fetchIndexes(baseUrl, searchContext);
+    try {
+      const remoteWorker = await getRemoteWorker();
+      await remoteWorker.fetchIndexes(baseUrl, searchContext);
+    } catch (error) {
+      console.error('Failed to fetch indexes:', error);
+      throw error;
+    }
+  } else {
+    // Explicitly return in non-production environment
+    return Promise.resolve();
   }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export async function fetchIndexesByWorker(
baseUrl: string,
searchContext: string
): Promise<void> {
if (process.env.NODE_ENV === "production") {
const remoteWorker = await getRemoteWorker();
await remoteWorker!.fetchIndexes(baseUrl, searchContext);
}
}
export async function fetchIndexesByWorker(
baseUrl: string,
searchContext: string
): Promise<void> {
if (process.env.NODE_ENV === "production") {
try {
const remoteWorker = await getRemoteWorker();
await remoteWorker.fetchIndexes(baseUrl, searchContext);
} catch (error) {
console.error('Failed to fetch indexes:', error);
throw error;
}
} else {
// Explicitly return in non-production environment
return Promise.resolve();
}
}

Comment on lines +91 to +93
(doc) => doc.i.toString() === result.ref
) as SearchDocument;
return {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Check for undefined when finding documents to prevent runtime errors

The find method may return undefined if the document is not found. Casting it directly to SearchDocument without checking can cause runtime errors. Add a check to ensure that document is defined before proceeding.

Apply this diff to add a check:

  const document = documents.find(
    (doc) => doc.i.toString() === result.ref
- ) as SearchDocument;
+ );
+ if (!document) {
+   // Handle the missing document scenario
+   return null; // or continue to next iteration
+ }
+ // Now it's safe to assert the type
+ const searchDocument = document as SearchDocument;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
(doc) => doc.i.toString() === result.ref
) as SearchDocument;
return {
const document = documents.find(
(doc) => doc.i.toString() === result.ref
);
if (!document) {
// Handle the missing document scenario
return null; // or continue to next iteration
}
// Now it's safe to assert the type
const searchDocument = document as SearchDocument;
return {

Comment on lines +60 to +63
const { wrappedIndexes, zhDictionary } = await this.lowLevelFetchIndexes(
baseUrl,
searchContext
);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling when fetching indexes in the search method

The call to this.lowLevelFetchIndexes may throw an error if fetching indexes fails. Currently, there's no error handling, which could lead to unhandled promise rejections. Wrap the call in a try-catch block to handle errors gracefully.

Apply this diff to add error handling:

  let wrappedIndexes, zhDictionary;
  try {
    const data = await this.lowLevelFetchIndexes(
      baseUrl,
      searchContext
    );
    wrappedIndexes = data.wrappedIndexes;
    zhDictionary = data.zhDictionary;
  } catch (error) {
    // Handle the error appropriately, e.g., log it or return an empty array
    console.error('Failed to fetch indexes:', error);
    return [];
  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const { wrappedIndexes, zhDictionary } = await this.lowLevelFetchIndexes(
baseUrl,
searchContext
);
let wrappedIndexes, zhDictionary;
try {
const data = await this.lowLevelFetchIndexes(
baseUrl,
searchContext
);
wrappedIndexes = data.wrappedIndexes;
zhDictionary = data.zhDictionary;
} catch (error) {
// Handle the error appropriately, e.g., log it or return an empty array
console.error('Failed to fetch indexes:', error);
return [];
}

Comment on lines +145 to +147
if (/\p{Unified_Ideograph}/u.test(tuple[0][0])) {
acc.add(tuple[0]);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Check for undefined elements before accessing to prevent runtime errors

When accessing tuple[0][0], there's an assumption that tuple[0] and tuple[0][0] exist. If the data is malformed, this could cause a runtime error. Add checks to ensure these elements are defined.

Apply this diff to add safety checks:

  for (const tuple of item.index.invertedIndex) {
-   if (/\p{Unified_Ideograph}/u.test(tuple[0][0])) {
+   if (
+     tuple[0] &&
+     tuple[0][0] &&
+     /\p{Unified_Ideograph}/u.test(tuple[0][0])
+   ) {
      acc.add(tuple[0]);
    }
  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (/\p{Unified_Ideograph}/u.test(tuple[0][0])) {
acc.add(tuple[0]);
}
if (
tuple[0] &&
tuple[0][0] &&
/\p{Unified_Ideograph}/u.test(tuple[0][0])
) {
acc.add(tuple[0]);
}

Comment on lines +133 to +134
const json = (await (await fetch(url)).json()) as SerializedIndex[];

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for fetch and JSON parsing operations

The fetch request and JSON parsing don't have error handling. Network failures or invalid JSON responses can cause unhandled exceptions. Wrap these operations in try-catch blocks to handle errors gracefully.

Apply this diff to add error handling:

- const json = (await (await fetch(url)).json()) as SerializedIndex[];
+ let response;
+ try {
+   response = await fetch(url);
+ } catch (error) {
+   throw new Error(`Failed to fetch indexes from ${url}: ${error.message}`);
+ }
+ let json;
+ try {
+   json = (await response.json()) as SerializedIndex[];
+ } catch (error) {
+   throw new Error(`Failed to parse JSON from ${url}: ${error.message}`);
+ }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const json = (await (await fetch(url)).json()) as SerializedIndex[];
let response;
try {
response = await fetch(url);
} catch (error) {
throw new Error(`Failed to fetch indexes from ${url}: ${error.message}`);
}
let json;
try {
json = (await response.json()) as SerializedIndex[];
} catch (error) {
throw new Error(`Failed to parse JSON from ${url}: ${error.message}`);
}

Comment on lines +115 to +122
if (
!Array.isArray(searchContextByPaths) ||
searchContext ||
useAllContextsWithNoSearchContext
? await fetchIndexes(versionUrl, searchContext)
: { wrappedIndexes: [], zhDictionary: [] };
setSearchSource(() =>
SearchSourceFactory(wrappedIndexes, zhDictionary, 100)
);
) {
await fetchIndexesByWorker(versionUrl, searchContext);
}
setSearchWorkerReady(true);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add missing dependencies to the useEffect dependency array.

The variables searchContextByPaths and useAllContextsWithNoSearchContext are used inside the useEffect but are not included in the dependency array. To ensure the effect runs correctly when these variables change, include them in the dependencies.

Apply this diff to include the missing dependencies:

 useEffect(() => {
   async function doFetchIndexes() {
     if (
       !Array.isArray(searchContextByPaths) ||
       searchContext ||
       useAllContextsWithNoSearchContext
     ) {
       await fetchIndexesByWorker(versionUrl, searchContext);
     }
     setSearchWorkerReady(true);
   }
   doFetchIndexes();
-}, [searchContext, versionUrl]);
+}, [searchContext, versionUrl, searchContextByPaths, useAllContextsWithNoSearchContext]);

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +81 to +91
if (searchQuery) {
(async () => {
const results = await searchByWorker(
versionUrl,
searchContext,
searchQuery
);
setSearchResults(results);
})();
} else {
setSearchResults(undefined);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Refactor: Avoid defining async functions inside useEffect.

Defining an async function inside a useEffect hook can lead to unintentional side effects. Consider refactoring to define the async function outside the hook.

Apply this diff to refactor the code:

 useEffect(() => {
   updateSearchPath(searchQuery);

-  if (searchQuery) {
-    (async () => {
-      const results = await searchByWorker(
-        versionUrl,
-        searchContext,
-        searchQuery
-      );
-      setSearchResults(results);
-    })();
-  } else {
-    setSearchResults(undefined);
-  }
+  const fetchResults = async () => {
+    if (searchQuery) {
+      const results = await searchByWorker(
+        versionUrl,
+        searchContext,
+        searchQuery
+      );
+      setSearchResults(results);
+    } else {
+      setSearchResults(undefined);
+    }
+  };
+  fetchResults();
 }, [searchQuery, versionUrl, searchContext]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (searchQuery) {
(async () => {
const results = await searchByWorker(
versionUrl,
searchContext,
searchQuery
);
setSearchResults(results);
})();
} else {
setSearchResults(undefined);
const fetchResults = async () => {
if (searchQuery) {
const results = await searchByWorker(
versionUrl,
searchContext,
searchQuery
);
setSearchResults(results);
} else {
setSearchResults(undefined);
}
};
fetchResults();

Comment on lines +257 to +267
source: async (
input: string,
callback: (output: SearchResult[]) => void
) => {
const result = await searchByWorker(
versionUrl,
searchContext,
input
);
callback(result);
},
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add error handling to the asynchronous source function

The source function used in the autocomplete configuration is asynchronous but lacks error handling. If searchByWorker throws an error, it could lead to unhandled promise rejections, impacting the user experience.

Apply this diff to include error handling:

          source: async (
            input: string,
            callback: (output: SearchResult[]) => void
          ) => {
+           try {
              const result = await searchByWorker(
                versionUrl,
                searchContext,
                input
              );
              callback(result);
+           } catch (error) {
+             console.error("Error during search:", error);
+             callback([]);
+           }
          },

This ensures that any errors are caught, logged, and the autocomplete doesn't break.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
source: async (
input: string,
callback: (output: SearchResult[]) => void
) => {
const result = await searchByWorker(
versionUrl,
searchContext,
input
);
callback(result);
},
source: async (
input: string,
callback: (output: SearchResult[]) => void
) => {
try {
const result = await searchByWorker(
versionUrl,
searchContext,
input
);
callback(result);
} catch (error) {
console.error("Error during search:", error);
callback([]);
}
},

@weareoutman weareoutman merged commit 5a241b9 into master Nov 27, 2024
7 checks passed
@weareoutman weareoutman deleted the steve/webworker branch November 27, 2024 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant