Skip to content
This repository was archived by the owner on Sep 17, 2023. It is now read-only.

Commit 373f750

Browse files
committed
FnContextKey.byDefault constructs default function
1 parent b9f1d38 commit 373f750

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/fn-context-key.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,28 @@ export class FnContextKey<Args extends any[], Ret = void>
3434
implements FnContextRef<Args, Ret, AfterEvent<((this: void, ...args: Args) => Ret)[]>> {
3535

3636
/**
37-
* A function that will be called unless the function or fallback provided.
37+
* Constructs a function that will be called unless fallback provided.
3838
*/
39-
readonly byDefault: (this: void, ...args: Args) => Ret;
39+
readonly byDefault: (this: void, context: ContextValues) => (this: void, ...args: Args) => Ret;
4040

4141
/**
4242
* Constructs updatable context function key.
4343
*
4444
* @param name Human-readable key name.
4545
* @param seedKey Value seed key. A new one will be constructed when omitted.
46-
* @param byDefault The default function to call. If unspecified then the default function would raise an error.
46+
* @param byDefault Constructs a default function to call. If unspecified then the default function would raise
47+
* an error.
4748
*/
4849
constructor(
4950
name: string,
5051
{
5152
seedKey,
52-
byDefault = () => { throw new ContextKeyError(this); },
53+
byDefault = () => () => { throw new ContextKeyError(this); },
5354
}: {
5455
seedKey?: ContextSeedKey<
5556
((this: void, ...args: Args) => Ret) | EventKeeper<((this: void, ...args: Args) => Ret)[]>,
5657
AfterEvent<((this: void, ...args: Args) => Ret)[]>>,
57-
byDefault?: (this: void, ...args: Args) => Ret,
58+
byDefault?: (this: void, context: ContextValues) => (this: void, ...args: Args) => Ret,
5859
} = {},
5960
) {
6061
super(name, seedKey);
@@ -76,9 +77,9 @@ export class FnContextKey<Args extends any[], Ret = void>
7677
delegated = fns[fns.length - 1];
7778
} else {
7879

79-
const fallback = opts.byDefault(() => this.byDefault);
80+
const fallback = opts.byDefault(() => this.byDefault(opts.context));
8081

81-
delegated = fallback || this.byDefault;
82+
delegated = fallback || this.byDefault(opts.context);
8283
}
8384
});
8485

0 commit comments

Comments
 (0)