Add Result utility and fixed race condition in useDelayedToggleState - #27
Merged
Conversation
Export the Result class as a named export, add `map`, `mapErr`, `andThen`, `orElse`, `unwrapOr`, and `unwrapOrElse` methods, and change `isOk`/`isErr` to getter properties. Add default error mapper to `run`/`runAsync`, update `runAsync` to call `fn` directly without `.call(null)`, and fix an issue where `null`/`undefined` values were coalescing to the `empty` symbol. Also fix a race condition in `useDelayedToggleState` by clearing timeouts before setting new ones, add a `coverage` entry to `.gitignore`, install `@vitest/coverage-v8`, and add tests for the new Result methods.
Result utilityResult utility and fixed race condition in useDelayedToggleState
There was a problem hiding this comment.
Pull request overview
This PR introduces a new Result utility in @jabascript/core for functional error handling, and also fixes a race condition in @jabascript/react’s useDelayedToggleState by tracking/cancelling pending timeouts.
Changes:
- Add
Resultimplementation + tests and export it from@jabascript/core. - Fix rapid-toggle race condition in
useDelayedToggleStateand extend hook tests to cover the scenario. - Add Vitest v8 coverage dependency and ignore generated
coverage/output.
Reviewed changes
Copilot reviewed 8 out of 10 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| pnpm-lock.yaml | Locks new dev dependency graph changes (Vitest coverage/UI-related additions). |
| packages/react/tests/client.test.js | Adds a new rapid-toggling race-condition regression test for useDelayedToggleState. |
| packages/react/src/client.js | Fixes useDelayedToggleState by storing/clearing timeout handles and cleaning up on unmount. |
| packages/core/tests/result.test.js | Adds comprehensive tests for the new Result API surface (ok/err, map, andThen, etc.). |
| packages/core/src/result.js | Introduces Result implementation and documentation. |
| packages/core/package.json | Exports ./result entrypoint (types + import) for consumers. |
| package.json | Adds @vitest/coverage-v8 dev dependency at the workspace root. |
| .gitignore | Ignores coverage/ output. |
| .changeset/thirty-llamas-invent.md | Declares a patch release for @jabascript/react for the hook race-condition fix. |
| .changeset/eager-planets-admire.md | Declares a patch release for @jabascript/core for the new Result utility. |
Files not reviewed (1)
- pnpm-lock.yaml: Generated file
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+55
to
+58
| constructor(value, error) { | ||
| this.error = error ?? empty; | ||
| this.value = value ?? empty; | ||
| } |
Comment on lines
+11
to
+14
| * ## Falsy Values | ||
| * Because the constructor uses the nullish coalescing operator (`?? empty`) to fallback, | ||
| * passing `null` or `undefined` will cause them to coalesce to the `empty` symbol. | ||
| * Other falsy values (such as `0`, `false`, `""`) are preserved. |
Comment on lines
+123
to
+142
| it("should handle actual rapid toggling without race conditions", async () => { | ||
| const { result, act } = await renderHook(() => useDelayedToggleState(true, 100)); | ||
|
|
||
| // Start closing (toggled becomes false immediately) | ||
| await act(() => result.current[2](false)); | ||
| expect(result.current[0]).toBe(false); | ||
| expect(result.current[1]).toBe(true); | ||
|
|
||
| // Reopen immediately (after 10ms, before 100ms delay finishes) | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
| await act(() => result.current[2](true)); | ||
| expect(result.current[1]).toBe(true); | ||
|
|
||
| // Wait for states to settle (past the 100ms delay) | ||
| await new Promise((resolve) => setTimeout(resolve, 150)); | ||
|
|
||
| // Since we reopened, both should remain true | ||
| expect(result.current[0]).toBe(true); | ||
| expect(result.current[1]).toBe(true); | ||
| }); |
Comment on lines
+1
to
+5
| --- | ||
| "@jabascript/react": patch | ||
| --- | ||
|
|
||
| - Fix race condition bug in useDelayedToggleState hook by tracking and clearing pending timeouts. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.