Skip to content

Commit

Permalink
Merge pull request #459 from InfiniteXyy/master
Browse files Browse the repository at this point in the history
fix(hooks): useAyanami type define
  • Loading branch information
Miloas authored Mar 4, 2021
2 parents b2402ff + 21330f8 commit a1cb54a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/hooks/use-ayanami-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import get from 'lodash/get'
import { ActionMethodOfAyanami, Ayanami, combineWithIkari } from '../core'
import { useSubscribeAyanamiState } from './use-subscribe-ayanami-state'

export interface UseAyanamiInstanceConfig<S, U> {
export interface UseAyanamiInstanceConfig<S = unknown, U = unknown> {
destroyWhenUnmount?: boolean
selector?: (state: S) => U
}
Expand Down
14 changes: 9 additions & 5 deletions src/hooks/use-ayanami.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ interface Config<S, U> extends Partial<ScopeConfig> {
selector?: (state: S) => U
}

export function useAyanami<M extends Ayanami<S>, S, U = M extends Ayanami<infer SS> ? SS : S>(
export function useAyanami<M extends Ayanami<any>, U = M extends Ayanami<infer S> ? S : never>(
A: ConstructorOf<M>,
config?: M extends Ayanami<infer SS> ? Config<SS, U> : Config<S, U>,
): M extends Ayanami<infer SS> ? Result<M, SS, U> : Result<M, S, U> {
config?: M extends Ayanami<infer S> ? Config<S, U> : never,
): M extends Ayanami<infer S>
? NonNullable<typeof config> extends Config<S, infer SS>
? Result<M, S, SS>
: Result<M, S, S>
: never {
const scope = get(config, 'scope')
const selector = get(config, 'selector')
const req = isSSREnabled() ? React.useContext(SSRContext) : null
const reqScope = req ? createScopeWithRequest(req, scope) : scope
const ayanami = React.useMemo(() => getInstanceWithScope(A, reqScope), [reqScope])
ayanami.scopeName = scope || DEFAULT_SCOPE_NAME

const useAyanamiInstanceConfig = React.useMemo((): UseAyanamiInstanceConfig<S, U> => {
const useAyanamiInstanceConfig = React.useMemo<UseAyanamiInstanceConfig>(() => {
return { destroyWhenUnmount: scope === TransientScope, selector }
}, [reqScope])

return useAyanamiInstance<M, S, U>(ayanami, useAyanamiInstanceConfig) as any
return useAyanamiInstance(ayanami, useAyanamiInstanceConfig) as any
}
4 changes: 2 additions & 2 deletions test/specs/hooks.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ describe('Hooks spec:', () => {
})

const OuterComponent = () => {
const [state, actions] = useAyanami(Count, { scope: TransientScope })
const [{ count }, actions] = useAyanami(Count, { scope: TransientScope })
const addOne = useCallback(() => actions.add(1), [])
outerRenderSpy(state.count)
outerRenderSpy(count)
return (
<div>
<button onClick={addOne}>add one</button>
Expand Down

0 comments on commit a1cb54a

Please sign in to comment.