Skip to content

Fix typings #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,25 @@ interface IProps {
}

export const Counter: React.FC<IProps> = ({ start }) => {

const [ value ] = useObservable(() => {
return rxjs.interval(1000)
.pipe(
map(v => v + start),
startWith(start),
);
}, [start])
return rxjs.interval(1000).pipe(
map(v => v + start),
startWith(start)
);
}, [/* deps */])

return (
<p>Started with {start}, value: {value}</p>
<p>Started with undefined, value: {value}</p>
);
}
```
```

## API

| Hook | Return | Description |
| --------------------------------------------- | ------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| useObservable(Func, DepsArray) | Value, Error, Completed | Run the observable function on every deps change, the first value will be undefined |
| useRetryableObservable(Func, DepsArray) | Value, Error, Completed, RetryFunc | Same useObservable, but the last value of return is a retry function to rerun the observable function, useful when an error happens or do a refresh |
| useMappedObservable(Func, MapFunc, DepsArray) | Value, Error, Completed | Same useObservable, but with a internal map and a distinctUntilChanged, useful to do less renders |
| useCallbackObservable(Func, DepsArray) | CallbackFunc, Value, Error, Completed | Same useObservable, but the first result is the callbackFunction and run the observable function only when it is called, useful to use in a submit or button's click and keep the unsubscribe on unmount |

4 changes: 2 additions & 2 deletions src/callbackObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useObservable } from './observable';
export function useCallbackObservable<T extends (...args: any[]) => Observable<any>>(
observableGenerator: T,
deps: DependencyList
): [() => void, T | undefined, any, boolean] {
): [() => void, T | undefined, any, boolean, undefined] {
const [error, setError] = useState();
const submitted$ = useRef(new Subject<any>()).current;

Expand All @@ -31,5 +31,5 @@ export function useCallbackObservable<T extends (...args: any[]) => Observable<a

const callback = useCallback((...args: any[]) => submitted$.next(args), [submitted$]);

return [callback, data, error, completed] as [typeof callback, T | undefined, any, boolean];
return [callback, data, error, completed, undefined];
}
2 changes: 1 addition & 1 deletion src/mappedObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function useMappedObservable<T, W>(
observableGenerator: observerFunction<T>,
mapperFunction: (data: T) => W,
deps: DependencyList
): [W | undefined, any, boolean] {
): [W | undefined, any, boolean, undefined] {
const newGenerator = useCallback(() => {
return observableGenerator().pipe(
map(mapperFunction),
Expand Down
4 changes: 2 additions & 2 deletions src/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type observerFunction<T> = () => Observable<T>;
export function useObservable<T>(
observableGenerator: observerFunction<T>,
deps: DependencyList
): [T | undefined, any, boolean] {
): [T | undefined, any, boolean, undefined] {
const [value, setValue] = useState<T>();
const [error, setError] = useState();
const [complete, setComplete] = useState<boolean>(false);
Expand All @@ -36,5 +36,5 @@ export function useObservable<T>(
return () => sub.unsubscribe();
}, [cb]);

return [value, error, complete];
return [value, error, complete, undefined];
}
6 changes: 3 additions & 3 deletions src/retryableObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { observerFunction, useObservable } from './observable';
* retry function
* @returns [observableValue, error, isCompleted, retryFunction]
*/
export function useRetryableObservable<T, W>(
export function useRetryableObservable<T>(
observableGenerator: observerFunction<T>,
deps: DependencyList
): [W | undefined, any, boolean, () => void] {
): [T | undefined, any, boolean, () => void, undefined] {
const [data, setData] = useState();
const [error, setError] = useState();
const submitted$ = useRef(new BehaviorSubject<boolean>(true)).current;
Expand All @@ -35,5 +35,5 @@ export function useRetryableObservable<T, W>(

const retry = useCallback(() => submitted$.next(true), [submitted$]);

return [data, error, completed, retry] as [W | undefined, any, boolean, typeof retry];
return [data, error, completed, retry, undefined];
}