import { capabilities, setHelperManager } from '@ember/helper'; import { sweetenOwner } from 'ember-sweet-owner'; import type Owner from '@ember/owner'; import type { HelperCapabilities } from '@glimmer/interfaces'; import type { SweetOwner } from 'ember-sweet-owner'; export const ACTION = Symbol('action'); interface Args { positional: never[]; named: object; } // eslint-disable-next-line @typescript-eslint/no-explicit-any export type AnyFunction = (...args: any[]) => any; type ActionFactory = (owner: SweetOwner) => F; /** * Representing an action build with the `action()` factory */ export type Action = ((owner: Owner) => F) & (() => F); class ActionFactoryManager { capabilities: HelperCapabilities = capabilities('3.23', { hasValue: true }); constructor(protected owner: Owner) {} createHelper(invoker: Action, args: Args) { return { fn: invoker(this.owner), args }; } getValue({ fn, args }: { fn: F; args: Args }) { return (...params: never[]) => { const parameters = [...args.positional, ...params] as unknown as Parameters; // eslint-disable-next-line @typescript-eslint/no-unsafe-return return fn(...parameters); }; } } // Provide a singleton manager. const ActionFactoryManagerInstance = (owner: Owner) => new ActionFactoryManager(owner); export function action(factory: ActionFactory): Action { const an = (owner: Owner) => (...args: Parameters) => // eslint-disable-next-line @typescript-eslint/no-unsafe-return factory(sweetenOwner(owner))(...args); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore an[ACTION] = ACTION; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore setHelperManager(ActionFactoryManagerInstance, an); return an as unknown as Action; }