-
Notifications
You must be signed in to change notification settings - Fork 191
/
defaults.ts
48 lines (37 loc) · 1.1 KB
/
defaults.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import type {
CapturedArguments as Arguments,
HelperCapabilities,
HelperManagerWithValue,
} from "@glimmer/interfaces";
import { buildCapabilities } from '../util/capabilities';
type FnArgs<Args extends Arguments = Arguments> =
| [...Args['positional'], Args['named']]
| [...Args['positional']];
type AnyFunction = (...args: any[]) => unknown;
interface State {
fn: AnyFunction;
args: Arguments;
}
export class FunctionHelperManager implements HelperManagerWithValue<State> {
capabilities = buildCapabilities({
hasValue: true,
hasDestroyable: false,
hasScheduledEffect: false,
}) as HelperCapabilities;
createHelper(fn: AnyFunction, args: Arguments): State {
return { fn, args };
}
getValue({ fn, args }: State): unknown {
if (Object.keys(args.named).length > 0) {
let argsForFn: FnArgs<Arguments> = [...args.positional, args.named];
return fn(...argsForFn);
}
return fn(...args.positional);
}
getDebugName(fn: AnyFunction): string {
if (fn.name) {
return `(helper function ${fn.name})`;
}
return '(anonymous helper function)';
}
}