Skip to content

Commit

Permalink
Implementation for RFC #756
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Nov 2, 2021
1 parent 0ac3cdf commit bfcc8a9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
36 changes: 36 additions & 0 deletions packages/@glimmer/manager/lib/internal/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { CAPABILITIES, CapturedArguments as Arguments } 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 = {
[CAPABILITIES]: true,
hasValue: true,
hasDestroyable: false,
hasScheduledEffect: false,
} as const;

createHelper(fn: State['fn'], args: State['args']) {
return { fn, args };
}

getValue({ fn, args }: State) {
let argsForFn: FnArgs<Arguments> = [
...args.positional,
Object.keys(args.named).length > 0 ? args.named : {},
];

return fn(...argsForFn);
}

getDebugName(fn: State['fn']) {
return fn.name || '(anonymous function)';
}
}
14 changes: 13 additions & 1 deletion packages/@glimmer/manager/lib/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Owner,
} from '@glimmer/interfaces';
import { CustomHelperManager } from '../public/helper';
import { FunctionHelperManager } from './defaults';

type InternalManager =
| InternalComponentManager
Expand Down Expand Up @@ -138,7 +139,18 @@ export function getInternalHelperManager(
);
}

const manager = getManager(HELPER_MANAGERS, definition)!;
let manager = getManager(HELPER_MANAGERS, definition)!;

// Functions are special-cased because functions are defined
// as the "default" helper, per: https://github.com/emberjs/rfcs/pull/756
if (manager === undefined && typeof definition === 'function') {
setInternalHelperManager(
new CustomHelperManager(() => new FunctionHelperManager()),
definition
);

manager = getManager(HELPER_MANAGERS, definition)!;
}

if (manager === undefined) {
if (isOptional === true) {
Expand Down

0 comments on commit bfcc8a9

Please sign in to comment.