@zenflux/react-commander 0.0.17 → 0.0.18
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/package.json +3 -3
- package/src/_internal/command-not-found.ts +8 -0
- package/src/command-base.ts +14 -17
- package/src/commands-manager.ts +15 -9
- package/src/definitions.ts +5 -0
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"name": "@zenflux/react-commander",
|
|
4
4
|
"author": "Leonid Vinikov <leonidvinikov@gmail.com> (https://github.com/iNewLegend)",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "0.0.
|
|
6
|
+
"version": "0.0.18",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"sideEffects": false,
|
|
9
9
|
"types": "src/index.ts",
|
|
@@ -48,8 +48,8 @@
|
|
|
48
48
|
"access": "public"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@zenflux/commands": "0.0.
|
|
52
|
-
"@zenflux/core": "0.
|
|
51
|
+
"@zenflux/commands": "0.0.2",
|
|
52
|
+
"@zenflux/core": "0.2.0",
|
|
53
53
|
"eventemitter3": "^5.0.1"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
package/src/command-base.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
// The class itself, not core's index: the index is its built bundle, with every module of it.
|
|
2
|
+
import { ObjectBase } from "@zenflux/core/src/bases/object-base";
|
|
2
3
|
|
|
3
4
|
import commandsManager from "@zenflux/react-commander/commands-manager";
|
|
4
5
|
|
|
@@ -11,12 +12,10 @@ import type React from "react";
|
|
|
11
12
|
/**
|
|
12
13
|
* Each created command is registered within the commands manager, and the instance created only once per command.
|
|
13
14
|
*
|
|
14
|
-
* Extends `
|
|
15
|
-
*
|
|
16
|
-
* - `CommandBase`: logger, `onBeforeApply`/`onAfterApply` lifecycle hooks, `run()` method
|
|
17
|
-
* - `CommandPublic`: semantic marker for user-facing commands
|
|
15
|
+
* Extends `ObjectBase` from `@zenflux/core`: a unique id, `getName()`, `getUniqueId()`, `getHierarchyNames()`. `$e`
|
|
16
|
+
* runs it (`execute()`): core has no command system of its own.
|
|
18
17
|
*/
|
|
19
|
-
export abstract class CommandBase<TState = React.ComponentState, TArgs = DCommandArgs> extends
|
|
18
|
+
export abstract class CommandBase<TState = React.ComponentState, TArgs = DCommandArgs> extends ObjectBase {
|
|
20
19
|
public readonly commandName: string;
|
|
21
20
|
|
|
22
21
|
private reactOptions: DCommandOptions<TState> = {};
|
|
@@ -41,11 +40,10 @@ export abstract class CommandBase<TState = React.ComponentState, TArgs = DComman
|
|
|
41
40
|
}
|
|
42
41
|
|
|
43
42
|
/**
|
|
44
|
-
* React-commander creates command instances during registration (not per-execution)
|
|
45
|
-
* so we pass empty args/options to core's CommandBase constructor.
|
|
43
|
+
* React-commander creates command instances during registration (not per-execution).
|
|
46
44
|
*/
|
|
47
45
|
public constructor( _registerArgs: DCommandRegisterArgs ) {
|
|
48
|
-
super(
|
|
46
|
+
super();
|
|
49
47
|
|
|
50
48
|
this.commandName = ( new.target as typeof CommandBase ).getName();
|
|
51
49
|
}
|
|
@@ -62,10 +60,8 @@ export abstract class CommandBase<TState = React.ComponentState, TArgs = DComman
|
|
|
62
60
|
}
|
|
63
61
|
|
|
64
62
|
/**
|
|
65
|
-
* React-aware command execution entry point
|
|
66
|
-
*
|
|
67
|
-
* Integrates core's lifecycle hooks (`onBeforeApply`/`onAfterApply`) while
|
|
68
|
-
* preserving react-commander's event emission and state management.
|
|
63
|
+
* React-aware command execution entry point: `onBeforeApply()`, `apply()`, `onAfterApply()`, then the emitter's
|
|
64
|
+
* listeners.
|
|
69
65
|
*/
|
|
70
66
|
public async execute( emitter: EventEmitter, args: TArgs, options?: DCommandOptions<TState> ): Promise<any> {
|
|
71
67
|
if ( options ) {
|
|
@@ -74,12 +70,10 @@ export abstract class CommandBase<TState = React.ComponentState, TArgs = DComman
|
|
|
74
70
|
|
|
75
71
|
this.validateArgs?.( args, options );
|
|
76
72
|
|
|
77
|
-
// Core lifecycle: before
|
|
78
73
|
this.onBeforeApply?.();
|
|
79
74
|
|
|
80
75
|
const result = await this.apply( args as any );
|
|
81
76
|
|
|
82
|
-
// Core lifecycle: after
|
|
83
77
|
this.onAfterApply?.();
|
|
84
78
|
|
|
85
79
|
// Listeners on the emitter it was given: a caller's own (the hooks are `$e`'s, run by the manager's run).
|
|
@@ -96,9 +90,12 @@ export abstract class CommandBase<TState = React.ComponentState, TArgs = DComman
|
|
|
96
90
|
protected validateArgs?( _args: TArgs, _options?: DCommandOptions<TState> ) {
|
|
97
91
|
}
|
|
98
92
|
|
|
93
|
+
protected onBeforeApply?(): void;
|
|
94
|
+
|
|
95
|
+
protected onAfterApply?(): void;
|
|
96
|
+
|
|
99
97
|
/**
|
|
100
|
-
*
|
|
101
|
-
* optional — react-commander commands may or may not implement apply.
|
|
98
|
+
* What the command does: optional, as a command may only be hooked.
|
|
102
99
|
*/
|
|
103
100
|
protected apply( _args?: any, _options?: any ): Promise<any> | any {
|
|
104
101
|
}
|
package/src/commands-manager.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
import { $e, API, CommandBase as ManagedCommandBase, ComponentBase, dispatch, hookData, hookUI, instancesStore, select, subscribe } from "@zenflux/commands";
|
|
3
3
|
|
|
4
|
-
import { CommandNotFound } from "
|
|
5
|
-
|
|
4
|
+
import { CommandNotFound } from "./_internal/command-not-found";
|
|
6
5
|
import { DEBUG_ENABLED, DEV_ENABLED, GET_INTERNAL_SYMBOL, HOT_RELOADING } from "./_internal/constants";
|
|
7
6
|
import { logger } from "./_internal/logger";
|
|
8
7
|
|
|
@@ -10,12 +9,11 @@ import core from "./_internal/core";
|
|
|
10
9
|
|
|
11
10
|
import type { CommandArgs, InstancesStoreConfig, SystemArgs } from "@zenflux/commands";
|
|
12
11
|
|
|
13
|
-
import type { TCommandCallbackType } from "@zenflux/core/src/interfaces";
|
|
14
|
-
|
|
15
12
|
import type { CommandBase } from "@zenflux/react-commander/command-base";
|
|
16
13
|
|
|
17
14
|
import type {
|
|
18
15
|
DCommandArgs,
|
|
16
|
+
DCommandCallback,
|
|
19
17
|
DCommandHookHandle,
|
|
20
18
|
DCommandIdArgs,
|
|
21
19
|
DCommandNewInstanceWithArgs,
|
|
@@ -157,6 +155,10 @@ class CommandsManager {
|
|
|
157
155
|
const manager = this;
|
|
158
156
|
|
|
159
157
|
return class extends ManagedCommandBase<ManagedArgs, Promise<unknown>> {
|
|
158
|
+
public static getName() {
|
|
159
|
+
return commandName;
|
|
160
|
+
}
|
|
161
|
+
|
|
160
162
|
public apply( { componentNameUnique, ... args }: ManagedArgs ) {
|
|
161
163
|
return manager.execute( { commandName, componentName, componentNameUnique }, args );
|
|
162
164
|
}
|
|
@@ -255,6 +257,10 @@ class CommandsManager {
|
|
|
255
257
|
this.components[ componentName ] = component;
|
|
256
258
|
} else {
|
|
257
259
|
this.components[ componentName ] = api.components.register( new ( class extends ComponentBase<InstancesStoreConfig> {
|
|
260
|
+
public static getName() {
|
|
261
|
+
return componentName;
|
|
262
|
+
}
|
|
263
|
+
|
|
258
264
|
public getNamespace() {
|
|
259
265
|
return componentName;
|
|
260
266
|
}
|
|
@@ -632,7 +638,7 @@ class CommandsManager {
|
|
|
632
638
|
/**
|
|
633
639
|
* Register a data-only before hook for a command: called with its args, before it applies.
|
|
634
640
|
*/
|
|
635
|
-
public onBefore( hookCommand: string, callback:
|
|
641
|
+
public onBefore( hookCommand: string, callback: DCommandCallback ) {
|
|
636
642
|
// Outside the class: HookBase reads `getId()` in its constructor, before a subclass's fields are set.
|
|
637
643
|
const id = hookId( "before", hookCommand );
|
|
638
644
|
|
|
@@ -652,7 +658,7 @@ class CommandsManager {
|
|
|
652
658
|
/**
|
|
653
659
|
* Register a UI-only before hook for a command: called with its args, before it runs.
|
|
654
660
|
*/
|
|
655
|
-
public onBeforeUI( hookCommand: string, callback:
|
|
661
|
+
public onBeforeUI( hookCommand: string, callback: DCommandCallback ) {
|
|
656
662
|
const id = hookId( "before-ui", hookCommand );
|
|
657
663
|
|
|
658
664
|
managerAPI().hooks.registerUIBefore( new ( class extends hookUI.Before {
|
|
@@ -671,7 +677,7 @@ class CommandsManager {
|
|
|
671
677
|
/**
|
|
672
678
|
* Register a data-only after hook for a command: called with its args and `result`, after it applied.
|
|
673
679
|
*/
|
|
674
|
-
public onAfter( hookCommand: string, callback:
|
|
680
|
+
public onAfter( hookCommand: string, callback: DCommandCallback ) {
|
|
675
681
|
const id = hookId( "after", hookCommand );
|
|
676
682
|
|
|
677
683
|
managerAPI().hooks.registerDataAfter( new ( class extends hookData.After {
|
|
@@ -690,7 +696,7 @@ class CommandsManager {
|
|
|
690
696
|
/**
|
|
691
697
|
* Register a UI-only after hook for a command: called with its args and `result`, after it ran.
|
|
692
698
|
*/
|
|
693
|
-
public onAfterUI( command: string, callback:
|
|
699
|
+
public onAfterUI( command: string, callback: DCommandCallback ) {
|
|
694
700
|
const id = hookId( "after-ui", command );
|
|
695
701
|
|
|
696
702
|
managerAPI().hooks.registerUIAfter( new ( class extends hookUI.After {
|
|
@@ -709,7 +715,7 @@ class CommandsManager {
|
|
|
709
715
|
/**
|
|
710
716
|
* Register a one-time after hook for a command: called after its next run only.
|
|
711
717
|
*/
|
|
712
|
-
public onAfterOnce( command: string, callback:
|
|
718
|
+
public onAfterOnce( command: string, callback: DCommandCallback ) {
|
|
713
719
|
const api = managerAPI(),
|
|
714
720
|
id = hookId( "after-once", command );
|
|
715
721
|
|
package/src/definitions.ts
CHANGED
|
@@ -138,6 +138,11 @@ export interface DCommandIdArgs {
|
|
|
138
138
|
/**
|
|
139
139
|
* Interface `DCommandArgs` - This interface defines the structure for the arguments provided to commands
|
|
140
140
|
**/
|
|
141
|
+
/**
|
|
142
|
+
* A callback of a command's hook (`onBefore()`, `onAfter()`...): what the command ran with, and its result.
|
|
143
|
+
*/
|
|
144
|
+
export type DCommandCallback = ( args?: any, options?: any ) => any;
|
|
145
|
+
|
|
141
146
|
export type DCommandArgs = {
|
|
142
147
|
[ key: string | number | symbol ]: any
|
|
143
148
|
};
|