-
Notifications
You must be signed in to change notification settings - Fork 85
refactor: add state$ in hooks #10
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,34 +1,76 @@ | ||
import { useEffect, useState, SyntheticEvent } from 'react' | ||
import { Observable, Subject, noop } from 'rxjs' | ||
import { useEffect, useMemo, useState, SyntheticEvent } from 'react' | ||
import { Observable, BehaviorSubject, Subject, noop } from 'rxjs' | ||
|
||
export type EventCallbackState<T, U> = [((e: SyntheticEvent<T>) => void) | typeof noop, U] | ||
export type VoidAsNull<T> = T extends void ? null : T | ||
|
||
export type EventCallback<T, U> = (eventSource$: Observable<SyntheticEvent<T>>) => Observable<U> | ||
export type EventCallbackState<_T, E, U, I = void> = [ | ||
(e: E) => void, | ||
[U extends void ? null : U, BehaviorSubject<U | null>, BehaviorSubject<I | null>] | ||
] | ||
export type ReturnedState<T, E, U, I> = [EventCallbackState<T, E, U, I>[0], EventCallbackState<T, E, U, I>[1][0]] | ||
|
||
export function useEventCallback<T, U = void>(callback: EventCallback<T, U>): EventCallbackState<T, U | null> | ||
export function useEventCallback<T, U = void>(callback: EventCallback<T, U>, initialState: U): EventCallbackState<T, U> | ||
export type EventCallback<_T, E, U, I> = I extends void | ||
? (eventSource$: Observable<E>, state$: Observable<U>) => Observable<U> | ||
: (eventSource$: Observable<E>, inputs$: Observable<I>, state$: Observable<U>) => Observable<U> | ||
|
||
export function useEventCallback<T, U = void>( | ||
callback: EventCallback<T, U>, | ||
export function useEventCallback<T, E extends SyntheticEvent<T>, U = void>( | ||
callback: EventCallback<T, E, U, void>, | ||
): ReturnedState<T, E, U | null, void> | ||
export function useEventCallback<T, E extends SyntheticEvent<T>, U = void>( | ||
callback: EventCallback<T, E, U, void>, | ||
initialState: U, | ||
): ReturnedState<T, E, U, void> | ||
export function useEventCallback<T, E extends SyntheticEvent<T>, U = void, I = void>( | ||
callback: EventCallback<T, E, U, I>, | ||
initialState: U, | ||
inputs: I, | ||
): ReturnedState<T, E, U, I> | ||
|
||
export function useEventCallback<T, E extends SyntheticEvent<T>, U = void, I = void>( | ||
callback: EventCallback<T, E, U, I>, | ||
initialState?: U, | ||
): EventCallbackState<T, U | null> { | ||
const initialValue = typeof initialState !== 'undefined' ? initialState : null | ||
const [state, setState] = useState<EventCallbackState<T, U | null>>([noop, initialValue]) | ||
inputs?: I, | ||
): ReturnedState<T, E, U | null, I> { | ||
const initialValue = (typeof initialState !== 'undefined' ? initialState : null) as VoidAsNull<U> | ||
const inputSubject$ = new BehaviorSubject<I | null>(typeof inputs === 'undefined' ? null : inputs) | ||
const stateSubject$ = new BehaviorSubject<U | null>(initialValue) | ||
const [state, setState] = useState(initialValue) | ||
const [returnedCallback, setEventCallback] = useState<(e: E) => void>(noop) | ||
const [state$] = useState(stateSubject$) | ||
const [inputs$] = useState(inputSubject$) | ||
|
||
useMemo(() => { | ||
inputs$.next(inputs!) | ||
}, ((inputs as unknown) as ReadonlyArray<any>) || []) | ||
|
||
useEffect( | ||
() => { | ||
const event$ = new Subject<SyntheticEvent<T>>() | ||
function eventCallback(e: SyntheticEvent<T>) { | ||
const event$ = new Subject<E>() | ||
function eventCallback(e: E) { | ||
return event$.next(e) | ||
} | ||
setState([eventCallback, initialValue]) | ||
const value$ = callback(event$) | ||
setState(initialValue) | ||
setEventCallback(() => eventCallback) | ||
let value$: Observable<U> | ||
|
||
if (!inputs) { | ||
value$ = (callback as EventCallback<T, E, U, void>)(event$, state$ as Observable<U>) | ||
} else { | ||
value$ = (callback as any)(event$, inputs$ as Observable<any>, state$ as Observable<U>) | ||
} | ||
const subscription = value$.subscribe((value) => { | ||
setState([eventCallback, value]) | ||
state$.next(value) | ||
setState(value as VoidAsNull<U>) | ||
}) | ||
return () => subscription.unsubscribe() | ||
return () => { | ||
subscription.unsubscribe() | ||
state$.complete() | ||
inputs$.complete() | ||
event$.complete() | ||
} | ||
}, | ||
[0], // immutable forever | ||
[], // immutable forever | ||
) | ||
|
||
return state | ||
return [returnedCallback, state] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.