File

src/lib/method.directive.ts

Implements

OnInit

Metadata

Index

Properties
Methods
Inputs
Outputs
HostListeners
Accessors

Inputs

immediately
Type : boolean
Default value : false
parameters
Type : Parameters
rxapConfirm
Type : any
rxapMethod
Type : Method<ReturnType | Parameters>

Outputs

executed
Type : EventEmitter
failure
Type : EventEmitter
successful
Type : EventEmitter

HostListeners

click
confirmed

Methods

Public Async execute
execute()
Returns : Promise<void>
Protected executed
executed(result: any)
Parameters :
Name Type Optional
result any No
Returns : void
Protected failure
failure(error: Error)
Parameters :
Name Type Optional
error Error No
Returns : void
Public onClick
onClick()
Decorators :
@HostListener('click')
Returns : any
Public onConfirmed
onConfirmed()
Decorators :
@HostListener('confirmed')
Returns : Promise<void>
Public setParameter
setParameter(parameterKey: Key, value)
Type parameters :
  • Key
Parameters :
Name Type Optional
parameterKey Key No
value No
Returns : void
Protected successful
successful(result: ReturnType)
Parameters :
Name Type Optional
result ReturnType No
Returns : void
Public updateParameters
updateParameters(parameters: Partial)
Parameters :
Name Type Optional
parameters Partial<Parameters> No
Returns : void

Properties

Public executing$
Default value : new ToggleSubject()

true - a remote method call is in progress

Accessors

hasConfirmDirective
sethasConfirmDirective(value: any)
Parameters :
Name Type Optional
value any No
Returns : void
import {
  Directive,
  EventEmitter,
  HostListener,
  Input,
  isDevMode,
  OnInit,
  Output,
} from '@angular/core';
import { coerceBoolean } from '@rxap/utilities';
import { ToggleSubject } from '@rxap/rxjs';
import { Method } from '@rxap/pattern';

@Directive({
  selector: '[rxapMethod]',
  exportAs: 'rxapMethod',
  standalone: true,
})
export class MethodDirective<ReturnType = any, Parameters = any> implements OnInit {

  @Input('rxapMethod')
  public method!: Method<ReturnType, Parameters>;
  @Input()
  public parameters?: Parameters;
  @Input()
  public immediately = false;
  @Output('executed')
  public executed$ = new EventEmitter();
  @Output('failure')
  public failure$ = new EventEmitter<Error>();
  @Output('successful')
  public successful$ = new EventEmitter<ReturnType>();
  /**
   * true - a remote method call is in progress
   */
  public executing$ = new ToggleSubject();

  private _hasConfirmDirective = false;

  @Input('rxapConfirm')
  public set hasConfirmDirective(value: any) {
    this._hasConfirmDirective = coerceBoolean(value);
  }

  public ngOnInit() {
    if (this.immediately) {
      this.execute();
    }
  }

  @HostListener('confirmed')
  public onConfirmed() {
    return this.execute();
  }

  @HostListener('click')
  public onClick() {
    if (!this._hasConfirmDirective) {
      return this.execute();
    } else {
      if (isDevMode()) {
        console.debug('Skip method call. Wait for confirmation.');
      }
    }
    return Promise.resolve();
  }

  public async execute(): Promise<void> {
    this.executing$.enable();
    try {
      const result = await this.method.call(this.parameters);
      this.executed(result);
      this.successful(result);
    } catch (error: any) {
      if (isDevMode()) {
        console.error(`Method directive execution failed:`, error.message);
      }
      this.failure(error);
    } finally {
      this.executing$.disable();
    }
  }

  public setParameter<Key extends keyof Parameters>(parameterKey: Key, value: Parameters[Key]): void {
    this.updateParameters({ [parameterKey]: value } as any);
  }

  public updateParameters(parameters: Partial<Parameters>): void {
    this.parameters = { ...(this.parameters ?? {}), ...parameters } as any;
  }

  protected executed(result: any) {
    this.executed$.emit(result);
  }

  protected failure(error: Error) {
    this.failure$.emit(error);
  }

  protected successful(result: ReturnType) {
    this.successful$.emit(result);
  }

}


results matching ""

    No results matching ""