Skip to content

fix(hooks): useAyanami type define #459

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 1 commit into from
Mar 4, 2021
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
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