This repository has been archived by the owner on Jul 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 新增 useEffectNovel; useNovel -> useMemoNovel
- Loading branch information
Showing
6 changed files
with
120 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { useEffect, useMemo, useRef, useState } from 'react' | ||
import { isObservable, Subject, Subscription } from 'rxjs' | ||
import { Novel } from './interfaces' | ||
import StateObservable from './StateObservable' | ||
|
||
export function useEffectNovel<I, S extends object, D extends object, E>( | ||
input: I, | ||
initialState: S, | ||
novel: Novel<I, S, D, E>, | ||
) { | ||
const [state, setState] = useState(initialState) | ||
const input$ = useMemo(() => new Subject<I>(), []) | ||
const state$ = useMemo(() => new Subject<S>(), []) | ||
|
||
// 每次渲染之后更新 input$ | ||
const mount = useRef(true) | ||
useEffect(() => { | ||
// 跳过第一次渲染 | ||
if (mount.current) { | ||
mount.current = false | ||
return | ||
} | ||
input$.next(input) | ||
}) | ||
|
||
const derivedValueRef = useRef<D>(null) | ||
const exportsRef = useRef<E>(null) | ||
const subscription = useMemo(() => new Subscription(), []) | ||
|
||
// 用 useEffect 来执行 novel 与订阅 derived$,注意这一个操作是「异步」的 | ||
// derived/nextState 的初始值会被丢弃 | ||
useEffect(() => { | ||
const output = novel( | ||
new StateObservable(input$, input), | ||
new StateObservable(state$, initialState), | ||
) | ||
if (output == null) { | ||
return | ||
} | ||
|
||
if (isObservable(output)) { | ||
subscription.add( | ||
output.subscribe(value => { | ||
state$.next(value) | ||
setState(value) | ||
}), | ||
) | ||
} else { | ||
subscription.add( | ||
output.nextState?.subscribe(value => { | ||
state$.next(value) | ||
setState(value) | ||
}), | ||
) | ||
if (output.derived) { | ||
subscription.add( | ||
output.derived.subscribe(value => { | ||
derivedValueRef.current = value | ||
}), | ||
) | ||
} | ||
subscription.add(output.teardown) | ||
exportsRef.current = output.exports | ||
} | ||
}, []) | ||
|
||
useEffect(() => { | ||
return () => { | ||
subscription.unsubscribe() | ||
} | ||
}, []) | ||
|
||
return [{ ...state, ...derivedValueRef.current }, exportsRef.current] as const | ||
} |
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,5 +1,13 @@ | ||
export * from './equal-fns' | ||
export * from './helpers' | ||
export * from './novel' | ||
export * from './operators' | ||
export { deepEqual, shallowEqual } from './equal-fns' | ||
export { SubjectProxy } from './helpers' | ||
export { combineLatestFromObject, applyMutatorAsReducer, log, ofType } from './operators' | ||
export { default as StateObservable } from './StateObservable' | ||
export { Novel} from './interfaces' | ||
export { useMemoNovel } from './memo-novel' | ||
export { useEffectNovel } from './effect-novel' | ||
export { NO_VALUE } from './memo-novel' | ||
|
||
import { useMemoNovel } from './memo-novel' | ||
|
||
/** @deprecated Use {useMemoNovel} instead */ | ||
export const useNovel = useMemoNovel |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Observable } from 'rxjs' | ||
import StateObservable from './StateObservable' | ||
|
||
export type Novel<I, S extends object, D extends object, E> = ( | ||
input$: StateObservable<I>, | ||
state$: StateObservable<S>, | ||
) => | ||
| Observable<S> | ||
| { | ||
nextState?: Observable<S> | ||
derived?: Observable<D> | ||
exports?: E | ||
teardown?(): void | ||
} |
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