Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/gossi/ember-command. Integrate your Business Logic with Ember gossi.github.io/ember-command
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211import { assert } from '@ember/debug';
import { Link } from 'ember-link';
import { setOwner } from './-owner.ts';import { ACTION } from './action.ts';import { Command } from './command.ts';import { LinkCommand } from './link-command.ts';
import type { Action, AnyFunction } from './action.ts';import type Owner from '@ember/owner';
// see: https://github.com/gossi/ember-command/issues/23// const INVOCABLES = Symbol('INVOCABLES');const INVOCABLES = '__INVOCABLES__';
type Invocable = Command | AnyFunction;
export interface CommandInstance { (...args: unknown[]): void | Promise<void>; link?: Link; [INVOCABLES]: Invocable[];}
export type Commandable = | AnyFunction | Command | LinkCommand | Link | CommandInstance | Action<AnyFunction>;export type CommandAction = AnyFunction | Command | LinkCommand | Link | CommandInstance;
const LINK_PROPERTIES = [ 'active', 'activeWithoutModels', 'activeWithoutQueryParams', 'entering', 'exiting', // 'open', // as of `ember-link@3` 'transitionTo', 'replaceWith', 'qualifiedRouteName', 'url', 'models', 'queryParams'];
function getAllPropertyNames(obj: object) { const names: string[] = [];
do { names.push(...Object.getOwnPropertyNames(obj)); // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment obj = Object.getPrototypeOf(obj); } while (obj !== Object.prototype);
return names.filter((name) => name !== 'constructor');}
function isLink(commandable: Commandable): commandable is Link { // `instanceOf` is not a reliable check, only when the host app runs with // embroider. In classic mode, the ember-link instance in the host app and in // ember-command addon are different and the check will fail, so this performs // some duck-type check const props = getAllPropertyNames(commandable);
// the first check should be sufficient enough, but isn't due to: // https://github.com/gossi/ember-command/issues/23 // so, there is another duck-type check for the link return commandable instanceof Link || LINK_PROPERTIES.every((prop) => props.includes(prop));}
function isCommandInstance(commandable: Commandable): commandable is CommandInstance { return Object.getOwnPropertyNames(commandable).includes(INVOCABLES);}
export function isCommand(commandable: unknown): commandable is Command { return ( // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition (commandable as Command).execute !== undefined && typeof (commandable as Command).execute === 'function' );}
function isAction(commandable: Commandable): commandable is Action<AnyFunction> { return Object.prototype.hasOwnProperty.call(commandable, ACTION);}
function containsLink(commandable: Commandable) { // the correct check would be, the first commented out one, but can't be due to: // https://github.com/gossi/ember-command/issues/23 // if (commandable instanceof Link || commandable instanceof LinkCommand) { if (isLink(commandable) || LinkCommand.isLinkCommand(commandable)) { return true; }
return ( isCommandInstance(commandable) && commandable.link !== undefined && isLink(commandable.link) );}
export function getLink(commandable: Commandable) { if (isLink(commandable)) { return commandable; }
if ( isCommandInstance(commandable) && commandable.link !== undefined && isLink(commandable.link) ) { return commandable.link; }}
function getLinkCommand(commandable: Commandable) { if (LinkCommand.isLinkCommand(commandable)) { return commandable; }}
export function createCommandInstance( owner: Owner, composition: Commandable | Commandable[]): CommandInstance { const commandables = Array.isArray(composition) ? composition : [composition];
// find the (first) link const potentialLink = commandables.find((commandable) => { return containsLink(commandable); }); const link = potentialLink ? (getLink(potentialLink) ?? getLinkCommand(potentialLink)) : undefined;
// keep remaining invocables const invocables = commandables.filter( (commandable) => isCommand(commandable) || typeof commandable === 'function' ) as Invocable[];
// set owner to commands invocables.flatMap((commandable) => { // this is failing due to: https://github.com/gossi/ember-command/issues/23 // if (commandable instanceof Command) { if (isCommand(commandable)) { setOwner(commandable, owner); }
if (isCommandInstance(commandable)) { return commandable[INVOCABLES]; }
return commandable; });
const instance = async function (this: CommandInstance, ...args: never[]) { const invocations = [];
for (const fn of invocables) { if (isCommand(fn)) { invocations.push(fn.execute(...args)); } else if (isAction(fn)) { invocations.push(fn(owner)(...args)); } else { invocations.push(fn(...args)); } }
await Promise.all(invocations); };
(instance as CommandInstance)[INVOCABLES] = invocables;
if (link && LinkCommand.isLinkCommand(link)) { const linkManager = owner.lookup('service:link-manager');
assert(`missing 'service:link-manager' for 'LinkCommand'`, linkManager); instance.link = linkManager.createLink(link.params); } else if (link && isLink(link)) { instance.link = link; }
return instance as CommandInstance;}
function isCommandable(commandable: unknown): commandable is Commandable { return ( typeof commandable === 'function' || commandable instanceof Command || isCommand(commandable) || commandable instanceof LinkCommand || LinkCommand.isLinkCommand(commandable) || commandable instanceof Link || isLink(commandable as Commandable) );}
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituentsexport function commandFor(commands: unknown | unknown[]): CommandInstance { assert( // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `${commands} do not appear to be a command`, commands && Array.isArray(commands) ? commands.every((commandable) => isCommandable(commandable)) : isCommandable(commands) );
return commands as CommandInstance;}