@ssv/ngx.command
Version:
Command pattern implementation for angular. Command used to encapsulate information which is needed to perform an action.
191 lines (181 loc) • 9.07 kB
TypeScript
import * as i0 from '@angular/core';
import { InjectionToken, EnvironmentProviders, OnInit, OnDestroy, Signal, 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;
/** Determine whether to set a `delay(1)` when setting the disabled. Which might be needed when working with external
* components/directives (such as material button)
*/
hasDisabledDelay: boolean;
}
declare const COMMAND_OPTIONS: InjectionToken<CommandOptions>;
declare function provideSsvCommandOptions(options: Partial<CommandOptions> | ((defaults: Readonly<CommandOptions>) => Partial<CommandOptions>)): EnvironmentProviders;
interface ICommand {
/** Determines whether the command is currently executing, as a snapshot value. */
readonly isExecuting: boolean;
/** Determines whether the command is currently executing, as an observable. */
readonly isExecuting$: Observable<boolean>;
/** Determines whether the command can execute or not, as a snapshot value. */
readonly canExecute: boolean;
/** Determines whether the command can execute or not, as an observable. */
readonly canExecute$: Observable<boolean>;
/** Determines whether to auto destroy when having 0 subscribers (defaults to `true`). */
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. */
subscribe(): void;
/** Unsubscribe listener, in order to handle auto disposing. */
unsubscribe(): void;
}
interface CommandCreator {
execute: (...args: any[]) => Observable<unknown> | Promise<unknown> | void;
canExecute?: Observable<boolean> | Function;
params?: unknown | unknown[];
isAsync?: boolean;
host: unknown;
}
declare class CommandDirective implements OnInit, OnDestroy {
private readonly globalOptions;
private readonly renderer;
private readonly element;
private readonly cdr;
commandOrCreator: ICommand | CommandCreator | undefined;
get commandOptions(): CommandOptions;
set commandOptions(value: Partial<CommandOptions>);
commandParams: unknown | unknown[];
get command(): ICommand;
private _command;
private _commandOptions;
private _destroy$;
ngOnInit(): void;
onClick(): void;
ngOnDestroy(): void;
private trySetDisabled;
static ɵfac: i0.ɵɵFactoryDeclaration<CommandDirective, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<CommandDirective, "[ssvCommand]", ["ssvCommand"], { "commandOrCreator": { "alias": "ssvCommand"; "required": false; }; "commandOptions": { "alias": "ssvCommandOptions"; "required": false; }; "commandParams": { "alias": "ssvCommandParams"; "required": false; }; }, {}, 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 CommandRefDirective implements OnInit, OnDestroy {
commandCreator: CommandCreator | undefined;
get command(): ICommand;
private _command;
ngOnInit(): void;
ngOnDestroy(): void;
static ɵfac: i0.ɵɵFactoryDeclaration<CommandRefDirective, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<CommandRefDirective, "[ssvCommandRef]", ["ssvCommandRef"], { "commandCreator": { "alias": "ssvCommandRef"; "required": false; }; }, {}, never, never, true, never>;
}
declare class SsvCommandModule {
static ɵfac: i0.ɵɵFactoryDeclaration<SsvCommandModule, never>;
static ɵmod: i0.ɵɵNgModuleDeclaration<SsvCommandModule, never, [typeof CommandDirective, typeof CommandRefDirective], [typeof CommandDirective, typeof CommandRefDirective]>;
static ɵinj: i0.ɵɵInjectorDeclaration<SsvCommandModule>;
}
type ExecuteFn = (...args: any[]) => unknown;
type ExecuteAsyncFn = (...args: any[]) => Observable<unknown> | Promise<unknown>;
type CanExecute = (() => boolean) | Signal<boolean> | Observable<boolean>;
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.
* @deprecated Use {@link command} or {@link commandAsync} instead for creating instances.
*/
declare class Command implements ICommand {
/** Determines whether the command is currently executing, as a snapshot value. */
get isExecuting(): boolean;
/** Determines whether the command can execute or not, as a snapshot value. */
get canExecute(): boolean;
/** Determines whether the command is currently executing, as an observable. */
get isExecuting$(): Observable<boolean>;
/** Determines whether to auto destroy when having 0 subscribers. */
autoDestroy: boolean;
/** Determines whether the command can execute or not, as an observable. */
readonly canExecute$: Observable<boolean>;
private _isExecuting$;
private _isExecuting;
private _canExecute;
private executionPipe$;
private isExecuting$$;
private canExecute$$;
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.
*/
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 {
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 = "3.3.0-dev.76";
export { COMMAND_OPTIONS, Command, CommandAsync, CommandDirective, CommandRefDirective, SsvCommandModule, VERSION, canExecuteFromNgForm, canExecuteFromSignals, command, commandAsync, isCommand, isCommandCreator, provideSsvCommandOptions };
export type { CanExecute, CanExecuteFormOptions, CommandCreateOptions, CommandCreator, CommandOptions, ICommand };