src/lib/method.directive.ts
OnInit
| Selector | [rxapMethod] |
| Standalone | true |
| exportAs | rxapMethod |
Properties |
|
Methods |
|
Inputs |
Outputs |
HostListeners |
Accessors |
| immediately | |
Type : boolean
|
|
Default value : false
|
|
|
Defined in src/lib/method.directive.ts:26
|
|
| parameters | |
Type : Parameters
|
|
|
Defined in src/lib/method.directive.ts:24
|
|
| rxapConfirm | |
Type : any
|
|
|
Defined in src/lib/method.directive.ts:41
|
|
| rxapMethod | |
Type : Method<ReturnType | Parameters>
|
|
|
Defined in src/lib/method.directive.ts:22
|
|
| executed | |
Type : EventEmitter
|
|
|
Defined in src/lib/method.directive.ts:28
|
|
| failure | |
Type : EventEmitter
|
|
|
Defined in src/lib/method.directive.ts:30
|
|
| successful | |
Type : EventEmitter
|
|
|
Defined in src/lib/method.directive.ts:32
|
|
| click |
|
Defined in src/lib/method.directive.ts:57
|
| confirmed |
|
Defined in src/lib/method.directive.ts:52
|
| Public Async execute |
execute()
|
|
Defined in src/lib/method.directive.ts:68
|
|
Returns :
Promise<void>
|
| Protected executed | ||||||
executed(result: any)
|
||||||
|
Defined in src/lib/method.directive.ts:92
|
||||||
|
Parameters :
Returns :
void
|
| Protected failure | ||||||
failure(error: Error)
|
||||||
|
Defined in src/lib/method.directive.ts:96
|
||||||
|
Parameters :
Returns :
void
|
| Public onClick |
onClick()
|
Decorators :
@HostListener('click')
|
|
Defined in src/lib/method.directive.ts:57
|
|
Returns :
any
|
| Public onConfirmed |
onConfirmed()
|
Decorators :
@HostListener('confirmed')
|
|
Defined in src/lib/method.directive.ts:52
|
|
Returns :
Promise<void>
|
| Public setParameter | |||||||||
setParameter(parameterKey: Key, value)
|
|||||||||
|
Defined in src/lib/method.directive.ts:84
|
|||||||||
Type parameters :
|
|||||||||
|
Parameters :
Returns :
void
|
| Protected successful | ||||||
successful(result: ReturnType)
|
||||||
|
Defined in src/lib/method.directive.ts:100
|
||||||
|
Parameters :
Returns :
void
|
| Public updateParameters | ||||||
updateParameters(parameters: Partial
|
||||||
|
Defined in src/lib/method.directive.ts:88
|
||||||
|
Parameters :
Returns :
void
|
| Public executing$ |
Default value : new ToggleSubject()
|
|
Defined in src/lib/method.directive.ts:36
|
|
true - a remote method call is in progress |
| hasConfirmDirective | ||||||
sethasConfirmDirective(value: any)
|
||||||
|
Defined in src/lib/method.directive.ts:41
|
||||||
|
Parameters :
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);
}
}