Skip to content

Conversation

@davemecha
Copy link
Owner

@davemecha davemecha commented Jun 25, 2025

Summary by CodeRabbit

  • New Features

    • Introduced two React hooks: useAsyncEffekt for managing async side effects with cleanup and sequential execution, and useAsyncMemo for memoizing asynchronous computations with dependency tracking.
    • Added a main entry point for simplified imports of these hooks.
  • Documentation

    • Expanded and clarified the README with detailed usage examples, API references, feature lists, and ESLint configuration guidance.
  • Chores

    • Added .gitignore, package.json, and TypeScript configuration files to support project setup and build processes.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 25, 2025

Walkthrough

This update introduces a new React hooks package, including its source code, configuration, and documentation. The main features are two custom hooks—useAsyncEffekt for managing asynchronous effects with cleanup and sequencing, and useAsyncMemo for memoizing async computations. Supporting files for TypeScript, npm, and documentation are added.

Changes

File(s) Change Summary
.gitignore Added ignore rules for dependencies, build outputs, logs, IDE, OS, and runtime files.
README.md Expanded with detailed usage, API docs, ESLint config, and feature list for useAsyncEffekt and useAsyncMemo.
package.json New npm package manifest with metadata, scripts, dependencies, and repository info for "use-async-effekt".
src/index.ts New entry point re-exporting useAsyncEffekt and useAsyncMemo.
src/useAsyncEffekt.ts New hook: useAsyncEffekt for async effects with cleanup, sequencing, and race condition protection.
src/useAsyncMemo.ts New hook: useAsyncMemo for memoizing async computations, with error handling and mount checks.
tsconfig.json New TypeScript config targeting ES5/ES6, strict checks, React JSX, output to dist, and source from src.

Sequence Diagram(s)

sequenceDiagram
  participant Component
  participant useAsyncEffekt
  participant EffectFunction
  participant CleanupFunction

  Component->>useAsyncEffekt: invoke with effect and deps
  useAsyncEffekt->>EffectFunction: call effect({ isMounted, waitForPrevious })
  EffectFunction-->>useAsyncEffekt: (optional) returns cleanup
  useAsyncEffekt->>CleanupFunction: call cleanup on deps change or unmount
  CleanupFunction-->>useAsyncEffekt: cleanup completes
Loading
sequenceDiagram
  participant Component
  participant useAsyncMemo
  participant FactoryFunction

  Component->>useAsyncMemo: invoke with factory and deps
  useAsyncMemo->>FactoryFunction: call factory(isMounted)
  FactoryFunction-->>useAsyncMemo: resolves value
  useAsyncMemo-->>Component: returns memoized value (or undefined while loading)
Loading

Poem

In the warren of code, async bunnies leap,
With Effekt and Memo, their promises keep.
They clean up their burrows, race not in vain,
And memoize carrots for use once again.
With docs and configs, the garden’s complete—
Hooray for new hooks, so nimble and neat!
🥕✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b8df250 and 65d79da.

📒 Files selected for processing (2)
  • package.json (1 hunks)
  • tsconfig.json (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • package.json
🚧 Files skipped from review as they are similar to previous changes (1)
  • tsconfig.json

🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

coderabbitai bot added a commit that referenced this pull request Jun 25, 2025
Docstrings generation was requested by @davemecha.

* #1 (comment)

The following files were modified:

* `src/useAsyncEffekt.ts`
* `src/useAsyncMemo.ts`
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 25, 2025

Note

Generated docstrings for this pull request at #2

Copy link
Contributor

@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: 3

🧹 Nitpick comments (8)
.gitignore (1)

40-55: Add modern package-manager artefacts to ignore list

Teams frequently use Yarn v2/3 (creates a .yarn/ folder) or pnpm (pnpm-lock.yaml). These artefacts should not be committed and are safe to ignore alongside node_modules/.

 # Dependency directories
 jspm_packages/
+
+# Modern package-manager artefacts
+.yarn/
+pnpm-lock.yaml
.npmignore (1)

1-58: Redundant with the files field – consider deleting

Because package.json already whitelists "dist", "README.md", "LICENSE", nothing else will be shipped.
Keeping a large .npmignore increases maintenance cost without changing the published output.

Action: delete the file, or remove everything except an explanatory comment.

tsconfig.json (1)

3-18: Tighten compiler options for a library build

Minor tweaks can improve DX and tree-shaking:

     "target": "es5",
-    "allowJs": true,
+    "allowJs": false,          // no JS sources -> faster compile
     "skipLibCheck": true,
@@
-    "noEmit": false,
+    "module": "ES2015",        // better for bundlers; emit CJS via build-step if needed
+    "noEmit": false,
+    "declarationMap": true,    // jump-to-def in consumers

Feel free to ignore if you intentionally target CommonJS with an additional bundler step.

package.json (2)

12-16: Add a simple test placeholder or remove the test script

Currently the test script exits with code 1, breaking CI if someone runs npm test.

Quick fix:

-    "test": "echo \"Error: no test specified\" && exit 1"
+    "test": "echo \"(no tests)\""

Or wire up a test runner (e.g. vitest / jest) before publishing.


35-41: Pin peer-dependency range to avoid future breaking React releases

React 20+ may introduce breaking changes. A capped range is safer:

-    "react": ">=16.8.0"
+    "react": ">=16.8.0 <20"

Consumers can still override the peer range if needed.

src/useAsyncMemo.ts (1)

27-31: Consider making error handling configurable.

Logging errors directly to console might not be appropriate for all use cases, especially in production environments.

Consider accepting an optional error handler:

 export function useAsyncMemo<T>(
   factory: (isMounted: () => boolean) => Promise<T> | T,
-  deps?: DependencyList
+  deps?: DependencyList,
+  options?: { onError?: (error: unknown) => void }
 ): T | undefined {

Then use it in the catch block:

 } catch (error) {
   if (isMountedRef.current) {
-    console.error("useAsyncMemo error:", error);
+    if (options?.onError) {
+      options.onError(error);
+    } else {
+      console.error("useAsyncMemo error:", error);
+    }
     // Keep the last successful value on error
     setValue(lastSuccessfulValueRef.current);
   }
 }
src/useAsyncEffekt.ts (2)

101-105: Consider making error handling configurable.

Similar to useAsyncMemo, logging errors directly to console might not be appropriate for all use cases.

Consider accepting an optional error handler in the hook signature to allow custom error handling strategies.


83-137: Consider adding AbortController support for cancellable operations.

For effects that perform network requests or other cancellable operations, it would be beneficial to provide an AbortSignal.

Consider passing an AbortSignal to the effect function:

 effect: ({
   isMounted,
   waitForPrevious,
+  signal,
 }: {
   isMounted: () => boolean;
   waitForPrevious: () => Promise<void>;
+  signal: AbortSignal;
 }) => Promise<void | (() => void | Promise<void>)>,

This would allow users to properly cancel fetch requests and other abortable operations when the effect is cleaned up.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 002f0e8 and 687cab5.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (8)
  • .gitignore (1 hunks)
  • .npmignore (1 hunks)
  • README.md (1 hunks)
  • package.json (1 hunks)
  • src/index.ts (1 hunks)
  • src/useAsyncEffekt.ts (1 hunks)
  • src/useAsyncMemo.ts (1 hunks)
  • tsconfig.json (1 hunks)
🧰 Additional context used
🪛 LanguageTool
README.md

[style] ~231-~231: You have already used this phrasing in nearby sentences. Consider replacing it to add variety to your writing.
Context: ...nditions in sequential operations - You need to guarantee cleanup order Don't use `wai...

(REP_NEED_TO_VB)


[uncategorized] ~235-~235: Use a comma before ‘and’ if it connects two independent clauses (unless they are closely connected and short).
Context: ...` when: - Effects can run independently and concurrently - You want maximum perform...

(COMMA_COMPOUND_SENTENCE)

🔇 Additional comments (3)
src/index.ts (1)

1-2: Barrel file looks good

Re-exporting the hooks from a single entry point keeps the public API clean. No issues spotted.

README.md (1)

1-365: Excellent documentation!

The README provides comprehensive documentation with clear examples covering various use cases. The API reference accurately reflects the implementation, and the ESLint configuration section is particularly helpful for ensuring proper dependency tracking.

src/useAsyncEffekt.ts (1)

69-144: Well-designed sequential execution mechanism!

The promise chaining implementation elegantly handles sequential effect execution and cleanup ordering. The waitForPrevious functionality is particularly well-implemented.

@davemecha
Copy link
Owner Author

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 26, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@davemecha
Copy link
Owner Author

Just for clarity: I did not add the error handling suggestions, since this would change the signature of the functions too much and it would lose the linting support.

@davemecha davemecha merged commit dc1142f into main Jun 26, 2025
1 check passed
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.

2 participants