-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9e592e
commit cf7f907
Showing
4 changed files
with
203 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { buildCapabilities } from '../util/capabilities'; | ||
|
||
import type { CapturedArguments as Arguments, HelperCapabilities } from '@glimmer/interfaces'; | ||
|
||
type FnArgs<Args extends Arguments = Arguments> = | ||
| [...Args['positional'], Args['named']] | ||
| [...Args['positional'], {}]; | ||
|
||
interface FunctionHelperState<Args extends Arguments = Arguments> { | ||
fn: <Return>(...args: FnArgs<Args>) => Return; | ||
args: Args; | ||
} | ||
|
||
export class FunctionHelperManager<State extends FunctionHelperState> { | ||
capabilities = buildCapabilities({ | ||
hasValue: true, | ||
hasDestroyable: false, | ||
hasScheduledEffect: false, | ||
}) as HelperCapabilities; | ||
|
||
createHelper(fn: State['fn'], args: State['args']) { | ||
return { fn, args }; | ||
} | ||
|
||
getValue({ fn, args }: State) { | ||
let namedKeys = Object.keys(args.named).length; | ||
|
||
if (args.positional.length === 0 && namedKeys === 0) { | ||
return fn(); | ||
} | ||
|
||
// This consumes all positional args | ||
let argsForFn: FnArgs<Arguments> = [...args.positional]; | ||
|
||
if (namedKeys > 0) { | ||
// This consumes all named args | ||
argsForFn.push(args.named); | ||
} | ||
|
||
return fn(...argsForFn); | ||
} | ||
|
||
getDebugName(fn: State['fn']) { | ||
if (fn.name) { | ||
return `(helper function ${fn.name})`; | ||
} | ||
|
||
return '(anonymous helper function)'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters