-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(observable-hooks): add useLayoutObservableState
- Loading branch information
Showing
8 changed files
with
371 additions
and
29 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
190 changes: 190 additions & 0 deletions
190
packages/observable-hooks/__tests__/use-layout-observable-state.spec.ts
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,190 @@ | ||
import { useLayoutObservableState, identity } from '../src' | ||
import { renderHook, act } from '@testing-library/react-hooks' | ||
import { BehaviorSubject, of, Subject, throwError } from 'rxjs' | ||
import { map, scan } from 'rxjs/operators' | ||
|
||
describe('useLayoutObservableState', () => { | ||
describe('with init function', () => { | ||
it('should call the init function once', () => { | ||
const timeSpy = jest.fn(() => of(Date.now())) | ||
const { rerender } = renderHook(() => useLayoutObservableState(timeSpy)) | ||
expect(timeSpy).toBeCalledTimes(1) | ||
rerender() | ||
expect(timeSpy).toBeCalledTimes(1) | ||
rerender() | ||
expect(timeSpy).toBeCalledTimes(1) | ||
}) | ||
|
||
it('should start receiving values after first rendering', () => { | ||
const spy = jest.fn() | ||
const { result } = renderHook(() => { | ||
const state = useLayoutObservableState(() => of(1, 2, 3)) | ||
spy(state[0]) | ||
return state | ||
}) | ||
expect(result.current[0]).toBe(3) | ||
expect(spy).toHaveBeenCalledTimes(2) | ||
expect(spy).toHaveBeenNthCalledWith(1, undefined) | ||
expect(spy).toHaveBeenNthCalledWith(2, 3) | ||
}) | ||
|
||
it('should update value when the returned function is called', () => { | ||
const { result } = renderHook(() => | ||
useLayoutObservableState<string, number>(input$ => | ||
input$.pipe(map(input => 'test' + input)) | ||
) | ||
) | ||
const [state, updateState] = result.current | ||
expect(state).toBeUndefined() | ||
act(() => { | ||
updateState(1) | ||
}) | ||
expect(result.current[0]).toBe('test1') | ||
act(() => { | ||
updateState(2) | ||
}) | ||
expect(result.current[0]).toBe('test2') | ||
}) | ||
|
||
it('should get the init state if given', () => { | ||
const { result } = renderHook(() => useLayoutObservableState(identity, 1)) | ||
expect(result.current[0]).toBe(1) | ||
}) | ||
|
||
it('should get the init state of BehaviorSubject without initialState', () => { | ||
const value$ = new BehaviorSubject('22') | ||
const { result } = renderHook(() => useLayoutObservableState(value$)) | ||
expect(result.current).toBe('22') | ||
}) | ||
|
||
it('should ignore the manual initialState for BehaviorSubject', () => { | ||
const value$ = new BehaviorSubject('22') | ||
const { result } = renderHook(() => | ||
useLayoutObservableState(value$, 'initialState') | ||
) | ||
expect(result.current).toBe('22') | ||
}) | ||
|
||
it('should ignore the given init state when Observable also emits sync values', () => { | ||
const { result } = renderHook(() => | ||
useLayoutObservableState(() => of(1, 2), 3) | ||
) | ||
expect(result.current[0]).toBe(2) | ||
}) | ||
|
||
it('should throw error when observable emits error', () => { | ||
const { result } = renderHook(() => | ||
useLayoutObservableState(() => throwError(new Error('oops'))) | ||
) | ||
expect(result.error).toBeInstanceOf(Error) | ||
expect(result.error.message).toBe('oops') | ||
}) | ||
|
||
it('should support reducer pattern', () => { | ||
interface StoreState { | ||
value1: string | ||
value2: number | ||
} | ||
|
||
type StoreAction = | ||
| { | ||
type: 'UPDATE_VALUE1' | ||
payload: string | ||
} | ||
| { | ||
type: 'INCREMENT_VALUE2' | ||
} | ||
|
||
const { result } = renderHook(() => | ||
useLayoutObservableState<StoreState, StoreAction>( | ||
(action$, initialState) => | ||
action$.pipe( | ||
scan((state, action) => { | ||
switch (action.type) { | ||
case 'UPDATE_VALUE1': | ||
return { | ||
...state, | ||
value1: action.payload | ||
} | ||
case 'INCREMENT_VALUE2': | ||
return { | ||
...state, | ||
value2: state.value2 + 1 | ||
} | ||
default: | ||
return state | ||
} | ||
}, initialState) | ||
), | ||
() => ({ value1: 'value1', value2: 2 }) | ||
) | ||
) | ||
|
||
let [state, dispatch] = result.current | ||
expect(state).toEqual({ value1: 'value1', value2: 2 }) | ||
|
||
act(() => { | ||
dispatch({ type: 'UPDATE_VALUE1', payload: 'value2' }) | ||
}) | ||
|
||
state = result.current[0] | ||
expect(state).toEqual({ value1: 'value2', value2: 2 }) | ||
|
||
act(() => { | ||
dispatch({ type: 'INCREMENT_VALUE2' }) | ||
}) | ||
|
||
state = result.current[0] | ||
expect(state).toEqual({ value1: 'value2', value2: 3 }) | ||
}) | ||
}) | ||
|
||
describe('with init Observable', () => { | ||
it('should start receiving values after first rendering', () => { | ||
const spy = jest.fn() | ||
const outer$ = of(1, 2, 3) | ||
const { result } = renderHook(() => { | ||
const state = useLayoutObservableState(outer$) | ||
spy(state) | ||
return state | ||
}) | ||
expect(result.current).toBe(3) | ||
expect(spy).toHaveBeenCalledTimes(2) | ||
expect(spy).toHaveBeenNthCalledWith(1, undefined) | ||
expect(spy).toHaveBeenNthCalledWith(2, 3) | ||
}) | ||
|
||
it('should update value when the Observable emits value', () => { | ||
const outer$$ = new Subject<number>() | ||
const { result } = renderHook(() => useLayoutObservableState(outer$$)) | ||
expect(result.current).toBeUndefined() | ||
act(() => { | ||
outer$$.next(1) | ||
}) | ||
expect(result.current).toBe(1) | ||
act(() => { | ||
outer$$.next(2) | ||
}) | ||
expect(result.current).toBe(2) | ||
}) | ||
|
||
it('should get the init state if given', () => { | ||
const outer$$ = new Subject() | ||
const { result } = renderHook(() => useLayoutObservableState(outer$$, 1)) | ||
expect(result.current).toBe(1) | ||
}) | ||
|
||
it('should ignore the given init state when Observable also emits sync values', () => { | ||
const outer$ = of(1, 2) | ||
const { result } = renderHook(() => useLayoutObservableState(outer$, 3)) | ||
expect(result.current).toBe(2) | ||
}) | ||
|
||
it('should throw error when observable emits error', () => { | ||
const outer$ = throwError(new Error('oops')) | ||
const { result } = renderHook(() => useLayoutObservableState(outer$, 3)) | ||
expect(result.error).toBeInstanceOf(Error) | ||
expect(result.error.message).toBe('oops') | ||
}) | ||
}) | ||
}) |
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
41 changes: 41 additions & 0 deletions
41
packages/observable-hooks/src/internal/use-observable-state-internal.ts
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,41 @@ | ||
import { Observable, isObservable, Subject } from 'rxjs' | ||
import { useState, useRef, useDebugValue } from 'react' | ||
import type { useSubscription as useSubscriptionType } from '../use-subscription' | ||
import { useRefFn, getEmptySubject } from '../helpers' | ||
|
||
export function useObservableStateInternal<TState, TInput = TState>( | ||
useSubscription: typeof useSubscriptionType, | ||
state$OrInit: | ||
| Observable<TState> | ||
| (( | ||
input$: Observable<TInput>, | ||
initialState?: TState | ||
) => Observable<TState>), | ||
initialState?: TState | (() => TState) | ||
): TState | undefined | [TState | undefined, (input: TInput) => void] { | ||
const [state, setState] = useState<TState | undefined>(initialState) | ||
|
||
let callback: undefined | ((input: TInput) => void) | ||
let state$: Observable<TState> | ||
|
||
if (isObservable(state$OrInit)) { | ||
state$ = state$OrInit | ||
} else { | ||
const init = state$OrInit | ||
// Even though hooks are under conditional block | ||
// it is for a completely different use case | ||
// which unlikely coexists with the other one. | ||
// A warning is also added to the docs. | ||
const input$Ref = useRefFn<Subject<TInput>>(getEmptySubject) | ||
|
||
state$ = useRefFn(() => init(input$Ref.current, state)).current | ||
callback = useRef((state: TInput) => input$Ref.current.next(state)).current | ||
} | ||
|
||
useSubscription(state$, setState) | ||
|
||
// Display state in React DevTools. | ||
useDebugValue(state) | ||
|
||
return callback ? [state, callback] : state | ||
} |
Oops, something went wrong.