@@ -39,29 +39,71 @@ function getStorage<TContext>(): ContextStorage<TContext> {
3939 return storage ! ;
4040}
4141
42- export function getContext < TContext = DefaultContext > ( ) : TContext | undefined {
43- return getStorage < TContext > ( ) . getStore ( ) ;
42+ export function getContext < TContext = DefaultContext > ( key ?: any ) : TContext | undefined {
43+ const store = getStorage < any > ( ) . getStore ( ) ;
44+ if ( key !== undefined ) {
45+ if ( store instanceof Map ) {
46+ return store . get ( key ) ;
47+ }
48+ if ( typeof store === 'object' && store !== null ) {
49+ return ( store as any ) [ key ] ;
50+ }
51+ return undefined ;
52+ }
53+ return store as TContext | undefined ;
4454}
4555
46- export function requireContext < TContext = DefaultContext > ( ) : TContext {
47- const ctx = getContext < TContext > ( ) ;
56+ export function requireContext < TContext = DefaultContext > ( key ?: any ) : TContext {
57+ const ctx = getContext < TContext > ( key ) ;
4858 if ( ctx === undefined ) {
49- throw new Error ( 'Runtime context is missing. Ensure you are running within runWithContext.' ) ;
59+ throw new Error (
60+ `Runtime context${ key ? ` for ${ String ( key ) } ` : '' } is missing. Ensure you are running within runWithContext.`
61+ ) ;
5062 }
5163 return ctx ;
5264}
5365
5466export function runWithContext < TContext , R > (
5567 ctx : TContext ,
5668 fn : ( ) => R
57- ) : R {
58- return getStorage < TContext > ( ) . run ( ctx , fn ) ;
69+ ) : R ;
70+ export function runWithContext < TContext , R > (
71+ key : any ,
72+ ctx : TContext ,
73+ fn : ( ) => R
74+ ) : R ;
75+ export function runWithContext < TContext , R > ( ...args : any [ ] ) : R {
76+ const storage = getStorage ( ) ;
77+ if ( args . length === 3 ) {
78+ const [ key , ctx , fn ] = args ;
79+ const parent = storage . getStore ( ) ;
80+ const map = parent instanceof Map ? new Map ( parent ) : new Map ( ) ;
81+ map . set ( key , ctx ) ;
82+ return storage . run ( map , fn ) ;
83+ }
84+ const [ ctx , fn ] = args ;
85+ return storage . run ( ctx , fn ) ;
5986}
6087
6188export function ensureContext < TContext , R > (
6289 create : ( ) => TContext ,
6390 fn : ( ) => Promise < R > | R
64- ) : Promise < R > | R {
91+ ) : Promise < R > | R ;
92+ export function ensureContext < TContext , R > (
93+ key : any ,
94+ create : ( ) => TContext ,
95+ fn : ( ) => Promise < R > | R
96+ ) : Promise < R > | R ;
97+ export function ensureContext < TContext , R > ( ...args : any [ ] ) : Promise < R > | R {
98+ if ( args . length === 3 ) {
99+ const [ key , create , fn ] = args ;
100+ const existing = getContext < TContext > ( key ) ;
101+ if ( existing !== undefined ) {
102+ return fn ( ) ;
103+ }
104+ return runWithContext ( key , create ( ) , fn ) ;
105+ }
106+ const [ create , fn ] = args ;
65107 const existing = getContext < TContext > ( ) ;
66108 if ( existing !== undefined ) {
67109 return fn ( ) ;
@@ -73,30 +115,48 @@ export function ensureContext<TContext, R>(
73115
74116export function setValue < TContext extends object = DefaultContext , K extends keyof TContext = keyof TContext > (
75117 key : K ,
76- value : TContext [ K ]
118+ value : TContext [ K ] ,
119+ contextKey ?: any
77120) : void {
78- const ctx = requireContext < TContext > ( ) ;
121+ const ctx = requireContext < any > ( contextKey ) ;
122+ if ( ctx instanceof Map ) {
123+ ctx . set ( key , value ) ;
124+ return ;
125+ }
79126 if ( typeof ctx !== 'object' || ctx === null ) {
80- throw new Error ( 'Context must be an object to use setValue.' ) ;
127+ throw new Error ( 'Context must be an object or Map to use setValue.' ) ;
81128 }
82129 ( ctx as any ) [ key ] = value ;
83130}
84131
85132export function getValue < TContext extends object = DefaultContext , K extends keyof TContext = keyof TContext > (
86- key : K
133+ key : K ,
134+ contextKey ?: any
87135) : TContext [ K ] | undefined {
88- const ctx = getContext < TContext > ( ) ;
136+ const ctx = getContext < any > ( contextKey ) ;
89137 if ( ! ctx ) return undefined ;
138+ if ( ctx instanceof Map ) {
139+ return ctx . get ( key ) ;
140+ }
90141 if ( typeof ctx !== 'object' ) {
91- throw new Error ( 'Context must be an object to use getValue.' ) ;
142+ throw new Error ( 'Context must be an object or Map to use getValue.' ) ;
92143 }
93144 return ( ctx as any ) [ key ] ;
94145}
95146
96- export function mergeContext < TContext extends object = DefaultContext > ( partial : Partial < TContext > ) : void {
97- const ctx = requireContext < TContext > ( ) ;
147+ export function mergeContext < TContext extends object = DefaultContext > (
148+ partial : Partial < TContext > ,
149+ contextKey ?: any
150+ ) : void {
151+ const ctx = requireContext < any > ( contextKey ) ;
152+ if ( ctx instanceof Map ) {
153+ for ( const [ key , value ] of Object . entries ( partial ) ) {
154+ ctx . set ( key , value ) ;
155+ }
156+ return ;
157+ }
98158 if ( typeof ctx !== 'object' || ctx === null ) {
99- throw new Error ( 'Context must be an object to use mergeContext.' ) ;
159+ throw new Error ( 'Context must be an object or Map to use mergeContext.' ) ;
100160 }
101161 Object . assign ( ctx , partial ) ;
102162}
0 commit comments