feat(once): implement once function with documentation and tests#91
feat(once): implement once function with documentation and tests#91
Conversation
|
Hey @copilot, please review this PR |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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