@ssv/ngx.command 0.0.0-PLACEHOLDER
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +243 -0
- package/eslint.config.js +43 -0
- package/jest.config.ts +21 -0
- package/ng-package.json +7 -0
- package/package.json +30 -0
- package/project.json +36 -0
- package/src/command-ref.directive.ts +57 -0
- package/src/command.directive.ts +191 -0
- package/src/command.directive.xspec.ts +105 -0
- package/src/command.model.ts +36 -0
- package/src/command.options.ts +45 -0
- package/src/command.spec.ts +215 -0
- package/src/command.ts +170 -0
- package/src/command.util.ts +50 -0
- package/src/index.ts +8 -0
- package/src/module.ts +17 -0
- package/src/test-setup.ts +8 -0
- package/src/version.ts +1 -0
- package/tsconfig.json +28 -0
- package/tsconfig.lib.json +17 -0
- package/tsconfig.lib.prod.json +9 -0
- package/tsconfig.spec.json +16 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// import {Observable} from "rxjs/Observable";
|
|
2
|
+
// import {inject, addProviders, async} from "@angular/core/testing";
|
|
3
|
+
// import {TestComponentBuilder, ComponentFixture} from "@angular/compiler/testing";
|
|
4
|
+
// import {Component, provide} from "@angular/core";
|
|
5
|
+
|
|
6
|
+
// import {Command, ICommand, CommandDirective} from "./index";
|
|
7
|
+
|
|
8
|
+
// @Component({
|
|
9
|
+
// template: `<button class="btn" [ssvCommand]="saveCmd" >Save</button>`,
|
|
10
|
+
// directives: [
|
|
11
|
+
// CommandDirective
|
|
12
|
+
// ]
|
|
13
|
+
// })
|
|
14
|
+
// class TestContainer {
|
|
15
|
+
// saveCmd = new Command(() => Observable.of("yey").delay(2000), null, true);
|
|
16
|
+
// }
|
|
17
|
+
|
|
18
|
+
// @Component({
|
|
19
|
+
// template: `<button class="btn" [ssvCommand]="emptyCmd" >Save</button>`,
|
|
20
|
+
// directives: [
|
|
21
|
+
// CommandDirective
|
|
22
|
+
// ]
|
|
23
|
+
// })
|
|
24
|
+
// class NullCommandTestContainer {
|
|
25
|
+
|
|
26
|
+
// }
|
|
27
|
+
|
|
28
|
+
// describe("CommandDirectiveSpecs", () => {
|
|
29
|
+
|
|
30
|
+
// let testComponentBuilder: TestComponentBuilder;
|
|
31
|
+
|
|
32
|
+
// beforeEach(inject([
|
|
33
|
+
// TestComponentBuilder
|
|
34
|
+
// ], (
|
|
35
|
+
// _testComponentBuilder: TestComponentBuilder
|
|
36
|
+
// ) => {
|
|
37
|
+
// testComponentBuilder = _testComponentBuilder;
|
|
38
|
+
// })
|
|
39
|
+
// );
|
|
40
|
+
|
|
41
|
+
// describe("given an undefined command", () => {
|
|
42
|
+
// xit("should throw error", async(() => {
|
|
43
|
+
// testComponentBuilder
|
|
44
|
+
// .createAsync(NullCommandTestContainer)
|
|
45
|
+
// .then((fixture: ComponentFixture<NullCommandTestContainer>) => {
|
|
46
|
+
// expect(() => {
|
|
47
|
+
// fixture.detectChanges();
|
|
48
|
+
// }).toThrowError("[commandDirective] command should be defined!");
|
|
49
|
+
// });
|
|
50
|
+
|
|
51
|
+
// }));
|
|
52
|
+
// });
|
|
53
|
+
|
|
54
|
+
// describe("given command isExecuting is set to true", () => {
|
|
55
|
+
// xit("should have element disabled", async(() => {
|
|
56
|
+
// testComponentBuilder
|
|
57
|
+
// .createAsync(TestContainer)
|
|
58
|
+
// .then((fixture: ComponentFixture<TestContainer>) => {
|
|
59
|
+
// fixture.detectChanges();
|
|
60
|
+
// let container = fixture.componentInstance as TestContainer;
|
|
61
|
+
// let element = fixture.debugElement.nativeElement.querySelector("button");
|
|
62
|
+
// element.click();
|
|
63
|
+
// fixture.detectChanges();
|
|
64
|
+
// expect(element).toBeDisabled();
|
|
65
|
+
// });
|
|
66
|
+
// }));
|
|
67
|
+
|
|
68
|
+
// xit("should have executingCssClass added", () => {
|
|
69
|
+
|
|
70
|
+
// });
|
|
71
|
+
|
|
72
|
+
// xdescribe("when isExecuting has been finished", () => {
|
|
73
|
+
// xit("should have element enabled", () => {
|
|
74
|
+
|
|
75
|
+
// });
|
|
76
|
+
|
|
77
|
+
// xit("should have executingCssClass removed", () => {
|
|
78
|
+
|
|
79
|
+
// });
|
|
80
|
+
// });
|
|
81
|
+
// });
|
|
82
|
+
|
|
83
|
+
// xdescribe("given command canExecute is set to true", () => {
|
|
84
|
+
// xit("should have element enabled", () => {
|
|
85
|
+
|
|
86
|
+
// });
|
|
87
|
+
// });
|
|
88
|
+
|
|
89
|
+
// xdescribe("given command canExecute is set to false", () => {
|
|
90
|
+
// xit("should have element disabled", () => {
|
|
91
|
+
|
|
92
|
+
// });
|
|
93
|
+
// });
|
|
94
|
+
|
|
95
|
+
// xdescribe("given commandOptions are provided", () => {
|
|
96
|
+
// xdescribe("when isExecuting is set true", () => {
|
|
97
|
+
// xit("should have executingCssClass added with the provided option", () => {
|
|
98
|
+
|
|
99
|
+
// });
|
|
100
|
+
// });
|
|
101
|
+
|
|
102
|
+
// });
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
// });
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Observable } from "rxjs";
|
|
2
|
+
|
|
3
|
+
export interface ICommand {
|
|
4
|
+
/** Determines whether the command is currently executing, as a snapshot value. */
|
|
5
|
+
readonly isExecuting: boolean;
|
|
6
|
+
/** Determines whether the command is currently executing, as an observable. */
|
|
7
|
+
readonly isExecuting$: Observable<boolean>;
|
|
8
|
+
/** Determines whether the command can execute or not, as a snapshot value. */
|
|
9
|
+
readonly canExecute: boolean;
|
|
10
|
+
/** Determines whether the command can execute or not, as an observable. */
|
|
11
|
+
readonly canExecute$: Observable<boolean>;
|
|
12
|
+
|
|
13
|
+
/** Determines whether to auto destroy when having 0 subscribers (defaults to `true`). */
|
|
14
|
+
autoDestroy: boolean;
|
|
15
|
+
|
|
16
|
+
/** Execute function to invoke. */
|
|
17
|
+
execute(...args: unknown[]): void;
|
|
18
|
+
|
|
19
|
+
/** Disposes all resources held by subscriptions. */
|
|
20
|
+
destroy(): void;
|
|
21
|
+
|
|
22
|
+
/** Subscribe listener, in order to handle auto disposing. */
|
|
23
|
+
subscribe(): void;
|
|
24
|
+
|
|
25
|
+
/** Unsubscribe listener, in order to handle auto disposing. */
|
|
26
|
+
unsubscribe(): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface CommandCreator {
|
|
30
|
+
execute: (...args: unknown[]) => Observable<unknown> | Promise<unknown> | void;
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
32
|
+
canExecute?: Observable<boolean> | Function;
|
|
33
|
+
params?: unknown | unknown[];
|
|
34
|
+
isAsync?: boolean;
|
|
35
|
+
host: unknown;
|
|
36
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { EnvironmentProviders, InjectionToken, makeEnvironmentProviders } from "@angular/core";
|
|
2
|
+
|
|
3
|
+
export interface CommandOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Css Class which gets added/removed on the Command element's host while Command `isExecuting$`.
|
|
6
|
+
*/
|
|
7
|
+
executingCssClass: string;
|
|
8
|
+
|
|
9
|
+
/** Determines whether the disabled will be handled by the directive or not.
|
|
10
|
+
* Disable handled by directive's doesn't always play nice when used with other component/pipe/directive and they also handle disabled.
|
|
11
|
+
* This disables the handling manually and need to pass explicitly `[disabled]="!saveCmd.canExecute"`.
|
|
12
|
+
*/
|
|
13
|
+
handleDisabled: boolean;
|
|
14
|
+
|
|
15
|
+
/** Determine whether to set a `delay(1)` when setting the disabled. Which might be needed when working with external
|
|
16
|
+
* components/directives (such as material button)
|
|
17
|
+
*/
|
|
18
|
+
hasDisabledDelay: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const DEFAULT_OPTIONS = Object.freeze<CommandOptions>({
|
|
22
|
+
executingCssClass: "executing",
|
|
23
|
+
handleDisabled: true,
|
|
24
|
+
hasDisabledDelay: false,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export const COMMAND_OPTIONS = new InjectionToken<CommandOptions>("SSV_COMMAND_OPTIONS", {
|
|
28
|
+
factory: () => DEFAULT_OPTIONS,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export function provideSsvCommandOptions(options: CommandOptions | (() => CommandOptions)): EnvironmentProviders {
|
|
32
|
+
let opts = typeof options === "function" ? options() : options;
|
|
33
|
+
opts = opts
|
|
34
|
+
? {
|
|
35
|
+
...DEFAULT_OPTIONS,
|
|
36
|
+
...opts,
|
|
37
|
+
}
|
|
38
|
+
: DEFAULT_OPTIONS;
|
|
39
|
+
return makeEnvironmentProviders([
|
|
40
|
+
{
|
|
41
|
+
provide: DEFAULT_OPTIONS,
|
|
42
|
+
useValue: opts,
|
|
43
|
+
},
|
|
44
|
+
]);
|
|
45
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { BehaviorSubject, EMPTY } from "rxjs";
|
|
2
|
+
|
|
3
|
+
import { Command } from "./command";
|
|
4
|
+
|
|
5
|
+
describe("CommandSpecs", () => {
|
|
6
|
+
let SUT: Command;
|
|
7
|
+
let executeFn: jest.Mock<void, unknown[], unknown> ;
|
|
8
|
+
// let executeSpyFn: jest.SpyInstance<void, unknown[], unknown>;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
executeFn = jest.fn();
|
|
12
|
+
// executeSpyFn = executeFn;
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe("given a command without canExecute$ param", () => {
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
SUT = new Command(executeFn);
|
|
18
|
+
// executeSpyFn = jest.spyOn(SUT, "execute");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe("when command is initialized", () => {
|
|
22
|
+
it("should have canExecute set to true", () => {
|
|
23
|
+
expect(SUT.canExecute).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should have canExecute$ set to true", done => {
|
|
27
|
+
SUT.canExecute$.subscribe(x => {
|
|
28
|
+
expect(x).toBe(true);
|
|
29
|
+
done();
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe("when execute is invoked", () => {
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
SUT.execute();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should have isExecuting set to false after execute finishes", () => {
|
|
40
|
+
expect(SUT.isExecuting).toBe(false);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should have canExecute set to true after execute finishes", () => {
|
|
44
|
+
expect(SUT.canExecute).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("should invoke execute function", () => {
|
|
48
|
+
expect(executeFn).toHaveBeenCalledTimes(1);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe("given execute is invoked", () => {
|
|
54
|
+
describe("when canExecute is true", () => {
|
|
55
|
+
beforeEach(() => {
|
|
56
|
+
const isInitialValid = true;
|
|
57
|
+
SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should invoke execute function passed", () => {
|
|
61
|
+
SUT.execute();
|
|
62
|
+
expect(executeFn).toHaveBeenCalledTimes(1);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe("when observable completes", () => {
|
|
67
|
+
beforeEach(() => {
|
|
68
|
+
const isInitialValid = true;
|
|
69
|
+
executeFn = jest.fn().mockImplementation(() => EMPTY);
|
|
70
|
+
SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("should invoke multiple times", () => {
|
|
74
|
+
SUT.execute();
|
|
75
|
+
SUT.execute();
|
|
76
|
+
expect(SUT.isExecuting).toBeFalsy();
|
|
77
|
+
expect(executeFn).toHaveBeenCalledTimes(2);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe("when an error is thrown", () => {
|
|
82
|
+
const _errorFn = console.error;
|
|
83
|
+
beforeAll(() => {
|
|
84
|
+
console.error = jest.fn();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
beforeEach(() => {
|
|
88
|
+
const isInitialValid = true;
|
|
89
|
+
executeFn = jest.fn().mockImplementation(() => {
|
|
90
|
+
throw new Error("Execution failed!");
|
|
91
|
+
});
|
|
92
|
+
SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("should invoke multiple times", () => {
|
|
96
|
+
SUT.execute();
|
|
97
|
+
SUT.execute();
|
|
98
|
+
expect(SUT.isExecuting).toBeFalsy();
|
|
99
|
+
expect(executeFn).toHaveBeenCalledTimes(2);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
afterAll(() => {
|
|
103
|
+
console.error = _errorFn;
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe("when args are passed", () => {
|
|
108
|
+
beforeEach(() => {
|
|
109
|
+
const isInitialValid = true;
|
|
110
|
+
SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("and has 1 param should receive 1 arg", () => {
|
|
114
|
+
const args = { name: "rexxar" };
|
|
115
|
+
SUT.execute(args);
|
|
116
|
+
expect(executeFn).toHaveBeenCalledTimes(1);
|
|
117
|
+
expect(executeFn).toHaveBeenCalledWith(args);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("and is array param should not spread", () => {
|
|
121
|
+
const hero = { name: "rexxar" };
|
|
122
|
+
const args = [hero, "yello"];
|
|
123
|
+
SUT.execute(args);
|
|
124
|
+
expect(executeFn).toHaveBeenCalledTimes(1);
|
|
125
|
+
expect(executeFn).toHaveBeenCalledWith([hero, "yello"]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("and multi args are pass should receive all", () => {
|
|
129
|
+
const hero = { name: "rexxar" };
|
|
130
|
+
SUT.execute(hero, "yello");
|
|
131
|
+
expect(executeFn).toHaveBeenCalledTimes(1);
|
|
132
|
+
expect(executeFn).toHaveBeenCalledWith(hero, "yello");
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe("when canExecute is false", () => {
|
|
137
|
+
beforeEach(() => {
|
|
138
|
+
const isInitialValid = false;
|
|
139
|
+
SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("should not execute the provided execute function", () => {
|
|
143
|
+
SUT.execute();
|
|
144
|
+
expect(executeFn).not.toHaveBeenCalled();
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
describe("given canExecute with an initial value of true", () => {
|
|
150
|
+
let canExecute$: BehaviorSubject<boolean>;
|
|
151
|
+
|
|
152
|
+
beforeEach(() => {
|
|
153
|
+
const isInitialValid = true;
|
|
154
|
+
canExecute$ = new BehaviorSubject<boolean>(isInitialValid);
|
|
155
|
+
SUT = new Command(executeFn, canExecute$);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("should have canExecute set to true", () => {
|
|
159
|
+
expect(SUT.canExecute).toBe(true);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("should have canExecute$ set to true", done => {
|
|
163
|
+
SUT.canExecute$.subscribe(x => {
|
|
164
|
+
expect(x).toBe(true);
|
|
165
|
+
done();
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe("when the canExecute observable changes", () => {
|
|
170
|
+
beforeEach(() => {
|
|
171
|
+
canExecute$.next(false);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("should update canExecute", () => {
|
|
175
|
+
expect(SUT.canExecute).toBe(false);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("should update canExecute$", done => {
|
|
179
|
+
SUT.canExecute$.subscribe(x => {
|
|
180
|
+
expect(x).toBe(false);
|
|
181
|
+
done();
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe("given canExecute with an initial value of false", () => {
|
|
188
|
+
beforeEach(() => {
|
|
189
|
+
const isInitialValid = false;
|
|
190
|
+
SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("should have canExecute set to false", () => {
|
|
194
|
+
expect(SUT.canExecute).toBe(false);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("should have canExecute$ set to false", done => {
|
|
198
|
+
SUT.canExecute$.subscribe(x => {
|
|
199
|
+
expect(x).toBe(false);
|
|
200
|
+
done();
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe("given destroy is invoked", () => {
|
|
206
|
+
beforeEach(() => {
|
|
207
|
+
const isInitialValid = false;
|
|
208
|
+
SUT = new Command(executeFn, new BehaviorSubject<boolean>(isInitialValid));
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("should destroy successfully", () => {
|
|
212
|
+
SUT.destroy();
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
});
|
package/src/command.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { Observable, combineLatest, Subscription, Subject, BehaviorSubject, of, EMPTY } from "rxjs";
|
|
3
|
+
import { tap, map, filter, switchMap, catchError, finalize, take } from "rxjs/operators";
|
|
4
|
+
import { ICommand } from "./command.model";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Command object used to encapsulate information which is needed to perform an action.
|
|
8
|
+
*/
|
|
9
|
+
export class Command implements ICommand {
|
|
10
|
+
|
|
11
|
+
/** Determines whether the command is currently executing, as a snapshot value. */
|
|
12
|
+
get isExecuting(): boolean {
|
|
13
|
+
return this._isExecuting;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Determines whether the command can execute or not, as a snapshot value. */
|
|
17
|
+
get canExecute(): boolean {
|
|
18
|
+
return this._canExecute;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Determines whether the command is currently executing, as an observable. */
|
|
22
|
+
get isExecuting$(): Observable<boolean> {
|
|
23
|
+
return this._isExecuting$.asObservable();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Determines whether to auto destroy when having 0 subscribers. */
|
|
27
|
+
autoDestroy = true;
|
|
28
|
+
|
|
29
|
+
/** Determines whether the command can execute or not, as an observable. */
|
|
30
|
+
readonly canExecute$: Observable<boolean>;
|
|
31
|
+
|
|
32
|
+
private _isExecuting$ = new BehaviorSubject<boolean>(false);
|
|
33
|
+
private _isExecuting = false;
|
|
34
|
+
private _canExecute = true;
|
|
35
|
+
private executionPipe$ = new Subject<unknown[] | undefined>();
|
|
36
|
+
private isExecuting$$ = Subscription.EMPTY;
|
|
37
|
+
private canExecute$$ = Subscription.EMPTY;
|
|
38
|
+
private executionPipe$$ = Subscription.EMPTY;
|
|
39
|
+
private subscribersCount = 0;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Creates an instance of Command.
|
|
43
|
+
*
|
|
44
|
+
* @param execute Execute function to invoke - use `isAsync: true` when `Observable<any>`.
|
|
45
|
+
* @param canExecute Observable which determines whether it can execute or not.
|
|
46
|
+
* @param isAsync Indicates that the execute function is async e.g. Observable.
|
|
47
|
+
*/
|
|
48
|
+
constructor(
|
|
49
|
+
execute: (...args: unknown[]) => unknown,
|
|
50
|
+
canExecute$?: Observable<boolean>,
|
|
51
|
+
isAsync?: boolean,
|
|
52
|
+
) {
|
|
53
|
+
if (canExecute$) {
|
|
54
|
+
this.canExecute$ = combineLatest([
|
|
55
|
+
this._isExecuting$,
|
|
56
|
+
canExecute$
|
|
57
|
+
]).pipe(
|
|
58
|
+
map(([isExecuting, canExecuteResult]) => {
|
|
59
|
+
// console.log("[command::combineLatest$] update!", { isExecuting, canExecuteResult });
|
|
60
|
+
this._isExecuting = isExecuting;
|
|
61
|
+
this._canExecute = !isExecuting && !!canExecuteResult;
|
|
62
|
+
return this._canExecute;
|
|
63
|
+
}),
|
|
64
|
+
);
|
|
65
|
+
this.canExecute$$ = this.canExecute$.subscribe();
|
|
66
|
+
} else {
|
|
67
|
+
this.canExecute$ = this._isExecuting$.pipe(
|
|
68
|
+
map(x => {
|
|
69
|
+
const canExecute = !x;
|
|
70
|
+
this._canExecute = canExecute;
|
|
71
|
+
return canExecute;
|
|
72
|
+
})
|
|
73
|
+
);
|
|
74
|
+
this.isExecuting$$ = this._isExecuting$
|
|
75
|
+
.pipe(tap(x => this._isExecuting = x))
|
|
76
|
+
.subscribe();
|
|
77
|
+
}
|
|
78
|
+
this.executionPipe$$ = this.buildExecutionPipe(execute, isAsync).subscribe();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Execute function to invoke. */
|
|
82
|
+
execute(...args: unknown[]): void {
|
|
83
|
+
// console.warn("[command::execute]", args);
|
|
84
|
+
this.executionPipe$.next(args);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Disposes all resources held by subscriptions. */
|
|
88
|
+
destroy(): void {
|
|
89
|
+
// console.warn("[command::destroy]");
|
|
90
|
+
this.executionPipe$$.unsubscribe();
|
|
91
|
+
this.canExecute$$.unsubscribe();
|
|
92
|
+
this.isExecuting$$.unsubscribe();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
subscribe(): void {
|
|
96
|
+
this.subscribersCount++;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
unsubscribe(): void {
|
|
100
|
+
this.subscribersCount--;
|
|
101
|
+
// console.log("[command::unsubscribe]", { autoDestroy: this.autoDestroy, subscribersCount: this.subscribersCount });
|
|
102
|
+
if (this.autoDestroy && this.subscribersCount <= 0) {
|
|
103
|
+
this.destroy();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private buildExecutionPipe(execute: (...args: unknown[]) => any, isAsync?: boolean): Observable<unknown> {
|
|
108
|
+
let pipe$ = this.executionPipe$.pipe(
|
|
109
|
+
// tap(x => console.warn(">>>> executionPipe", this._canExecute)),
|
|
110
|
+
filter(() => this._canExecute),
|
|
111
|
+
tap(() => {
|
|
112
|
+
// console.log("[command::executionPipe$] do#1 - set execute", { args: x });
|
|
113
|
+
this._isExecuting$.next(true);
|
|
114
|
+
})
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
const execFn = isAsync
|
|
118
|
+
? switchMap<unknown[] | undefined, any[]>(args => {
|
|
119
|
+
if (args) {
|
|
120
|
+
return execute(...args);
|
|
121
|
+
}
|
|
122
|
+
return execute();
|
|
123
|
+
})
|
|
124
|
+
: tap((args: unknown[] | undefined) => {
|
|
125
|
+
if (args) {
|
|
126
|
+
execute(...args);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
execute();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
pipe$ = pipe$.pipe(
|
|
133
|
+
switchMap(args => of(args).pipe(
|
|
134
|
+
execFn,
|
|
135
|
+
finalize(() => {
|
|
136
|
+
// console.log("[command::executionPipe$] finalize inner#1 - set idle");
|
|
137
|
+
this._isExecuting$.next(false);
|
|
138
|
+
}),
|
|
139
|
+
take(1),
|
|
140
|
+
catchError(error => {
|
|
141
|
+
console.error("Unhandled execute error", error);
|
|
142
|
+
return EMPTY;
|
|
143
|
+
}),
|
|
144
|
+
)),
|
|
145
|
+
tap(
|
|
146
|
+
() => {
|
|
147
|
+
// console.log("[command::executionPipe$] tap#2 - set idle");
|
|
148
|
+
this._isExecuting$.next(false);
|
|
149
|
+
},
|
|
150
|
+
)
|
|
151
|
+
);
|
|
152
|
+
return pipe$;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Async Command object used to encapsulate information which is needed to perform an action,
|
|
159
|
+
* which takes an execute function as Observable/Promise.
|
|
160
|
+
*/
|
|
161
|
+
export class CommandAsync extends Command {
|
|
162
|
+
|
|
163
|
+
constructor(
|
|
164
|
+
execute: (...args: any[]) => Observable<unknown> | Promise<unknown>,
|
|
165
|
+
canExecute$?: Observable<boolean>,
|
|
166
|
+
) {
|
|
167
|
+
super(execute, canExecute$, true);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { AbstractControl, AbstractControlDirective, FormControlStatus } from "@angular/forms";
|
|
2
|
+
import { Observable, of } from "rxjs";
|
|
3
|
+
import { map, distinctUntilChanged, startWith, delay } from "rxjs/operators";
|
|
4
|
+
|
|
5
|
+
import { CommandCreator, ICommand } from "./command.model";
|
|
6
|
+
import { Command } from "./command";
|
|
7
|
+
|
|
8
|
+
/** Determines whether the arg object is of type `Command`. */
|
|
9
|
+
export function isCommand(arg: unknown): arg is ICommand {
|
|
10
|
+
return arg instanceof Command;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Determines whether the arg object is of type `CommandCreator`. */
|
|
14
|
+
export function isCommandCreator(arg: unknown): arg is CommandCreator {
|
|
15
|
+
if (arg instanceof Command) {
|
|
16
|
+
return false;
|
|
17
|
+
} else if (isAssumedType<CommandCreator>(arg) && arg.execute && arg.host) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CanExecuteFormOptions {
|
|
24
|
+
/** Determines whether to check for validity. (defaults: true) */
|
|
25
|
+
validity?: boolean;
|
|
26
|
+
|
|
27
|
+
/** Determines whether to check whether UI has been touched. (defaults: true) */
|
|
28
|
+
dirty?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Get form is valid as an observable. */
|
|
32
|
+
export function canExecuteFromNgForm(
|
|
33
|
+
form: AbstractControl | AbstractControlDirective,
|
|
34
|
+
options?: CanExecuteFormOptions
|
|
35
|
+
): Observable<boolean> {
|
|
36
|
+
const opts: CanExecuteFormOptions = { validity: true, dirty: true, ...options };
|
|
37
|
+
|
|
38
|
+
return form.statusChanges
|
|
39
|
+
? (form.statusChanges as Observable<FormControlStatus>).pipe( // todo: remove cast when working correctly
|
|
40
|
+
delay(0),
|
|
41
|
+
startWith(form.valid),
|
|
42
|
+
map(() => !!(!opts.validity || form.valid) && !!(!opts.dirty || form.dirty)),
|
|
43
|
+
distinctUntilChanged(),
|
|
44
|
+
)
|
|
45
|
+
: of(true);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isAssumedType<T = Record<string, unknown>>(x: unknown): x is Partial<T> {
|
|
49
|
+
return x !== null && typeof x === "object";
|
|
50
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./command";
|
|
2
|
+
export * from "./command.directive";
|
|
3
|
+
export * from "./command-ref.directive";
|
|
4
|
+
export * from "./command.util";
|
|
5
|
+
export { CommandCreator, ICommand } from "./command.model";
|
|
6
|
+
export * from "./command.options";
|
|
7
|
+
export * from "./module";
|
|
8
|
+
export * from "./version";
|
package/src/module.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { NgModule } from "@angular/core";
|
|
2
|
+
|
|
3
|
+
import { CommandDirective } from "./command.directive";
|
|
4
|
+
import { CommandRefDirective } from "./command-ref.directive";
|
|
5
|
+
|
|
6
|
+
const EXPORTED_IMPORTS = [
|
|
7
|
+
CommandDirective,
|
|
8
|
+
CommandRefDirective
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
@NgModule({
|
|
12
|
+
imports: [EXPORTED_IMPORTS],
|
|
13
|
+
exports: [EXPORTED_IMPORTS]
|
|
14
|
+
})
|
|
15
|
+
export class SsvCommandModule {
|
|
16
|
+
|
|
17
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// @ts-expect-error https://thymikee.github.io/jest-preset-angular/docs/getting-started/test-environment
|
|
2
|
+
globalThis.ngJest = {
|
|
3
|
+
testEnvironmentOptions: {
|
|
4
|
+
errorOnUnknownElements: true,
|
|
5
|
+
errorOnUnknownProperties: true,
|
|
6
|
+
},
|
|
7
|
+
};
|
|
8
|
+
import 'jest-preset-angular/setup-jest';
|
package/src/version.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const VERSION = "0.0.0-PLACEHOLDER";
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2022",
|
|
4
|
+
"forceConsistentCasingInFileNames": true,
|
|
5
|
+
"strict": true,
|
|
6
|
+
"noImplicitOverride": true,
|
|
7
|
+
"noPropertyAccessFromIndexSignature": true,
|
|
8
|
+
"noImplicitReturns": true,
|
|
9
|
+
"noFallthroughCasesInSwitch": true
|
|
10
|
+
},
|
|
11
|
+
"files": [],
|
|
12
|
+
"include": [],
|
|
13
|
+
"references": [
|
|
14
|
+
{
|
|
15
|
+
"path": "./tsconfig.lib.json"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"path": "./tsconfig.spec.json"
|
|
19
|
+
}
|
|
20
|
+
],
|
|
21
|
+
"extends": "../../tsconfig.base.json",
|
|
22
|
+
"angularCompilerOptions": {
|
|
23
|
+
"enableI18nLegacyMessageIdFormat": false,
|
|
24
|
+
"strictInjectionParameters": true,
|
|
25
|
+
"strictInputAccessModifiers": true,
|
|
26
|
+
"strictTemplates": true
|
|
27
|
+
}
|
|
28
|
+
}
|