@zenflux/commands 0.0.0-stage → 0.0.2

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.
Files changed (43) hide show
  1. package/LICENSE +674 -0
  2. package/package.json +25 -3
  3. package/readme.md +42 -0
  4. package/src/api.ts +166 -0
  5. package/src/core/commands-internal.ts +15 -0
  6. package/src/core/commands.ts +684 -0
  7. package/src/core/components.ts +107 -0
  8. package/src/core/hooks/base.ts +451 -0
  9. package/src/core/hooks/data.ts +58 -0
  10. package/src/core/hooks/ui.ts +44 -0
  11. package/src/core/hooks.ts +267 -0
  12. package/src/core/routes.ts +247 -0
  13. package/src/core/shortcuts.ts +358 -0
  14. package/src/core/store.ts +155 -0
  15. package/src/env.d.ts +15 -0
  16. package/src/extras/dev-tools.ts +443 -0
  17. package/src/index.ts +83 -0
  18. package/src/instance.ts +30 -0
  19. package/src/modules/args-object.ts +113 -0
  20. package/src/modules/command-base.ts +51 -0
  21. package/src/modules/command-callback-base.ts +42 -0
  22. package/src/modules/command-infra.ts +230 -0
  23. package/src/modules/command-internal-base.ts +19 -0
  24. package/src/modules/component-base.ts +483 -0
  25. package/src/modules/hook-base.ts +158 -0
  26. package/src/modules/hook-break.ts +8 -0
  27. package/src/modules/hooks/data/after.ts +19 -0
  28. package/src/modules/hooks/data/base.ts +15 -0
  29. package/src/modules/hooks/data/catch.ts +16 -0
  30. package/src/modules/hooks/data/dependency.ts +16 -0
  31. package/src/modules/hooks/data/index.ts +6 -0
  32. package/src/modules/hooks/ui/after.ts +18 -0
  33. package/src/modules/hooks/ui/base.ts +15 -0
  34. package/src/modules/hooks/ui/before.ts +16 -0
  35. package/src/modules/hooks/ui/catch.ts +16 -0
  36. package/src/modules/hooks/ui/index.ts +6 -0
  37. package/src/modules/instance-type.ts +119 -0
  38. package/src/modules/module.ts +224 -0
  39. package/src/types.ts +194 -0
  40. package/src/utils/instances-slice.ts +123 -0
  41. package/src/utils/object.ts +162 -0
  42. package/src/utils/value-store.ts +145 -0
  43. package/README.md +0 -3
@@ -0,0 +1,51 @@
1
+ import CommandInfra from "./command-infra";
2
+
3
+ import { $e } from "@zenflux/commands/src/instance";
4
+
5
+ import type ComponentBase from "./component-base";
6
+ import type { CommandArgs, EmptyArgs, HookReturn } from "@zenflux/commands/src/types";
7
+
8
+ /**
9
+ * A command with hooks: UI hooks before and after it runs, data hooks around what it applies.
10
+ *
11
+ * @name $e.modules.CommandBase
12
+ */
13
+ export default abstract class CommandBase<
14
+ TArgs extends CommandArgs = EmptyArgs,
15
+ TResult = void,
16
+ TComponent extends ComponentBase = ComponentBase,
17
+ > extends CommandInfra<TArgs, TResult, TComponent> {
18
+ public static getName(): string {
19
+ return "ZenFlux/Commands/Modules/CommandBase";
20
+ }
21
+
22
+ public onBeforeRun( args: TArgs ): void {
23
+ $e.hooks.runUIBefore( this.command, args );
24
+ }
25
+
26
+ public onAfterRun<TValue>( args: TArgs, result: TValue ): void {
27
+ $e.hooks.runUIAfter( this.command, args, result );
28
+ }
29
+
30
+ public onBeforeApply( args: TArgs ): void {
31
+ $e.hooks.runDataDependency( this.command, args );
32
+ }
33
+
34
+ public onAfterApply<TValue>( args: TArgs, result: TValue ): HookReturn[] | false {
35
+ return $e.hooks.runDataAfter( this.command, args, result );
36
+ }
37
+
38
+ public onCatchApply<TError>( e: TError ): void {
39
+ this.runCatchHooks( e );
40
+ }
41
+
42
+ /**
43
+ * Run all the catch hooks.
44
+ *
45
+ * @param e What the command threw, or rejected with.
46
+ */
47
+ public runCatchHooks<TError>( e: TError ): void {
48
+ $e.hooks.runDataCatch( this.command, this.args, e );
49
+ $e.hooks.runUICatch( this.command, this.args, e );
50
+ }
51
+ }
@@ -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 getName(): string {
13
+ return "ZenFlux/Commands/Modules/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
+ getName(): 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
+ "getName" in value && "function" === typeof value.getName && "getInfo" in value;
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 getName(): string {
81
+ return "ZenFlux/Commands/Modules/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 getName(): string {
17
+ return "ZenFlux/Commands/Modules/CommandInternalBase";
18
+ }
19
+ }