Skip to content

Commit c95f434

Browse files
committed
feat: refactoring hooks
- split functions into separate file - rename type `HooksResult` to `UseAyanamiInstanceResult`
1 parent f8b1c53 commit c95f434

File tree

4 files changed

+75
-64
lines changed

4 files changed

+75
-64
lines changed

src/hooks.ts

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/hooks/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './use-ayanami-instance'
2+
export * from './use-ayanami'

src/hooks/use-ayanami-instance.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as React from 'react'
2+
import { get } from 'lodash'
3+
4+
import { ActionMethodOfAyanami, Ayanami, combineWithIkari } from '../core'
5+
6+
export interface UseAyanamiInstanceConfig {
7+
destroyWhenUnmount?: boolean
8+
}
9+
10+
export type UseAyanamiInstanceResult<M extends Ayanami<S>, S> = [
11+
Readonly<S>,
12+
ActionMethodOfAyanami<M, S>
13+
]
14+
15+
type Config = UseAyanamiInstanceConfig
16+
17+
type Result<M extends Ayanami<S>, S> = UseAyanamiInstanceResult<M, S>
18+
19+
export function useAyanamiInstance<M extends Ayanami<S>, S>(
20+
ayanami: M,
21+
config?: Config,
22+
): Result<M, S> {
23+
const ayanamiRef = React.useRef(ayanami)
24+
const ikari = React.useMemo(() => combineWithIkari(ayanami), [ayanami])
25+
const [state, setState] = React.useState<S>(() => ayanami.getState())
26+
27+
if (ayanamiRef.current !== ayanami) {
28+
ayanamiRef.current = ayanami
29+
setState(ayanami.getState())
30+
}
31+
32+
React.useEffect(() => {
33+
const subscription = ayanami.getState$().subscribe(setState)
34+
return () => subscription.unsubscribe()
35+
}, [ayanami])
36+
37+
React.useEffect(
38+
() => () => {
39+
const isDestroyWhenUnmount = get(config, 'destroyWhenUnmount', false)
40+
41+
if (isDestroyWhenUnmount) {
42+
ayanami.destroy()
43+
}
44+
},
45+
[ayanami, config],
46+
)
47+
48+
return [state, ikari.triggerActions] as Result<M, S>
49+
}

src/hooks/use-ayanami.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as React from 'react'
2+
import { get } from 'lodash'
3+
4+
import { Ayanami, ConstructorOf, getInstanceWithScope, ScopeConfig, TransientScope } from '../core'
5+
6+
import {
7+
useAyanamiInstance,
8+
UseAyanamiInstanceResult,
9+
UseAyanamiInstanceConfig,
10+
} from './use-ayanami-instance'
11+
12+
export function useAyanami<M extends Ayanami<S>, S>(
13+
A: ConstructorOf<M>,
14+
config?: ScopeConfig,
15+
): M extends Ayanami<infer SS> ? UseAyanamiInstanceResult<M, SS> : UseAyanamiInstanceResult<M, S> {
16+
const scope = get(config, 'scope')
17+
const ayanami = React.useMemo(() => getInstanceWithScope(A, scope), [scope])
18+
19+
const useAyanamiInstanceConfig = React.useMemo((): UseAyanamiInstanceConfig => {
20+
return { destroyWhenUnmount: scope === TransientScope }
21+
}, [scope])
22+
23+
return useAyanamiInstance<M, S>(ayanami, useAyanamiInstanceConfig) as any
24+
}

0 commit comments

Comments
 (0)