1
- import { AsyncLocalStorage } from ' node:async_hooks'
2
- import * as crypto from ' node:crypto'
3
- import type { Optional } from ' utility-types'
1
+ import { AsyncLocalStorage } from " node:async_hooks" ;
2
+ import * as crypto from " node:crypto" ;
3
+ import type { Optional } from " utility-types" ;
4
4
5
- const asyncLocalStorage = new AsyncLocalStorage ( )
5
+ const asyncLocalStorage = new AsyncLocalStorage ( ) ;
6
6
7
- const globalStore = { }
7
+ const globalStore = { } ;
8
8
9
- interface CreateHookOptions < State extends Record < string , any > , ExecuteResult , HookOptions > {
10
- name : string
11
- data : ( ) => State
12
- execute : ( state : State , options : HookOptions ) => ExecuteResult
9
+ interface CreateHookOptions <
10
+ State extends Record < string , any > ,
11
+ ExecuteResult ,
12
+ HookOptions
13
+ > {
14
+ name : string ;
15
+ data : ( ) => State ;
16
+ execute : ( state : State , options : HookOptions ) => ExecuteResult ;
13
17
}
14
18
15
19
const getCurrentStore = ( ) => {
16
- return asyncLocalStorage . getStore ( ) || globalStore
17
- }
20
+ return asyncLocalStorage . getStore ( ) || globalStore ;
21
+ } ;
18
22
19
- const generateHookFunction = < State , ExecuteResult , HookOptions > ( options : CreateHookOptions < State , ExecuteResult , HookOptions > ) => {
23
+ const generateHookFunction = < State , ExecuteResult , HookOptions > (
24
+ options : CreateHookOptions < State , ExecuteResult , HookOptions >
25
+ ) => {
20
26
return ( parameters ?: HookOptions ) => {
21
- const store = getCurrentStore ( )
27
+ const store = getCurrentStore ( ) ;
22
28
if ( ! store [ options . name ] ) {
23
- store [ options . name ] = options . data ( )
29
+ store [ options . name ] = options . data ( ) ;
24
30
}
25
- const hookStore = store [ options . name ] as State
26
- return options . execute ( hookStore , parameters )
27
- }
28
- }
31
+ const hookStore = store [ options . name ] as State ;
32
+ return options . execute ( hookStore , parameters ) ;
33
+ } ;
34
+ } ;
29
35
30
- const generateUpdateHookStateFunction = < State , ExecuteResult , HookOptions > ( options : CreateHookOptions < State , ExecuteResult , HookOptions > ) => {
36
+ const generateUpdateHookStateFunction = < State , ExecuteResult , HookOptions > (
37
+ options : CreateHookOptions < State , ExecuteResult , HookOptions >
38
+ ) => {
31
39
return ( fn : ( currentState : State ) => State ) => {
32
- const store = getCurrentStore ( )
40
+ const store = getCurrentStore ( ) ;
33
41
if ( ! store [ options . name ] ) {
34
- store [ options . name ] = options . data ( )
42
+ store [ options . name ] = options . data ( ) ;
35
43
}
36
- const hookStore = store [ options . name ] as State
37
- store [ options . name ] = fn ( hookStore )
38
- }
39
- }
44
+ const hookStore = store [ options . name ] as State ;
45
+ store [ options . name ] = fn ( hookStore ) ;
46
+ } ;
47
+ } ;
40
48
41
- export const createHook = < State , ExecuteResult , HookOptions > ( options : Optional < Omit < CreateHookOptions < State , ExecuteResult , HookOptions > , 'name' > , 'data' > ) : [ ( parameters ?: HookOptions ) => ExecuteResult , ( fn : ( currentState : State ) => State ) => void ] => {
42
- const name = crypto . randomUUID ( )
43
- const data = options . data || ( ( ) => ( { } as State ) )
49
+ export const createHook = < State , ExecuteResult , HookOptions > (
50
+ options : Optional <
51
+ Omit < CreateHookOptions < State , ExecuteResult , HookOptions > , "name" > ,
52
+ "data"
53
+ >
54
+ ) : [
55
+ ( parameters ?: HookOptions ) => ExecuteResult ,
56
+ ( fn : ( currentState : State ) => State ) => void
57
+ ] => {
58
+ const name = crypto . randomUUID ( ) ;
59
+ const data = options . data || ( ( ) => ( { } as State ) ) ;
44
60
return [
45
61
generateHookFunction ( {
46
62
...options ,
47
63
name,
48
- data
64
+ data,
49
65
} ) ,
50
66
generateUpdateHookStateFunction ( {
51
67
...options ,
52
68
name,
53
- data
54
- } )
55
- ]
56
- }
69
+ data,
70
+ } ) ,
71
+ ] ;
72
+ } ;
57
73
58
- export const runHookContext = async < T > ( fn : ( ) => Promise < T > | T ) : Promise < T > => {
59
- const result = await asyncLocalStorage . run ( { } , fn ) as T
60
- return result
61
- }
74
+ export const runHookContext = async < T > (
75
+ fn : ( ) => Promise < T > | T
76
+ ) : Promise < T > => {
77
+ const result = ( await asyncLocalStorage . run ( { } , fn ) ) as T ;
78
+ return result ;
79
+ } ;
0 commit comments