From 0bb092229a1c3716b4161a0f904e3b9982b8b804 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 19 Dec 2025 12:45:53 +0000 Subject: [PATCH 1/2] Initial plan From cc92d499bfbcdbc38b75ad1d2eb2f7694dda18af Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 19 Dec 2025 12:51:39 +0000 Subject: [PATCH 2/2] fix(once): correct error handling to match standard once semantics Co-authored-by: ASafaeirad <28571761+ASafaeirad@users.noreply.github.com> --- src/function/once.spec.ts | 19 ++++++++++++++++--- src/function/once.ts | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/function/once.spec.ts b/src/function/once.spec.ts index d1593d4..ab54aea 100644 --- a/src/function/once.spec.ts +++ b/src/function/once.spec.ts @@ -93,14 +93,27 @@ describe('once', () => { expect(fn).toHaveBeenCalledOnce(); }); - it('should handle functions that throw errors', () => { + it('should not cache errors and retry on subsequent calls', () => { + let callCount = 0; const fn = vi.fn(() => { - throw new Error('Test error'); + callCount++; + if (callCount < 2) { + throw new Error('Test error'); + } + return 'success'; }); const onceFn = once(fn); + // First call throws expect(() => onceFn()).toThrow('Test error'); - expect(() => onceFn()).not.toThrow(); // Returns cached undefined expect(fn).toHaveBeenCalledOnce(); + + // Second call succeeds and caches result + expect(onceFn()).toBe('success'); + expect(fn).toHaveBeenCalledTimes(2); + + // Third call returns cached result + expect(onceFn()).toBe('success'); + expect(fn).toHaveBeenCalledTimes(2); }); }); diff --git a/src/function/once.ts b/src/function/once.ts index 378ff02..e9bf120 100644 --- a/src/function/once.ts +++ b/src/function/once.ts @@ -36,8 +36,8 @@ export function once any>(fn: T): T { ...args: Parameters ): ReturnType { if (!called) { - called = true; result = fn.apply(this, args) as ReturnType; + called = true; return result; } return result;