-
Notifications
You must be signed in to change notification settings - Fork 18
feat(once): implement once function with documentation and tests #91
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
base: main
Are you sure you want to change the base?
Conversation
|
Hey @copilot, please review this PR |
There was a problem hiding this 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
oncefunction 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 |
Copilot
AI
Dec 19, 2025
There was a problem hiding this comment.
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.
| expect(() => onceFn()).not.toThrow(); // Returns cached undefined | |
| expect(() => onceFn()).toThrow('Test error'); // Rethrows cached error without calling fn again |
|
@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>
Related to #90