@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,42 @@
|
|
|
1
|
+
import CommandBase from "./command-base";
|
|
2
|
+
|
|
3
|
+
import type { RegisteredCallback } from "./command-infra";
|
|
4
|
+
import type { CommandArgs } from "@zenflux/commands/src/types";
|
|
5
|
+
|
|
6
|
+
const isCallbackClass = ( value: Function ): value is typeof CommandCallbackBase => "getCallback" in value;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* To support pure callbacks in the API (core/commands.ts), to ensure they have registered with the proper context.
|
|
10
|
+
*/
|
|
11
|
+
export default class CommandCallbackBase extends CommandBase<CommandArgs> {
|
|
12
|
+
public static getInstanceType(): string {
|
|
13
|
+
return "CommandCallbackBase";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Get original callback of the command.
|
|
18
|
+
*
|
|
19
|
+
* @throws {Error} when the class was registered without one
|
|
20
|
+
*
|
|
21
|
+
* @return the callback
|
|
22
|
+
*/
|
|
23
|
+
public static getCallback(): RegisteredCallback {
|
|
24
|
+
const { callback } = this.getRegisterConfig();
|
|
25
|
+
|
|
26
|
+
if ( ! callback ) {
|
|
27
|
+
throw Error( `\`${ this.getCommand() }\` has no callback.` );
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return callback;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public apply( args: CommandArgs ): void {
|
|
34
|
+
const Command = this.constructor;
|
|
35
|
+
|
|
36
|
+
if ( ! isCallbackClass( Command ) ) {
|
|
37
|
+
throw RangeError( "Doing it wrong: a callback command is a class of CommandCallbackBase." );
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return Command.getCallback()( args );
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import ArgsObject from "./args-object";
|
|
2
|
+
|
|
3
|
+
import type ComponentBase from "./component-base";
|
|
4
|
+
import type { CommandArgs, CommandInfo, EmptyArgs, HookReturn } from "@zenflux/commands/src/types";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A command or route that is a function: called with the args of its run. A component's maps name it with its own
|
|
8
|
+
* args' type (`( { slug }: CategoryArgs ) => ...`).
|
|
9
|
+
*/
|
|
10
|
+
export type CommandCallback = ( args: never ) => void;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Such a function as the commands systems keep it: called with the args of its run, whatever its name runs with.
|
|
14
|
+
*/
|
|
15
|
+
export type RegisteredCallback = ( args: CommandArgs ) => void;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The command and component a class was registered for (`ComponentBase.registerCommand()`), and the function it runs
|
|
19
|
+
* for a command that is one.
|
|
20
|
+
*/
|
|
21
|
+
export interface RegisterConfig {
|
|
22
|
+
command: string;
|
|
23
|
+
component: ComponentBase;
|
|
24
|
+
callback?: RegisteredCallback;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A command as the commands systems run it, whatever its args and result: `run()` gives its result, which they pass on
|
|
29
|
+
* to the hooks and events as it is.
|
|
30
|
+
*/
|
|
31
|
+
export interface RunnableCommand {
|
|
32
|
+
args: CommandArgs;
|
|
33
|
+
command: string;
|
|
34
|
+
component: ComponentBase;
|
|
35
|
+
run(): void;
|
|
36
|
+
onBeforeRun( args: CommandArgs ): void;
|
|
37
|
+
onAfterRun<TValue>( args: CommandArgs, result: TValue ): void;
|
|
38
|
+
onBeforeApply( args: CommandArgs ): void;
|
|
39
|
+
onAfterApply<TValue>( args: CommandArgs, result: TValue ): HookReturn[] | false | void;
|
|
40
|
+
onCatchApply<TError>( e: TError ): void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A command class, as the commands systems keep it by name: it takes the args of its own type (never: none that TS can
|
|
45
|
+
* check here).
|
|
46
|
+
*/
|
|
47
|
+
export interface CommandClass {
|
|
48
|
+
new ( args: never ): RunnableCommand;
|
|
49
|
+
registerConfig: Readonly<RegisterConfig> | null;
|
|
50
|
+
getInstanceType(): string;
|
|
51
|
+
getInfo(): CommandInfo;
|
|
52
|
+
getRegisterConfig(): Readonly<RegisterConfig>;
|
|
53
|
+
getCommand(): string;
|
|
54
|
+
getComponent(): ComponentBase;
|
|
55
|
+
setRegisterConfig( config: RegisterConfig ): void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param value A function: a class, or a callback.
|
|
60
|
+
*
|
|
61
|
+
* @return whether it is a command class (it names its type)
|
|
62
|
+
*/
|
|
63
|
+
export const isCommandClass = ( value: Function ): value is CommandClass =>
|
|
64
|
+
"getInstanceType" in value && "function" === typeof value.getInstanceType;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A command: `apply( args )` does it. `TArgs` are its args (checked by `$e.run()`), `TResult` what `apply()` returns,
|
|
68
|
+
* and `TComponent` the component that registers it (`this.component`).
|
|
69
|
+
*/
|
|
70
|
+
export default abstract class CommandInfra<
|
|
71
|
+
TArgs extends CommandArgs = EmptyArgs,
|
|
72
|
+
TResult = void,
|
|
73
|
+
TComponent extends ComponentBase = ComponentBase,
|
|
74
|
+
> extends ArgsObject<TArgs> {
|
|
75
|
+
/**
|
|
76
|
+
* The command and component the class was registered for (set by `ComponentBase.registerCommand()`).
|
|
77
|
+
*/
|
|
78
|
+
public static registerConfig: Readonly<RegisterConfig> | null = null;
|
|
79
|
+
|
|
80
|
+
public static getInstanceType(): string {
|
|
81
|
+
return "CommandInfra";
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Get info of command.
|
|
86
|
+
*
|
|
87
|
+
* @return Extra information about the command (CommandInfo).
|
|
88
|
+
*/
|
|
89
|
+
public static getInfo(): CommandInfo {
|
|
90
|
+
return {};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @throws {RangeError} when the class was not registered
|
|
95
|
+
*
|
|
96
|
+
* @return what the class was registered for
|
|
97
|
+
*/
|
|
98
|
+
public static getRegisterConfig(): Readonly<RegisterConfig> {
|
|
99
|
+
if ( ! this.registerConfig ) {
|
|
100
|
+
throw RangeError( "Doing it wrong: Each command type should have `registerConfig`." );
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return this.registerConfig;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @return Self command name.
|
|
108
|
+
*/
|
|
109
|
+
public static getCommand(): string {
|
|
110
|
+
return this.getRegisterConfig().command;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @return Self component
|
|
115
|
+
*/
|
|
116
|
+
public static getComponent(): ComponentBase {
|
|
117
|
+
return this.getRegisterConfig().component;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public static setRegisterConfig( config: RegisterConfig ): void {
|
|
121
|
+
this.registerConfig = Object.freeze( config );
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The command it runs as: 'component/command'.
|
|
126
|
+
*/
|
|
127
|
+
public command: string;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The component that registered it.
|
|
131
|
+
*/
|
|
132
|
+
public component: TComponent;
|
|
133
|
+
|
|
134
|
+
public constructor( args: TArgs ) {
|
|
135
|
+
super( args );
|
|
136
|
+
|
|
137
|
+
const Command = this.constructor;
|
|
138
|
+
|
|
139
|
+
if ( ! isCommandClass( Command ) || ! Command.registerConfig ) {
|
|
140
|
+
throw RangeError( "Doing it wrong: Each command type should have `registerConfig`." );
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Acknowledge self about which command it run.
|
|
144
|
+
this.command = Command.getCommand();
|
|
145
|
+
|
|
146
|
+
// Assign instance of current component: the one that registered the class, which the class names as its
|
|
147
|
+
// TComponent (TS can't follow a class to the component it was registered by).
|
|
148
|
+
this.component = Command.getComponent() as TComponent;
|
|
149
|
+
|
|
150
|
+
// Whoever needs to do something before, without the constructor's `super`, can use `initialize()`.
|
|
151
|
+
this.initialize( args );
|
|
152
|
+
|
|
153
|
+
// Refresh args, maybe they changed in `initialize()`.
|
|
154
|
+
args = this.args;
|
|
155
|
+
|
|
156
|
+
// Validate args before run.
|
|
157
|
+
this.validateArgs( args );
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Initialize command, called after construction.
|
|
162
|
+
*
|
|
163
|
+
* @param args
|
|
164
|
+
*/
|
|
165
|
+
public initialize( _args: TArgs ): void {}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Validate command arguments.
|
|
169
|
+
*
|
|
170
|
+
* @param args
|
|
171
|
+
*/
|
|
172
|
+
public validateArgs( _args: TArgs ): void {}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Do the actual command.
|
|
176
|
+
*
|
|
177
|
+
* @param args
|
|
178
|
+
*
|
|
179
|
+
* @return Command results.
|
|
180
|
+
*/
|
|
181
|
+
public abstract apply( args: TArgs ): TResult;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Run command with history & hooks.
|
|
185
|
+
*
|
|
186
|
+
* @return Command results.
|
|
187
|
+
*/
|
|
188
|
+
public run(): TResult {
|
|
189
|
+
return this.apply( this.args );
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Called before run().
|
|
194
|
+
*
|
|
195
|
+
* @param args
|
|
196
|
+
*/
|
|
197
|
+
public onBeforeRun( _args: TArgs ): void {}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Called after run().
|
|
201
|
+
*
|
|
202
|
+
* @param args
|
|
203
|
+
* @param result
|
|
204
|
+
*/
|
|
205
|
+
public onAfterRun<TValue>( _args: TArgs, _result: TValue ): void {}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Called before apply().
|
|
209
|
+
*
|
|
210
|
+
* @param args
|
|
211
|
+
*/
|
|
212
|
+
public onBeforeApply( _args: TArgs ): void {}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Called after apply().
|
|
216
|
+
*
|
|
217
|
+
* @param args
|
|
218
|
+
* @param result
|
|
219
|
+
*
|
|
220
|
+
* @return what the 'after' data hooks returned: the promises among them are waited for
|
|
221
|
+
*/
|
|
222
|
+
public onAfterApply<TValue>( _args: TArgs, _result: TValue ): HookReturn[] | false | void {}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Called after apply() failed.
|
|
226
|
+
*
|
|
227
|
+
* @param e What it threw, or rejected with.
|
|
228
|
+
*/
|
|
229
|
+
public onCatchApply<TError>( _e: TError ): void {}
|
|
230
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import CommandBase from "./command-base";
|
|
2
|
+
|
|
3
|
+
import type ComponentBase from "./component-base";
|
|
4
|
+
import type { CommandArgs, EmptyArgs } from "@zenflux/commands/src/types";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A command that runs through `$e.internal()`: one the app runs, not the user.
|
|
8
|
+
*
|
|
9
|
+
* @name $e.modules.CommandInternalBase
|
|
10
|
+
*/
|
|
11
|
+
export default abstract class CommandInternalBase<
|
|
12
|
+
TArgs extends CommandArgs = EmptyArgs,
|
|
13
|
+
TResult = void,
|
|
14
|
+
TComponent extends ComponentBase = ComponentBase,
|
|
15
|
+
> extends CommandBase<TArgs, TResult, TComponent> {
|
|
16
|
+
public static getInstanceType(): string {
|
|
17
|
+
return "CommandInternalBase";
|
|
18
|
+
}
|
|
19
|
+
}
|