-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
92 additions
and
12 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
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 |
---|---|---|
@@ -1,22 +1,46 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { Observable } from 'rxjs'; | ||
|
||
/** | ||
* Returns a value provided by `source$`. | ||
* | ||
* The hook returns the initial value and subscribes on the `source$`. After | ||
* that, the hook returns values which are provided by the source. | ||
* | ||
* @param source$ an observable for values | ||
* @param initialValue th first value which is returned by the hook | ||
* @param selector a transform function for getting a derived value based on | ||
* the source value | ||
* @param comparator a comparator for previous and next values | ||
* | ||
* @example | ||
* ```ts | ||
* const value = useSelector<{data: Record<string, string>}>( | ||
* source$, | ||
* undefined, | ||
* (state) => state.data, | ||
* (data1, data2) => data1.key === data2.key | ||
* ); | ||
* ``` | ||
*/ | ||
export function useSelector<S, R>( | ||
state$: Observable<S>, | ||
initialState: S, | ||
source$: Observable<S>, | ||
initialValue: S, | ||
selector: (state: S) => R, | ||
compare: (v1: R, v2: R) => boolean = Object.is, | ||
comparator: (v1: R, v2: R) => boolean = Object.is, | ||
): R { | ||
const [value, setValue] = useState<R>(() => selector(initialState)); | ||
const [value, setValue] = useState<R>(() => selector(initialValue)); | ||
|
||
useEffect(() => { | ||
const subscription = state$.subscribe((state) => { | ||
const subscription = source$.subscribe((state) => { | ||
const value = selector(state); | ||
setValue((prevValue) => (compare(value, prevValue) ? prevValue : value)); | ||
setValue((prevValue) => | ||
comparator(value, prevValue) ? prevValue : value, | ||
); | ||
}); | ||
|
||
return () => subscription.unsubscribe(); | ||
}, [compare, selector, state$]); | ||
}, [comparator, selector, source$]); | ||
|
||
return value; | ||
} |
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