1
+ import { noop } from 'call-thru' ;
1
2
import { AfterEvent , EventKeeper } from 'fun-events' ;
2
- import { ContextSeedKey , ContextValueOpts } from './context-key' ;
3
+ import { ContextKeyDefault , ContextSeedKey , ContextValueOpts } from './context-key' ;
3
4
import { ContextKeyError } from './context-key-error' ;
4
5
import { ContextUpKey , ContextUpRef } from './context-up-key' ;
5
6
import { ContextValues } from './context-values' ;
@@ -36,7 +37,8 @@ export class FnContextKey<Args extends any[], Ret = void>
36
37
/**
37
38
* Constructs a function that will be called unless fallback provided.
38
39
*/
39
- readonly byDefault : ( this : void , context : ContextValues ) => ( this : void , ...args : Args ) => Ret ;
40
+ readonly byDefault : ( this : void , context : ContextValues , key : FnContextKey < Args , Ret > ) =>
41
+ ( this : void , ...args : Args ) => Ret ;
40
42
41
43
/**
42
44
* Constructs updatable context function key.
@@ -50,16 +52,16 @@ export class FnContextKey<Args extends any[], Ret = void>
50
52
name : string ,
51
53
{
52
54
seedKey,
53
- byDefault = ( ) => ( ) => { throw new ContextKeyError ( this ) ; } ,
55
+ byDefault = noop ,
54
56
} : {
55
57
seedKey ?: ContextSeedKey <
56
58
( ( this : void , ...args : Args ) => Ret ) | EventKeeper < ( ( this : void , ...args : Args ) => Ret ) [ ] > ,
57
59
AfterEvent < ( ( this : void , ...args : Args ) => Ret ) [ ] > > ,
58
- byDefault ?: ( this : void , context : ContextValues ) => ( this : void , ...args : Args ) => Ret ,
60
+ byDefault ?: ContextKeyDefault < ( this : void , ...args : Args ) => Ret , FnContextKey < Args , Ret > > ,
59
61
} = { } ,
60
62
) {
61
63
super ( name , seedKey ) ;
62
- this . byDefault = byDefault ;
64
+ this . byDefault = ( context , key ) => byDefault ( context , key ) || ( ( ) => { throw new ContextKeyError ( this ) ; } ) ;
63
65
}
64
66
65
67
grow < Ctx extends ContextValues > (
@@ -77,9 +79,10 @@ export class FnContextKey<Args extends any[], Ret = void>
77
79
delegated = fns [ fns . length - 1 ] ;
78
80
} else {
79
81
80
- const fallback = opts . byDefault ( ( ) => this . byDefault ( opts . context ) ) ;
82
+ const defaultProvider = ( ) => this . byDefault ( opts . context , this ) ;
83
+ const fallback = opts . byDefault ( defaultProvider ) ;
81
84
82
- delegated = fallback || this . byDefault ( opts . context ) ;
85
+ delegated = fallback || defaultProvider ( ) ;
83
86
}
84
87
} ) ;
85
88
0 commit comments