Skip to content

Commit

Permalink
feat: 🎸 improve useObservable() type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed May 30, 2019
1 parent 70697d9 commit d0c3713
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/useObservable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { useEffect, useState } from 'react';

const useObservable = <T>(observable$, initialValue?: T): T | undefined => {
export interface Observable<T> {
subscribe: (
listener: (value: T) => void
) => {
unsubscribe: () => void;
};
}

function useObservable<T>(observable$: Observable<T>): T | undefined;
function useObservable<T>(observable$: Observable<T>, initialValue: T): T;
function useObservable<T>(observable$: Observable<T>, initialValue?: T): T | undefined {
const [value, update] = useState<T | undefined>(initialValue);

useEffect(() => {
Expand All @@ -9,6 +19,6 @@ const useObservable = <T>(observable$, initialValue?: T): T | undefined => {
}, [observable$]);

return value;
};
}

export default useObservable;

0 comments on commit d0c3713

Please sign in to comment.