@zenflux/commands 0.0.0-stage → 0.0.1
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/LICENSE +674 -0
- package/package.json +22 -3
- package/readme.md +40 -0
- package/src/api.ts +158 -0
- package/src/core/commands-internal.ts +11 -0
- package/src/core/commands.ts +674 -0
- package/src/core/components.ts +103 -0
- package/src/core/hooks/base.ts +444 -0
- package/src/core/hooks/data.ts +54 -0
- package/src/core/hooks/ui.ts +40 -0
- package/src/core/hooks.ts +257 -0
- package/src/core/routes.ts +243 -0
- package/src/core/shortcuts.ts +351 -0
- package/src/core/store.ts +155 -0
- package/src/env.d.ts +15 -0
- package/src/extras/dev-tools.ts +427 -0
- package/src/index.ts +83 -0
- package/src/instance.ts +30 -0
- package/src/modules/args-object.ts +113 -0
- package/src/modules/command-base.ts +51 -0
- package/src/modules/command-callback-base.ts +42 -0
- package/src/modules/command-infra.ts +230 -0
- package/src/modules/command-internal-base.ts +19 -0
- package/src/modules/component-base.ts +479 -0
- package/src/modules/hook-base.ts +149 -0
- package/src/modules/hook-break.ts +8 -0
- package/src/modules/hooks/data/after.ts +15 -0
- package/src/modules/hooks/data/base.ts +11 -0
- package/src/modules/hooks/data/catch.ts +12 -0
- package/src/modules/hooks/data/dependency.ts +12 -0
- package/src/modules/hooks/data/index.ts +6 -0
- package/src/modules/hooks/ui/after.ts +14 -0
- package/src/modules/hooks/ui/base.ts +11 -0
- package/src/modules/hooks/ui/before.ts +12 -0
- package/src/modules/hooks/ui/catch.ts +12 -0
- package/src/modules/hooks/ui/index.ts +6 -0
- package/src/modules/instance-type.ts +114 -0
- package/src/modules/module.ts +216 -0
- package/src/types.ts +194 -0
- package/src/utils/console.ts +19 -0
- package/src/utils/instances-slice.ts +123 -0
- package/src/utils/object.ts +162 -0
- package/src/utils/value-store.ts +145 -0
- package/README.md +0 -3
|
@@ -0,0 +1,674 @@
|
|
|
1
|
+
import CommandBase from "@zenflux/commands/src/modules/command-base";
|
|
2
|
+
import Console from "@zenflux/commands/src/utils/console";
|
|
3
|
+
import HookBreak from "@zenflux/commands/src/modules/hook-break";
|
|
4
|
+
import Module from "@zenflux/commands/src/modules/module";
|
|
5
|
+
|
|
6
|
+
import { $e } from "@zenflux/commands/src/instance";
|
|
7
|
+
import { isCommandClass } from "@zenflux/commands/src/modules/command-infra";
|
|
8
|
+
|
|
9
|
+
import type { ModuleEvents } from "@zenflux/commands/src/modules/module";
|
|
10
|
+
import type { CommandClass, RegisteredCallback, RunnableCommand } from "@zenflux/commands/src/modules/command-infra";
|
|
11
|
+
import type ComponentBase from "@zenflux/commands/src/modules/component-base";
|
|
12
|
+
import type { CommandArgs } from "@zenflux/commands/src/types";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* What a commands system keeps by name: a command class, or a function (a route's).
|
|
16
|
+
*/
|
|
17
|
+
export type CommandEntry = CommandClass | RegisteredCallback;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* What a run by name gives: the command's result as the system passes it on (for an async command, a promise of it
|
|
21
|
+
* that resolves once its 'after' hooks are done), or false when it did not run. Typed by name: `$e.run()`.
|
|
22
|
+
*/
|
|
23
|
+
export type CommandsRunResult = void | Promise<void> | false;
|
|
24
|
+
|
|
25
|
+
export interface CommandsEvents extends ModuleEvents {
|
|
26
|
+
/**
|
|
27
|
+
* A command is about to run.
|
|
28
|
+
*/
|
|
29
|
+
"run:before": ( component: ComponentBase, command: string, args: CommandArgs ) => void;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A command ran: its result, or the error it failed with.
|
|
33
|
+
*/
|
|
34
|
+
"run:after": <TResult>( component: ComponentBase, command: string, args: CommandArgs, result: TResult ) => void;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A command failed: what it threw or rejected with, or an error it handled itself and reported (`catchApply()`).
|
|
38
|
+
*/
|
|
39
|
+
"run:catch": <TError>( component: ComponentBase, command: string, args: CommandArgs, error: TError ) => void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @param value
|
|
44
|
+
*
|
|
45
|
+
* @return whether the value is a promise (an async command's result)
|
|
46
|
+
*/
|
|
47
|
+
const isPromise = <TValue>( value: TValue ): value is TValue & Promise<Awaited<TValue>> => value instanceof Promise;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param namespace A component's.
|
|
51
|
+
* @param name A command's or route's full name.
|
|
52
|
+
*
|
|
53
|
+
* @return whether the name is one of the component's: '<namespace>/<name>', not one of a component inside it
|
|
54
|
+
* ('editor/elements/select' is the 'editor/elements' component's, not the editor's). As OwnNames (types.ts).
|
|
55
|
+
*/
|
|
56
|
+
const isOwnName = ( namespace: string, name: string ): boolean => {
|
|
57
|
+
const own = name.startsWith( namespace + "/" ) ? name.slice( namespace.length + 1 ) : "";
|
|
58
|
+
|
|
59
|
+
return "" !== own && ! own.includes( "/" );
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The commands systems' base: commands by name ('component/command'), each run with its component's dependency, hooks
|
|
64
|
+
* and trace. `$e.commands`, `$e.commandsInternal` and `$e.routes` are commands systems; each runs by name in its own
|
|
65
|
+
* way (`run()`, `to()`), through `runCommand()`.
|
|
66
|
+
*/
|
|
67
|
+
export class CommandsBase<TEntry extends CommandEntry = CommandEntry> extends Module<CommandsEvents> {
|
|
68
|
+
/**
|
|
69
|
+
* The commands running now, of every kind, first to last.
|
|
70
|
+
*/
|
|
71
|
+
public static trace: string[] = [];
|
|
72
|
+
|
|
73
|
+
public current: { [ container: string ]: string };
|
|
74
|
+
public currentArgs: { [ container: string ]: CommandArgs };
|
|
75
|
+
public currentTrace: string[];
|
|
76
|
+
public commands: { [ command: string ]: TEntry };
|
|
77
|
+
public components: { [ command: string ]: string };
|
|
78
|
+
|
|
79
|
+
public constructor( args?: object ) {
|
|
80
|
+
super( args );
|
|
81
|
+
|
|
82
|
+
this.current = {};
|
|
83
|
+
this.currentArgs = {};
|
|
84
|
+
this.currentTrace = [];
|
|
85
|
+
this.commands = {};
|
|
86
|
+
this.components = {};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @param id
|
|
91
|
+
*
|
|
92
|
+
* @return command class (a route's function)
|
|
93
|
+
*/
|
|
94
|
+
public getCommandClass( id: string ): TEntry | undefined {
|
|
95
|
+
return this.commands[ id ];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Receive all loaded commands.
|
|
100
|
+
*
|
|
101
|
+
* @return commands
|
|
102
|
+
*/
|
|
103
|
+
public getAll(): string[] {
|
|
104
|
+
return Object.keys( this.commands ).sort();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Register new command.
|
|
109
|
+
*
|
|
110
|
+
* @param component Its component, or its namespace.
|
|
111
|
+
* @param command Its full name, in the component's namespace: 'panel/close'.
|
|
112
|
+
* @param callback The command class (a route's function).
|
|
113
|
+
*
|
|
114
|
+
* @return commands
|
|
115
|
+
*/
|
|
116
|
+
public register( component: ComponentBase | string, command: string, callback: TEntry ): this {
|
|
117
|
+
const owner = this.componentOf( component ),
|
|
118
|
+
namespace = owner.getNamespace();
|
|
119
|
+
|
|
120
|
+
if ( ! isOwnName( namespace, command ) ) {
|
|
121
|
+
this.error( `\`${ command }\` is not a name of the '${ namespace }' component's: '${ namespace }/<name>'.` );
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if ( this.commands[ command ] ) {
|
|
125
|
+
this.error( `\`${ command }\` is already registered.` );
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
this.commands[ command ] = callback;
|
|
129
|
+
this.components[ command ] = namespace;
|
|
130
|
+
|
|
131
|
+
const shortcut = owner.getShortcuts()[ command ];
|
|
132
|
+
|
|
133
|
+
if ( shortcut ) {
|
|
134
|
+
const handler = Object.assign( shortcut, {
|
|
135
|
+
command,
|
|
136
|
+
callback: ( event: KeyboardEvent ) => this.runShortcut( command, event ),
|
|
137
|
+
} );
|
|
138
|
+
|
|
139
|
+
$e.shortcuts.register( handler.keys, handler );
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* @param component Its component, or its namespace.
|
|
147
|
+
* @param command Its full name.
|
|
148
|
+
*
|
|
149
|
+
* @return commands
|
|
150
|
+
*/
|
|
151
|
+
public unregister( component: ComponentBase | string, command: string ): this {
|
|
152
|
+
const owner = this.componentOf( component );
|
|
153
|
+
|
|
154
|
+
if ( ! this.commands[ command ] || this.components[ command ] !== owner.getNamespace() ) {
|
|
155
|
+
this.error( `\`${ command }\` not exist.` );
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
delete this.commands[ command ];
|
|
159
|
+
delete this.components[ command ];
|
|
160
|
+
|
|
161
|
+
const shortcut = owner.getShortcuts()[ command ];
|
|
162
|
+
|
|
163
|
+
if ( shortcut ) {
|
|
164
|
+
$e.shortcuts.unregister( shortcut.keys, shortcut );
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return this;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Receive Component of the command.
|
|
172
|
+
*
|
|
173
|
+
* @param command
|
|
174
|
+
*
|
|
175
|
+
* @return component
|
|
176
|
+
*/
|
|
177
|
+
public getComponent( command: string ): ComponentBase | undefined {
|
|
178
|
+
return $e.components.getByNamespace( this.components[ command ] );
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Checks if current running command is the same parameter command.
|
|
183
|
+
*
|
|
184
|
+
* @param command
|
|
185
|
+
*
|
|
186
|
+
* @return is this command the same as the one passed in the arguments
|
|
187
|
+
*/
|
|
188
|
+
public is( command: string ): boolean {
|
|
189
|
+
const component = this.getComponent( command );
|
|
190
|
+
|
|
191
|
+
if ( ! component ) {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return command === this.current[ component.getServiceName() ];
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Checks if parameter command is the first command in trace that currently running.
|
|
200
|
+
*
|
|
201
|
+
* @param command
|
|
202
|
+
*
|
|
203
|
+
* @return is parameter command the first command in trace that currently running
|
|
204
|
+
*/
|
|
205
|
+
public isCurrentFirstTrace( command: string ): boolean {
|
|
206
|
+
return command === this.getCurrentFirstTrace();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Receive currently running components and its commands.
|
|
211
|
+
*
|
|
212
|
+
* @param container
|
|
213
|
+
*
|
|
214
|
+
* @return the container's command (false when none runs); without one, every container's
|
|
215
|
+
*/
|
|
216
|
+
public getCurrent(): { [ container: string ]: string };
|
|
217
|
+
public getCurrent( container: string ): string | false;
|
|
218
|
+
public getCurrent( container = "" ): { [ container: string ]: string } | string | false {
|
|
219
|
+
if ( container ) {
|
|
220
|
+
if ( ! this.current[ container ] ) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return this.current[ container ];
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return this.current;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Receive currently running command args.
|
|
232
|
+
*
|
|
233
|
+
* @param container
|
|
234
|
+
*
|
|
235
|
+
* @return the container's command's args (false when none runs); without one, every container's
|
|
236
|
+
*/
|
|
237
|
+
public getCurrentArgs(): { [ container: string ]: CommandArgs };
|
|
238
|
+
public getCurrentArgs( container: string ): CommandArgs | false;
|
|
239
|
+
public getCurrentArgs( container = "" ): { [ container: string ]: CommandArgs } | CommandArgs | false {
|
|
240
|
+
if ( container ) {
|
|
241
|
+
if ( ! this.currentArgs[ container ] ) {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return this.currentArgs[ container ];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return this.currentArgs;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Receive first command that currently running.
|
|
253
|
+
*
|
|
254
|
+
* @return first running command
|
|
255
|
+
*/
|
|
256
|
+
public getCurrentFirst(): string | undefined {
|
|
257
|
+
return Object.values( this.current )[ 0 ];
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Receive last command that currently running.
|
|
262
|
+
*
|
|
263
|
+
* @return last running command
|
|
264
|
+
*/
|
|
265
|
+
public getCurrentLast(): string | undefined {
|
|
266
|
+
const current = Object.values( this.current );
|
|
267
|
+
|
|
268
|
+
return current[ current.length - 1 ];
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Receive first command in trace that currently running
|
|
273
|
+
*
|
|
274
|
+
* @return first command in trace
|
|
275
|
+
*/
|
|
276
|
+
public getCurrentFirstTrace(): string | undefined {
|
|
277
|
+
return this.currentTrace[ 0 ];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Responsible to add current command to trace and trigger 'run:before' event.
|
|
282
|
+
* Run before command.
|
|
283
|
+
*
|
|
284
|
+
* @param command
|
|
285
|
+
* @param args
|
|
286
|
+
* @param addTrace
|
|
287
|
+
*/
|
|
288
|
+
public beforeRun( command: string, args: CommandArgs = {}, addTrace = true ): void {
|
|
289
|
+
const component = this.requireComponent( command ),
|
|
290
|
+
container = component.getServiceName();
|
|
291
|
+
|
|
292
|
+
if ( addTrace ) {
|
|
293
|
+
this.addCurrentTrace( container, command, args );
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if ( args.onBefore ) {
|
|
297
|
+
args.onBefore.apply( component, [ args ] );
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if ( this.hasListeners( "run:before" ) ) {
|
|
301
|
+
this.trigger( "run:before", component, command, args );
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Responsible to validate if the run is even possible.
|
|
307
|
+
* Runs immediately after entering `run()`.
|
|
308
|
+
*
|
|
309
|
+
* @param command
|
|
310
|
+
* @param args
|
|
311
|
+
*
|
|
312
|
+
* @return dependency result
|
|
313
|
+
*/
|
|
314
|
+
public validateRun( command: string, args: CommandArgs = {} ): boolean {
|
|
315
|
+
if ( ! this.commands[ command ] ) {
|
|
316
|
+
this.error( `\`${ command }\` not found.` );
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return this.requireComponent( command ).dependency( command, args );
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Runs a command, by its name.
|
|
324
|
+
*
|
|
325
|
+
* @param command
|
|
326
|
+
* @param args
|
|
327
|
+
*
|
|
328
|
+
* @return results
|
|
329
|
+
*/
|
|
330
|
+
protected runCommand( command: string, args: CommandArgs = {} ): CommandsRunResult {
|
|
331
|
+
if ( ! this.validateRun( command, args ) ) {
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
this.beforeRun( command, args );
|
|
336
|
+
|
|
337
|
+
// Get command class or callback.
|
|
338
|
+
const context: CommandEntry = this.commands[ command ],
|
|
339
|
+
currentComponent = this.requireComponent( command );
|
|
340
|
+
|
|
341
|
+
// Is simple callback? (e.g. a route)
|
|
342
|
+
if ( ! isCommandClass( context ) ) {
|
|
343
|
+
const results = context.apply( currentComponent, [ args ] );
|
|
344
|
+
|
|
345
|
+
this.afterRun( command, args, results );
|
|
346
|
+
|
|
347
|
+
return results;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
let instance: RunnableCommand;
|
|
351
|
+
|
|
352
|
+
try {
|
|
353
|
+
// The class registered under the name takes the args its name runs with (`$e.run()` checks them against
|
|
354
|
+
// its registry): TS can't follow a class through its name.
|
|
355
|
+
instance = new context( args as never );
|
|
356
|
+
} catch( e ) {
|
|
357
|
+
// Its args did not validate.
|
|
358
|
+
this.removeCurrentTrace( currentComponent );
|
|
359
|
+
|
|
360
|
+
throw e;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
if ( ! this.validateInstanceScope( instance, currentComponent, command ) ) {
|
|
364
|
+
this.removeCurrentTrace( currentComponent );
|
|
365
|
+
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return this.runInstance( instance );
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* @param instance
|
|
374
|
+
*
|
|
375
|
+
* @return results
|
|
376
|
+
*/
|
|
377
|
+
public runInstance( instance: RunnableCommand ): CommandsRunResult {
|
|
378
|
+
let results: void;
|
|
379
|
+
|
|
380
|
+
// For UI Hooks.
|
|
381
|
+
instance.onBeforeRun( instance.args );
|
|
382
|
+
|
|
383
|
+
try {
|
|
384
|
+
// For data hooks.
|
|
385
|
+
instance.onBeforeApply( instance.args );
|
|
386
|
+
|
|
387
|
+
results = instance.run();
|
|
388
|
+
} catch( e ) {
|
|
389
|
+
this.catchApply( e, instance );
|
|
390
|
+
|
|
391
|
+
// A dependency hook stopped the command.
|
|
392
|
+
if ( e instanceof HookBreak ) {
|
|
393
|
+
this.removeCurrentTrace( instance.component );
|
|
394
|
+
|
|
395
|
+
return false;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// The error is the caller's, as when an async command rejects (Elementor's went on to the 'after' hooks
|
|
399
|
+
// with a null result).
|
|
400
|
+
this.afterRun( instance.command, instance.args, e );
|
|
401
|
+
|
|
402
|
+
throw e;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
return this.applyRunAfter( instance, results );
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Responsible for applying everything that need to be run after each command runs.
|
|
410
|
+
* Called on run() after runInstance(), to manipulate results & apply 'after' hooks.
|
|
411
|
+
*
|
|
412
|
+
* @param instance
|
|
413
|
+
* @param result
|
|
414
|
+
*
|
|
415
|
+
* @return result; for a promise, one of its value once the 'after' hooks are done
|
|
416
|
+
*/
|
|
417
|
+
public applyRunAfter<TResult>( instance: RunnableCommand, result: TResult ): TResult | Promise<Awaited<TResult>> {
|
|
418
|
+
if ( isPromise( result ) ) {
|
|
419
|
+
return this.applyRunAfterAsync( instance, result );
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
this.applyRunAfterSync( instance, result );
|
|
423
|
+
|
|
424
|
+
return result;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Responsible to handle simple(synchronous) 'run after' behavior.
|
|
429
|
+
*
|
|
430
|
+
* @param instance
|
|
431
|
+
* @param result
|
|
432
|
+
*/
|
|
433
|
+
public applyRunAfterSync<TResult>( instance: RunnableCommand, result: TResult ): void {
|
|
434
|
+
// Run Data hooks.
|
|
435
|
+
instance.onAfterApply( instance.args, result );
|
|
436
|
+
|
|
437
|
+
// For UI hooks.
|
|
438
|
+
instance.onAfterRun( instance.args, result );
|
|
439
|
+
|
|
440
|
+
this.afterRun( instance.command, instance.args, result );
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Await for promise result.
|
|
445
|
+
*
|
|
446
|
+
* @param instance
|
|
447
|
+
* @param result
|
|
448
|
+
*
|
|
449
|
+
* @return result, once the 'after' hooks are done
|
|
450
|
+
*/
|
|
451
|
+
public applyRunAfterAsync<TResult>( instance: RunnableCommand, result: Promise<TResult> ): Promise<TResult> {
|
|
452
|
+
// One step after the result: its 'after' hooks, and only then the value (or the error, after the catch
|
|
453
|
+
// hooks). Each `await` or wrapping promise more would cost every run a turn of the microtask queue.
|
|
454
|
+
return result.then(
|
|
455
|
+
( value ) => {
|
|
456
|
+
const hooks = this.applyRunAfterAsyncResult( instance, value );
|
|
457
|
+
|
|
458
|
+
return hooks ? hooks.then( () => value ) : value;
|
|
459
|
+
},
|
|
460
|
+
( e ) => {
|
|
461
|
+
this.catchApply( e, instance );
|
|
462
|
+
this.afterRun( instance.command, instance.args, e );
|
|
463
|
+
|
|
464
|
+
throw e;
|
|
465
|
+
},
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Runs the 'after' hooks of a command whose promise resolved, and waits for those that answer with a promise,
|
|
471
|
+
* before the command is released.
|
|
472
|
+
*
|
|
473
|
+
* @param instance
|
|
474
|
+
* @param result
|
|
475
|
+
*
|
|
476
|
+
* @return what to wait for, when a hook answered with a promise
|
|
477
|
+
*/
|
|
478
|
+
public applyRunAfterAsyncResult<TResult>( instance: RunnableCommand, result: TResult ): Promise<void> | void {
|
|
479
|
+
// Run Data hooks.
|
|
480
|
+
const results = instance.onAfterApply( instance.args, result ),
|
|
481
|
+
promises = Array.isArray( results ) ? results.flat().filter( ( filtered ) => filtered instanceof Promise ) : [];
|
|
482
|
+
|
|
483
|
+
if ( promises.length ) {
|
|
484
|
+
// Wait for hooks before return the value.
|
|
485
|
+
return Promise.all( promises ).then( () => this.releaseRunAfter( instance, result ) );
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
this.releaseRunAfter( instance, result );
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* The UI 'after' hooks of a command, and its release.
|
|
493
|
+
*
|
|
494
|
+
* @param instance
|
|
495
|
+
* @param result
|
|
496
|
+
*/
|
|
497
|
+
public releaseRunAfter<TResult>( instance: RunnableCommand, result: TResult ): void {
|
|
498
|
+
// For UI hooks.
|
|
499
|
+
instance.onAfterRun( instance.args, result );
|
|
500
|
+
|
|
501
|
+
this.afterRun( instance.command, instance.args, result );
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Responsible to clear command from trace, and run 'run:after' event.
|
|
506
|
+
* Method fired after the command runs.
|
|
507
|
+
*
|
|
508
|
+
* @param command
|
|
509
|
+
* @param args
|
|
510
|
+
* @param results Its result, or the error it failed with.
|
|
511
|
+
* @param removeTrace
|
|
512
|
+
*/
|
|
513
|
+
public afterRun<TResult>( command: string, args: CommandArgs, results?: TResult, removeTrace = true ): void {
|
|
514
|
+
const component = this.requireComponent( command );
|
|
515
|
+
|
|
516
|
+
if ( args.onAfter ) {
|
|
517
|
+
args.onAfter.call( component, args, results );
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if ( this.hasListeners( "run:after" ) ) {
|
|
521
|
+
this.trigger( "run:after", component, command, args, results );
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
if ( removeTrace ) {
|
|
525
|
+
this.removeCurrentTrace( component );
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* A command failed: its catch hooks run, and 'run:catch' tells who listens. A command that handles its own
|
|
531
|
+
* failure (it tells the user, and its run goes on) reports it here too, so it failed for its caller as well.
|
|
532
|
+
*
|
|
533
|
+
* @param e What the command threw, or rejected with, or the error it handled.
|
|
534
|
+
* @param instance
|
|
535
|
+
*/
|
|
536
|
+
public catchApply<TError>( e: TError, instance: RunnableCommand ): void {
|
|
537
|
+
instance.onCatchApply( e );
|
|
538
|
+
|
|
539
|
+
Console.error( e );
|
|
540
|
+
|
|
541
|
+
this.trigger( "run:catch", instance.component, instance.command, instance.args, e );
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Run shortcut.
|
|
546
|
+
*
|
|
547
|
+
* It's separated in order to allow override.
|
|
548
|
+
*
|
|
549
|
+
* @param command
|
|
550
|
+
* @param event The command's args.
|
|
551
|
+
*
|
|
552
|
+
* @return result
|
|
553
|
+
*/
|
|
554
|
+
public runShortcut( command: string, event: KeyboardEvent ): CommandsRunResult {
|
|
555
|
+
return this.runCommand( command, event );
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
public validateInstanceScope( instance: RunnableCommand, currentComponent: ComponentBase, command: string ): boolean {
|
|
559
|
+
if ( ! ( instance instanceof CommandBase ) ) {
|
|
560
|
+
this.error( `invalid instance, command: '${ command }' ` );
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// In case of different scope.
|
|
564
|
+
if ( currentComponent !== instance.component ) {
|
|
565
|
+
if ( $e.devTools ) {
|
|
566
|
+
$e.devTools.log.warn( `Command: '${ command }' registerArgs.component: '${ instance.component.getNamespace() }' while current component is: '${ currentComponent.getNamespace() }'` );
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
return false;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
return true;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
public addCurrentTrace( container: string, command: string, args: CommandArgs ): void {
|
|
576
|
+
this.currentTrace.push( command );
|
|
577
|
+
|
|
578
|
+
CommandsBase.trace.push( command );
|
|
579
|
+
|
|
580
|
+
this.attachCurrent( container, command, args );
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
public removeCurrentTrace( currentComponent: ComponentBase ): void {
|
|
584
|
+
const container = currentComponent.getServiceName();
|
|
585
|
+
|
|
586
|
+
this.currentTrace.pop();
|
|
587
|
+
|
|
588
|
+
CommandsBase.trace.pop();
|
|
589
|
+
|
|
590
|
+
this.detachCurrent( container );
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
public attachCurrent( container: string, command: string, args: CommandArgs ): void {
|
|
594
|
+
this.current[ container ] = command;
|
|
595
|
+
this.currentArgs[ container ] = args;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
public detachCurrent( container: string ): void {
|
|
599
|
+
delete this.current[ container ];
|
|
600
|
+
delete this.currentArgs[ container ];
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* @return the API call that runs these commands, for the dev tools' log
|
|
605
|
+
*/
|
|
606
|
+
public getCall(): string {
|
|
607
|
+
return "$e.run";
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Throws error.
|
|
612
|
+
*
|
|
613
|
+
* @throws {Error}
|
|
614
|
+
*
|
|
615
|
+
* @param message
|
|
616
|
+
*/
|
|
617
|
+
public error( message: string ): never {
|
|
618
|
+
throw Error( `Commands: ${ message }` );
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* @param command
|
|
623
|
+
*
|
|
624
|
+
* @throws {Error} when the command has no component (it is not registered)
|
|
625
|
+
*
|
|
626
|
+
* @return the command's component
|
|
627
|
+
*/
|
|
628
|
+
protected requireComponent( command: string ): ComponentBase {
|
|
629
|
+
const component = this.getComponent( command );
|
|
630
|
+
|
|
631
|
+
if ( ! component ) {
|
|
632
|
+
return this.error( `\`${ command }\` has no component.` );
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
return component;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* @param component A component, or its namespace.
|
|
640
|
+
*
|
|
641
|
+
* @return the component
|
|
642
|
+
*/
|
|
643
|
+
private componentOf( component: ComponentBase | string ): ComponentBase {
|
|
644
|
+
if ( "string" !== typeof component ) {
|
|
645
|
+
return component;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
const registered = $e.components.getByNamespace( component );
|
|
649
|
+
|
|
650
|
+
if ( ! registered ) {
|
|
651
|
+
return this.error( `'${ component }' component is not exist.` );
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
return registered;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* `$e.commands`: commands by name ('component/command'), each run with its component's dependency, hooks and trace.
|
|
660
|
+
* `$e.commandsInternal` and `$e.routes` are commands too.
|
|
661
|
+
*/
|
|
662
|
+
export default class Commands<TEntry extends CommandEntry = CommandClass> extends CommandsBase<TEntry> {
|
|
663
|
+
/**
|
|
664
|
+
* Runs a command, by a name known at runtime (`$e.run()` takes the app's names, typed).
|
|
665
|
+
*
|
|
666
|
+
* @param command
|
|
667
|
+
* @param args
|
|
668
|
+
*
|
|
669
|
+
* @return results
|
|
670
|
+
*/
|
|
671
|
+
public run( command: string, args?: CommandArgs ): CommandsRunResult {
|
|
672
|
+
return this.runCommand( command, args );
|
|
673
|
+
}
|
|
674
|
+
}
|