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" ;
44
5- const asyncLocalStorage = new AsyncLocalStorage ( )
5+ const asyncLocalStorage = new AsyncLocalStorage ( ) ;
66
7- const globalStore = { }
7+ const globalStore = { } ;
88
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 ;
1317}
1418
1519const getCurrentStore = ( ) => {
16- return asyncLocalStorage . getStore ( ) || globalStore
17- }
20+ return asyncLocalStorage . getStore ( ) || globalStore ;
21+ } ;
1822
19- const generateHookFunction = < State , ExecuteResult , HookOptions > ( options : CreateHookOptions < State , ExecuteResult , HookOptions > ) => {
23+ const generateHookFunction = < State , ExecuteResult , HookOptions > (
24+ options : CreateHookOptions < State , ExecuteResult , HookOptions >
25+ ) => {
2026 return ( parameters ?: HookOptions ) => {
21- const store = getCurrentStore ( )
27+ const store = getCurrentStore ( ) ;
2228 if ( ! store [ options . name ] ) {
23- store [ options . name ] = options . data ( )
29+ store [ options . name ] = options . data ( ) ;
2430 }
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+ } ;
2935
30- const generateUpdateHookStateFunction = < State , ExecuteResult , HookOptions > ( options : CreateHookOptions < State , ExecuteResult , HookOptions > ) => {
36+ const generateUpdateHookStateFunction = < State , ExecuteResult , HookOptions > (
37+ options : CreateHookOptions < State , ExecuteResult , HookOptions >
38+ ) => {
3139 return ( fn : ( currentState : State ) => State ) => {
32- const store = getCurrentStore ( )
40+ const store = getCurrentStore ( ) ;
3341 if ( ! store [ options . name ] ) {
34- store [ options . name ] = options . data ( )
42+ store [ options . name ] = options . data ( ) ;
3543 }
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+ } ;
4048
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 ) ) ;
4460 return [
4561 generateHookFunction ( {
4662 ...options ,
4763 name,
48- data
64+ data,
4965 } ) ,
5066 generateUpdateHookStateFunction ( {
5167 ...options ,
5268 name,
53- data
54- } )
55- ]
56- }
69+ data,
70+ } ) ,
71+ ] ;
72+ } ;
5773
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