Skip to content

Commit

Permalink
feat(observable-hooks): better type infer for BehaviorSubject
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Oct 15, 2021
1 parent 86026ca commit 25f8eec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
16 changes: 15 additions & 1 deletion packages/observable-hooks/__tests__/use-observable-state.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useObservableState, identity } from '../src'
import { renderHook, act } from '@testing-library/react-hooks'
import { of, Subject, throwError } from 'rxjs'
import { BehaviorSubject, of, Subject, throwError } from 'rxjs'
import { map, scan } from 'rxjs/operators'

describe('useObservableState', () => {
Expand Down Expand Up @@ -51,6 +51,20 @@ describe('useObservableState', () => {
expect(result.current[0]).toBe(1)
})

it('should get the init state of BehaviorSubject without initialState', () => {
const value$ = new BehaviorSubject('22')
const { result } = renderHook(() => useObservableState(value$))
expect(result.current).toBe('22')
})

it('should ignore the manual initialState for BehaviorSubject', () => {
const value$ = new BehaviorSubject('22')
const { result } = renderHook(() =>
useObservableState(value$, 'initialState')
)
expect(result.current).toBe('22')
})

it('should ignore the given init state when Observable also emits sync values', () => {
const { result } = renderHook(() => useObservableState(() => of(1, 2), 3))
expect(result.current[0]).toBe(2)
Expand Down
21 changes: 7 additions & 14 deletions packages/observable-hooks/src/use-observable-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,19 @@ import { useRefFn, getEmptySubject } from './helpers'
*
* @template TState Output state.
*
* @param input$ An Observable.
* @param input$ A BehaviorSubject.
*/
export function useObservableState<TState>(
input$: Observable<TState>
): TState | undefined
input$: BehaviorSubject<TState>
): TState
/**
* @template TState Output state.
*
* @param input$ A BehaviorSubject.
* @param initialState Optional initial state (defaults to the BehaviorSubject's value).
* Can be the state value or a function that returns the state.
* @param input$ An Observable.
*/
export function useObservableState<TState>(
input$: BehaviorSubject<TState>,
initialState?: TState | (() => TState)
): TState
input$: Observable<TState>
): TState | undefined
/**
* @template TState Output state.
*
Expand Down Expand Up @@ -105,11 +102,7 @@ export function useObservableState<TState, TInput = TState>(
) => Observable<TState>),
initialState?: TState | (() => TState)
): TState | undefined | [TState | undefined, (input: TInput) => void] {
const [state, setState] = useState<TState | undefined>(
state$OrInit instanceof BehaviorSubject
? state$OrInit.value
: initialState
)
const [state, setState] = useState<TState | undefined>(initialState)

let callback: undefined | ((input: TInput) => void)
let state$: Observable<TState>
Expand Down

0 comments on commit 25f8eec

Please sign in to comment.