From c4b29755c4d6efecec981b5154a336bba815e7b7 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli Date: Sat, 16 Oct 2021 13:28:44 -0400 Subject: [PATCH] Implementation for RFC #756 --- .../@glimmer/manager/lib/internal/defaults.ts | 36 +++++++++++++++++++ packages/@glimmer/manager/lib/public/index.ts | 3 ++ 2 files changed, 39 insertions(+) create mode 100644 packages/@glimmer/manager/lib/internal/defaults.ts diff --git a/packages/@glimmer/manager/lib/internal/defaults.ts b/packages/@glimmer/manager/lib/internal/defaults.ts new file mode 100644 index 0000000000..0a698ba064 --- /dev/null +++ b/packages/@glimmer/manager/lib/internal/defaults.ts @@ -0,0 +1,36 @@ +import { CAPABILITIES, CapturedArguments as Arguments } from '@glimmer/interfaces'; + +type FnArgs = + | [...Args['positional'], Args['named']] + | [...Args['positional'], {}]; + +interface FunctionHelperState { + fn: (...args: FnArgs) => Return; + args: Args; +} + +export class FunctionHelperManager { + 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 = [ + ...args.positional, + Object.keys(args.named).length > 0 ? args.named : {}, + ]; + + return fn(...argsForFn); + } + + getDebugName(fn: State['fn']) { + return fn.name || '(anonymous function)'; + } +} diff --git a/packages/@glimmer/manager/lib/public/index.ts b/packages/@glimmer/manager/lib/public/index.ts index 3766b96726..2e46653057 100644 --- a/packages/@glimmer/manager/lib/public/index.ts +++ b/packages/@glimmer/manager/lib/public/index.ts @@ -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 | ModifierManager | HelperManager; @@ -33,3 +34,5 @@ export function setHelperManager( ): T { return setInternalHelperManager(new CustomHelperManager(factory), obj); } + +setHelperManager(() => new FunctionHelperManager(), Function.prototype);