Skip to content

Conversation

@EhsanKey
Copy link
Contributor

@EhsanKey EhsanKey commented Dec 5, 2025

Related to #90

@EhsanKey EhsanKey requested a review from ASafaeirad December 5, 2025 17:45
@EhsanKey EhsanKey self-assigned this Dec 5, 2025
@EhsanKey EhsanKey added the proposal New feature or request label Dec 5, 2025
@ASafaeirad ASafaeirad requested a review from Copilot December 19, 2025 12:44
@ASafaeirad
Copy link
Member

Hey @copilot, please review this PR

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements a once function that ensures a given function is executed only once, caching and returning the first invocation's result on subsequent calls.

  • Implements the once function with proper TypeScript generics and context preservation
  • Adds comprehensive test coverage including edge cases (async functions, errors, falsy values)
  • Provides documentation with usage examples

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/function/once.ts Core implementation of the once wrapper function
src/function/once.spec.ts Test suite covering various scenarios and edge cases
src/function/index.ts Exports the new once function
docs/pages/function/once.mdx User-facing documentation with examples
docs/pages/function/_meta.json Adds once to documentation navigation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

const onceFn = once(fn);

expect(() => onceFn()).toThrow('Test error');
expect(() => onceFn()).not.toThrow(); // Returns cached undefined
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

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

When the wrapped function throws an error on first call, the result variable remains undefined and subsequent calls return undefined instead of re-throwing the error. This behavior may be unexpected. Consider either: (1) storing the thrown error and re-throwing it on subsequent calls, or (2) not marking the function as called when an error occurs, allowing it to retry. The current implementation silently swallows errors after the first call.

Suggested change
expect(() => onceFn()).not.toThrow(); // Returns cached undefined
expect(() => onceFn()).toThrow('Test error'); // Rethrows cached error without calling fn again

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI commented Dec 19, 2025

@ASafaeirad I've opened a new pull request, #92, to work on those changes. Once the pull request is ready, I'll request review from you.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

proposal New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants