Skip to content

Commit a3b6213

Browse files
refactor(command): rename command without create prefix (#12)
1 parent 29c5ab6 commit a3b6213

File tree

4 files changed

+17
-19
lines changed

4 files changed

+17
-19
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
## 3.0.0 (2024-11-21)
1+
## 3.0.0 (2025-01-21)
22

33
### 🚀 Features
44

5-
- **command:** add `createCommand`/`createCommandAsync` which handles auto destroy with `DestroyRef`
5+
- **command:** add `command`/`commandAsync` which handles auto destroy with `DestroyRef`
66
- **command:** canExecute signal support
77
- **deps:** update angular 17
88
- **ux viewport:** add module `SsvUxViewportModule`

apps/test-app/src/app/command/example-command.component.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { MatIconModule } from "@angular/material/icon";
55
import { MatProgressSpinnerModule } from "@angular/material/progress-spinner";
66

77
import { BehaviorSubject, timer, Observable, tap, filter, map, distinctUntilChanged } from "rxjs";
8-
import { CommandAsync, createCommandAsync, SsvCommandModule } from "@ssv/ngx.command";
8+
import { CommandAsync, commandAsync, SsvCommandModule } from "@ssv/ngx.command";
99
import { CommonModule } from "@angular/common";
1010

1111
interface Hero {
@@ -54,7 +54,7 @@ export class ExampleCommandComponent {
5454
this.saveRedux.bind(this),
5555
this.isValidRedux$,
5656
);
57-
readonly containerDestroySaveCmd = createCommandAsync(() => this.save$());
57+
readonly containerDestroySaveCmd = commandAsync(() => this.save$());
5858

5959
heroes: Hero[] = [
6060
{ key: "rexxar", name: "Rexxar" },

libs/ngx.command/README.md

+8-11
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,15 @@ Choose the version corresponding to your Angular version:
3131
In order to start working with Command, you need to create a new instance of it.
3232

3333
```ts
34-
import { Command, CommandAsync, ICommand } from "@ssv/ngx.command";
34+
import { command, commandAsync } from "@ssv/ngx.command";
3535

3636
isValid$ = new BehaviorSubject(false);
3737

38-
// use `CommandAsync` when execute function returns an observable/promise OR else 3rd argument must be true.
39-
saveCmd = new Command(() => this.save()), this.isValid$);
38+
// non async
39+
saveCmd = command(() => this.save()), this.isValid$);
4040

41-
// using CommandAsync
42-
saveCmd = new CommandAsync(() => Observable.timer(2000), this.isValid$);
43-
44-
// using ICommand interface
45-
saveCmd: ICommand = new CommandAsync(() => Observable.timer(2000), this.isValid$);
41+
// async - returns an observable/promise.
42+
saveCmd = commandAsync(() => Observable.timer(2000), this.isValid$);
4643
```
4744

4845
## Command Attribute (Directive)
@@ -135,12 +132,12 @@ In order to use with `NgForm` easily, you can use the following utility method.
135132
This will make canExecute respond to `form.valid` and for `form.dirty` - also can optionally disable validity or dirty.
136133

137134
```ts
138-
import { CommandAsync, canExecuteFromNgForm } from "@ssv/ngx.command";
135+
import { commandAsync, canExecuteFromNgForm } from "@ssv/ngx.command";
139136

140-
loginCmd = new CommandAsync(x => this.login(), canExecuteFromNgForm(this.form));
137+
loginCmd = commandAsync(x => this.login(), canExecuteFromNgForm(this.form));
141138

142139
// options - disable dirty check
143-
loginCmd = new CommandAsync(x => this.login(), canExecuteFromNgForm(this.form, {
140+
loginCmd = commandAsync(x => this.login(), canExecuteFromNgForm(this.form, {
144141
dirty: false
145142
}));
146143

libs/ngx.command/src/command.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,29 @@ export type CanExecute = Signal<boolean> | Observable<boolean>;
1414
/** Creates an async {@link Command}. Must be used within an injection context.
1515
* NOTE: this auto injects `DestroyRef` and handles auto destroy. {@link ICommand.autoDestroy} should not be used.
1616
*/
17-
export function createCommandAsync(
17+
export function commandAsync(
1818
execute: ExecuteAsyncFn,
1919
canExecute$?: CanExecute,
2020
): Command {
21-
return createCommand(execute, canExecute$, true);
21+
return command(execute, canExecute$, true);
2222
}
2323

2424
/** Creates a {@link Command}. Must be used within an injection context.
2525
* NOTE: this auto injects `DestroyRef` and handles auto destroy. {@link ICommand.autoDestroy} should not be used.
2626
*/
27-
export function createCommand(
27+
export function command(
2828
execute: ExecuteFn,
2929
canExecute$?: CanExecute,
3030
isAsync?: boolean,
3131
): Command {
32+
// todo: add injector/destroyRef to the command (needs overload)
3233
const destroyRef = inject(DestroyRef);
3334

3435
const cmd = new Command(execute, canExecute$, isAsync);
3536
cmd.autoDestroy = false;
3637

3738
destroyRef.onDestroy(() => {
38-
// console.warn("[createCommandAsync::destroy]");
39+
// console.warn("[command::destroy]");
3940
cmd.destroy();
4041
});
4142
return cmd;

0 commit comments

Comments
 (0)