@ssv/ngx.command
Version:
Command pattern implementation for angular. Command used to encapsulate information which is needed to perform an action.
193 lines (183 loc) • 9.04 kB
TypeScript
import * as _angular_core from '@angular/core';
import { InjectionToken, Provider, Signal, OnInit, Injector } from '@angular/core';
import { Observable } from 'rxjs';
import { AbstractControl } from '@angular/forms';
interface CommandOptions {
/**
* Css Class which gets added/removed on the Command element's host while Command `isExecuting$`.
*/
executingCssClass: string;
/** Determines whether the disabled will be handled by the directive or not.
* Disable handled by directive's doesn't always play nice when used with other component/pipe/directive and they also handle disabled.
* This disables the handling manually and need to pass explicitly `[disabled]="!saveCmd.canExecute"`.
*/
handleDisabled: boolean;
}
declare const COMMAND_OPTIONS: InjectionToken<CommandOptions>;
declare function provideSsvCommandOptions(options: Partial<CommandOptions> | ((defaults: Readonly<CommandOptions>) => Partial<CommandOptions>)): Provider[];
type ExecuteFn = (...args: any[]) => unknown;
type ExecuteAsyncFn = (...args: any[]) => Observable<unknown> | Promise<unknown>;
type CanExecute = (() => boolean) | Signal<boolean> | Observable<boolean>;
interface ICommand {
/** Determines whether the command is currently executing, as a snapshot value.
* @deprecated Use {@link $isExecuting} signal instead.
*/
readonly isExecuting: boolean;
/** Determines whether the command is currently executing, as a signal. */
readonly $isExecuting: Signal<boolean>;
/** Determines whether the command can execute or not, as a snapshot value.
* @deprecated Use {@link $canExecute} signal instead.
*/
readonly canExecute: boolean;
/** Determines whether the command can execute or not, as a signal. */
readonly $canExecute: Signal<boolean>;
/** Determines whether to auto destroy when having 0 subscribers (defaults to `true`).
* @deprecated Use using command/commandAsync is handled automatically.
*/
autoDestroy: boolean;
/** Execute function to invoke. */
execute(...args: unknown[]): void;
/** Disposes all resources held by subscriptions. */
destroy(): void;
/** Subscribe listener, in order to handle auto disposing.
* @deprecated Use using command/commandAsync is handled automatically.
*/
subscribe(): void;
/**
* Unsubscribe listener, in order to handle auto disposing.
* @deprecated Use using command/commandAsync is handled automatically.
*/
unsubscribe(): void;
}
interface CommandCreator {
execute: (...args: any[]) => Observable<unknown> | Promise<unknown> | void;
canExecute?: CanExecute | Function;
params?: unknown | unknown[];
isAsync?: boolean;
host: unknown;
}
declare class SsvCommand implements OnInit {
#private;
readonly commandOrCreator: _angular_core.InputSignal<ICommand | CommandCreator>;
readonly ssvCommandOptions: _angular_core.InputSignal<Partial<CommandOptions>>;
readonly commandOptions: _angular_core.Signal<CommandOptions>;
readonly ssvCommandParams: _angular_core.InputSignal<unknown>;
readonly commandParams: _angular_core.Signal<unknown>;
readonly _hostClasses: _angular_core.Signal<string[]>;
private creatorParams;
get command(): ICommand;
private _command;
constructor();
ngOnInit(): void;
_handleClick(): void;
private trySetDisabled;
static ɵfac: _angular_core.ɵɵFactoryDeclaration<SsvCommand, never>;
static ɵdir: _angular_core.ɵɵDirectiveDeclaration<SsvCommand, "[ssvCommand]", ["ssvCommand"], { "commandOrCreator": { "alias": "ssvCommand"; "required": true; "isSignal": true; }; "ssvCommandOptions": { "alias": "ssvCommandOptions"; "required": false; "isSignal": true; }; "ssvCommandParams": { "alias": "ssvCommandParams"; "required": false; "isSignal": true; }; }, {}, never, never, true, never>;
}
/**
* Command creator ref, directive which allows creating Command in the template
* and associate it to a command (in order to share executions).
* @example
* ### Most common usage
* ```html
* <div #actionCmd="ssvCommandRef" [ssvCommandRef]="{host: this, execute: removeHero$, canExecute: isValid$}">
* <button [ssvCommand]="actionCmd.command" [ssvCommandParams]="hero">
* Remove
* </button>
* <button [ssvCommand]="actionCmd.command" [ssvCommandParams]="hero">
* Remove
* </button>
* </div>
* ```
*
*/
declare class SsvCommandRef implements OnInit {
#private;
readonly commandCreator: _angular_core.InputSignal<CommandCreator>;
get command(): ICommand;
private _command;
constructor();
ngOnInit(): void;
static ɵfac: _angular_core.ɵɵFactoryDeclaration<SsvCommandRef, never>;
static ɵdir: _angular_core.ɵɵDirectiveDeclaration<SsvCommandRef, "[ssvCommandRef]", ["ssvCommandRef"], { "commandCreator": { "alias": "ssvCommandRef"; "required": true; "isSignal": true; }; }, {}, never, never, true, never>;
}
/** @deprecated Use standalone instead. */
declare class SsvCommandModule {
static ɵfac: _angular_core.ɵɵFactoryDeclaration<SsvCommandModule, never>;
static ɵmod: _angular_core.ɵɵNgModuleDeclaration<SsvCommandModule, never, [typeof SsvCommand, typeof SsvCommandRef], [typeof SsvCommand, typeof SsvCommandRef]>;
static ɵinj: _angular_core.ɵɵInjectorDeclaration<SsvCommandModule>;
}
interface CommandCreateOptions {
isAsync: boolean;
injector?: Injector;
}
/** Creates an async {@link Command}. Must be used within an injection context.
* NOTE: this auto injects `DestroyRef` and handles auto destroy. {@link ICommand.autoDestroy} should not be used.
*/
declare function commandAsync(execute: ExecuteAsyncFn, canExecute$?: CanExecute, opts?: Omit<CommandCreateOptions, "isAsync">): Command;
/** Creates a {@link Command}. Must be used within an injection context.
* NOTE: this auto injects `DestroyRef` and handles auto destroy. {@link ICommand.autoDestroy} should not be used.
*/
declare function command(execute: ExecuteFn, canExecute$?: CanExecute, opts?: CommandCreateOptions): Command;
/**
* Command object used to encapsulate information which is needed to perform an action.
*
*/
declare class Command implements ICommand {
get isExecuting(): boolean;
get canExecute(): boolean;
readonly $isExecuting: _angular_core.WritableSignal<boolean>;
readonly $canExecute: Signal<boolean>;
private readonly _$canExecute;
autoDestroy: boolean;
private executionPipe$;
private executionPipe$$;
private subscribersCount;
/**
* Creates an instance of Command.
*
* @param execute Execute function to invoke - use `isAsync: true` when `Observable<any>`.
* @param canExecute Observable which determines whether it can execute or not.
* @param isAsync Indicates that the execute function is async e.g. Observable.
* @deprecated Use {@link command} or {@link commandAsync} instead for creating instances.
*/
constructor(execute: ExecuteFn, canExecute$?: CanExecute, isAsync?: boolean, injector?: Injector);
/** Execute function to invoke. */
execute(...args: unknown[]): void;
/** Disposes all resources held by subscriptions. */
destroy(): void;
subscribe(): void;
unsubscribe(): void;
private buildExecutionPipe;
}
/**
* Async Command object used to encapsulate information which is needed to perform an action,
* which takes an execute function as Observable/Promise.
* @deprecated Use {@link commandAsync} instead.
*/
declare class CommandAsync extends Command {
/**
* @deprecated Use {@link commandAsync} instead to create an instance.
*/
constructor(execute: ExecuteAsyncFn, canExecute$?: CanExecute);
}
/** Determines whether the arg object is of type `Command`. */
declare function isCommand(arg: unknown): arg is ICommand;
/** Determines whether the arg object is of type `CommandCreator`. */
declare function isCommandCreator(arg: unknown): arg is CommandCreator;
interface CanExecuteFormOptions {
/** Determines whether to check for validity. (defaults: true) */
validity?: boolean;
/** Determines whether to check whether UI has been touched. (defaults: true) */
dirty?: boolean;
}
/** Get can execute from form validity/pristine as an observable. */
declare function canExecuteFromNgForm(form: AbstractControl, options?: CanExecuteFormOptions): Observable<boolean>;
/** Can executed based on valid/dirty signal inputs. */
declare function canExecuteFromSignals(signals: {
valid: Signal<boolean>;
dirty: Signal<boolean>;
}, options?: CanExecuteFormOptions): Signal<boolean>;
declare const VERSION = "4.0.0-dev.83";
export { COMMAND_OPTIONS, Command, CommandAsync, SsvCommand, SsvCommandModule, SsvCommandRef, VERSION, canExecuteFromNgForm, canExecuteFromSignals, command, commandAsync, isCommand, isCommandCreator, provideSsvCommandOptions };
export type { CanExecute, CanExecuteFormOptions, CommandCreateOptions, CommandCreator, CommandOptions, ExecuteAsyncFn, ExecuteFn, ICommand };