-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: withAtomEffect allows binding effects to atoms
- Loading branch information
David Maskasky
committed
Aug 24, 2024
1 parent
fdf3049
commit 76f2cf8
Showing
9 changed files
with
204 additions
and
88 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Component, type ErrorInfo, type ReactNode, createElement } from 'react' | ||
|
||
export function increment(count: number) { | ||
return count + 1 | ||
} | ||
|
||
export function incrementLetter(str: string) { | ||
return String.fromCharCode(increment(str.charCodeAt(0))) | ||
} | ||
|
||
export function delay(ms: number) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms) | ||
}) | ||
} | ||
|
||
export function assert(value: boolean, message?: string): asserts value { | ||
if (!value) { | ||
throw new Error(message ?? 'assertion failed') | ||
} | ||
} | ||
|
||
type ErrorBoundaryState = { | ||
hasError: boolean | ||
} | ||
type ErrorBoundaryProps = { | ||
componentDidCatch?: (error: Error, errorInfo: ErrorInfo) => void | ||
children: ReactNode | ||
} | ||
export class ErrorBoundary extends Component< | ||
ErrorBoundaryProps, | ||
ErrorBoundaryState | ||
> { | ||
state = { hasError: false } | ||
|
||
static getDerivedStateFromError(): ErrorBoundaryState { | ||
return { hasError: true } | ||
} | ||
|
||
componentDidCatch(error: Error, _errorInfo: ErrorInfo): void { | ||
this.props.componentDidCatch?.(error, _errorInfo) | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
return createElement('div', { children: 'error' }) | ||
} | ||
return this.props.children | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { atom, createStore } from 'jotai/vanilla' | ||
import { withAtomEffect } from '../src/withAtomEffect' | ||
import { delay } from './test-utils' | ||
|
||
describe('withAtomEffect', () => { | ||
it('ensures readonly atoms remain readonly', () => { | ||
const readOnlyAtom = atom(() => 10) | ||
const enhancedAtom = withAtomEffect(readOnlyAtom, () => {}) | ||
const store = createStore() | ||
store.sub(enhancedAtom, () => {}) | ||
expect(store.get(enhancedAtom)).toBe(10) | ||
expect(() => { | ||
// @ts-expect-error: should error | ||
store.set(enhancedAtom, 20) | ||
}).toThrow() | ||
}) | ||
|
||
it('ensures writable atoms remain writable', () => { | ||
const writableAtom = atom(0) | ||
const enhancedAtom = withAtomEffect(writableAtom, () => {}) | ||
const store = createStore() | ||
store.sub(enhancedAtom, () => {}) | ||
store.set(enhancedAtom, 5) | ||
expect(store.get(enhancedAtom)).toBe(5) | ||
}) | ||
|
||
it('calls effect on initial use and on dependencies change', async () => { | ||
const baseAtom = atom(0) | ||
const effectMock = jest.fn() | ||
const enhancedAtom = withAtomEffect(baseAtom, (get) => { | ||
effectMock() | ||
get(baseAtom) | ||
}) | ||
const store = createStore() | ||
store.sub(enhancedAtom, () => {}) | ||
await delay(0) | ||
expect(effectMock).toHaveBeenCalledTimes(1) | ||
store.set(baseAtom, 1) | ||
await delay(0) | ||
expect(effectMock).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
it('cleans up when the atom is no longer in use', async () => { | ||
const cleanupMock = jest.fn() | ||
const baseAtom = atom(0) | ||
const mountMock = jest.fn() | ||
const enhancedAtom = withAtomEffect(baseAtom, () => { | ||
mountMock() | ||
return () => { | ||
cleanupMock() | ||
} | ||
}) | ||
const store = createStore() | ||
const unsubscribe = store.sub(enhancedAtom, () => {}) | ||
await delay(0) | ||
expect(mountMock).toHaveBeenCalledTimes(1) | ||
unsubscribe() | ||
await delay(0) | ||
expect(cleanupMock).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Oops, something went wrong.