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 Oct 16, 2021
1 parent 6f9b8b1 commit c4b2975
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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)';
}
}
3 changes: 3 additions & 0 deletions packages/@glimmer/manager/lib/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { CustomComponentManager } from './component';
import { CustomModifierManager } from './modifier';
import { CustomHelperManager } from './helper';
import { FunctionHelperManager } from '../internal/defaults';

type Manager = ComponentManager<unknown> | ModifierManager<unknown> | HelperManager<unknown>;

Expand All @@ -33,3 +34,5 @@ export function setHelperManager<O extends Owner, T extends object>(
): T {
return setInternalHelperManager(new CustomHelperManager(factory), obj);
}

setHelperManager(() => new FunctionHelperManager(), Function.prototype);

0 comments on commit c4b2975

Please sign in to comment.