@ssv/ngx.command
Version:
Command pattern implementation for angular. Command used to encapsulate information which is needed to perform an action.
66 lines (65 loc) • 3.15 kB
TypeScript
import { Observable } from "rxjs";
import type { ICommand } from "./command.model";
import { Injector, type Signal } from "@angular/core";
export type ExecuteFn = (...args: any[]) => unknown;
export type ExecuteAsyncFn = (...args: any[]) => Observable<unknown> | Promise<unknown>;
export type CanExecute = (() => boolean) | Signal<boolean> | Observable<boolean>;
export 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.
*/
export 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.
*/
export 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.
*/
export 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.
*/
export declare class CommandAsync extends Command {
constructor(execute: ExecuteAsyncFn, canExecute$?: CanExecute);
}