@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
package/package.json
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
{
|
|
2
|
+
"$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/package.json",
|
|
2
3
|
"name": "@zenflux/commands",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
4
|
+
"author": "Leonid Vinikov <leonidvinikov@gmail.com> (https://github.com/iNewLegend)",
|
|
5
|
+
"description": "$e: Elementor's web-cli as a package. Components that register commands, routes, hooks, shortcuts and a store each (a reducer's state), run by name ($e.run( 'component/command', args )). @zenflux/react-commander's manager.",
|
|
6
|
+
"license": "GPL-3.0-or-later",
|
|
7
|
+
"version": "0.0.1",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"files": [
|
|
11
|
+
"src"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/zenflux/zenflux.git",
|
|
16
|
+
"directory": "packages/zenflux-commands"
|
|
17
|
+
},
|
|
18
|
+
"exports": {
|
|
19
|
+
".": "./src/index.ts",
|
|
20
|
+
"./src/*": "./src/*.ts"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
}
|
|
6
25
|
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# @zenflux/commands
|
|
2
|
+
|
|
3
|
+
`$e`: Elementor's web-cli as a package. Components register commands, routes, hooks, shortcuts and a store each (a reducer's state); everything runs by name. It is [@zenflux/react-commander](../zenflux-react-commander/readme.md)'s manager.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { API, $e, ComponentBase, CommandBase, dispatch, valueStore, type CommandsOf } from "@zenflux/commands";
|
|
7
|
+
|
|
8
|
+
const store = valueStore< { count: number } >( { count: 0 } );
|
|
9
|
+
|
|
10
|
+
class Add extends CommandBase< { by: number }, void, CounterComponent > {
|
|
11
|
+
apply( { by }: { by: number } ) {
|
|
12
|
+
dispatch( this.component.store ).setCount( ( count ) => count + by );
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
class CounterComponent extends ComponentBase< typeof store > {
|
|
17
|
+
getNamespace() { return "counter"; }
|
|
18
|
+
defaultCommands(): CommandsOf< "counter" > { return { "counter/add": Add }; }
|
|
19
|
+
defaultStore() { return store; }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare module "@zenflux/commands" {
|
|
23
|
+
interface CommandRegistry { "counter/add": typeof Add; }
|
|
24
|
+
interface ComponentRegistry { counter: CounterComponent; }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
new API( { isDebug: true, storePrefix: "my-app" } );
|
|
28
|
+
$e.components.register( new CounterComponent() );
|
|
29
|
+
|
|
30
|
+
$e.run( "counter/add", { by: 2 } );
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
- `new API()` puts `$e` on the page, `window.$e`, as Elementor's was, unless another `$e` is there already (Elementor's own, in its editor and wp-admin), which it leaves alone; the package's classes read their own binding, so both work on such a page.
|
|
34
|
+
- `$e.run()` runs a command by its full name (`<component>/<command>`), typed by the `CommandRegistry` an app declares.
|
|
35
|
+
- Hooks (`hookUI.Before`, `hookUI.After`, `hookData.Dependency`, `hookData.After`, the catch hooks) run around a command by its name; `$e.hooks.unregister( type, id )` takes one back.
|
|
36
|
+
- `$e.routes`, `$e.shortcuts`, and `$e.devTools` (with `isDebug`) as Elementor's.
|
|
37
|
+
- `withInstances( config )` adds a slice to a component's store for the state of its instances, each by its unique name (`patchInstance()`, `removeInstance()`, `getInstance()`, `getInstances()`); `instancesStore()` is a store of that slice alone. @zenflux/react-commander keeps its components' state there.
|
|
38
|
+
- A component's store is a reducer's state and nothing more: `defaultStore()` gives `{ reducer, actions, selectors }` (`valueStore( { ... } )` makes one of plain values), `select( component.store )` binds the selectors, `dispatch( component.store )` the action creators, `subscribe( listener, component.store )` hears each change. An action that changes nothing tells no one. No dependency: @zenflux/react-commander's `useSelect()` draws a component again as a store it read changes.
|
|
39
|
+
|
|
40
|
+
GPL-3.0-or-later, the license of Elementor, whose web-cli it ports (LICENSE).
|
package/src/api.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/// <reference path="./env.d.ts" />
|
|
2
|
+
|
|
3
|
+
/* Alphabetical order */
|
|
4
|
+
import CommandBase from "./modules/command-base";
|
|
5
|
+
import CommandInternalBase from "./modules/command-internal-base";
|
|
6
|
+
import Commands from "./core/commands";
|
|
7
|
+
import CommandsInternal from "./core/commands-internal";
|
|
8
|
+
import ComponentBase from "./modules/component-base";
|
|
9
|
+
import Components from "./core/components";
|
|
10
|
+
import DevTools from "./extras/dev-tools";
|
|
11
|
+
import HookBreak from "./modules/hook-break";
|
|
12
|
+
import Hooks from "./core/hooks";
|
|
13
|
+
import Routes from "./core/routes";
|
|
14
|
+
|
|
15
|
+
import Shortcuts from "./core/shortcuts";
|
|
16
|
+
import { setAPI } from "./instance";
|
|
17
|
+
|
|
18
|
+
import * as hookData from "./modules/hooks/data";
|
|
19
|
+
import * as hookUI from "./modules/hooks/ui";
|
|
20
|
+
|
|
21
|
+
import type { RouteOptions } from "./core/routes";
|
|
22
|
+
|
|
23
|
+
import type {
|
|
24
|
+
ArgsOf,
|
|
25
|
+
ArgsParam,
|
|
26
|
+
CommandInternalName,
|
|
27
|
+
CommandInternalRegistry,
|
|
28
|
+
CommandName,
|
|
29
|
+
CommandRegistry,
|
|
30
|
+
EmptyArgs,
|
|
31
|
+
RouteArgs,
|
|
32
|
+
RouteName,
|
|
33
|
+
RunResult,
|
|
34
|
+
} from "./types";
|
|
35
|
+
|
|
36
|
+
export interface APIConfig {
|
|
37
|
+
/**
|
|
38
|
+
* `$e.devTools` (in a development build only): what runs, logged in the console (each command, the system that ran
|
|
39
|
+
* it, its hooks), and what is registered; and warnings, such as several handlers for one shortcut.
|
|
40
|
+
*/
|
|
41
|
+
isDebug?: boolean;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* What the components' stores are named under: '<storePrefix>/<component>'.
|
|
45
|
+
*/
|
|
46
|
+
storePrefix?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A route's args and options, after its name: the args optional when the route takes none.
|
|
51
|
+
*/
|
|
52
|
+
export type RouteParams<TArgs> = EmptyArgs extends TArgs
|
|
53
|
+
? [ args?: TArgs, options?: RouteOptions ]
|
|
54
|
+
: [ args: TArgs, options?: RouteOptions ];
|
|
55
|
+
|
|
56
|
+
const DEFAULT_CONFIG: Required<APIConfig> = {
|
|
57
|
+
isDebug: false,
|
|
58
|
+
storePrefix: "zenflux",
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export default class API {
|
|
62
|
+
public config: Required<APIConfig>;
|
|
63
|
+
public components: Components;
|
|
64
|
+
public commands: Commands;
|
|
65
|
+
public commandsInternal: CommandsInternal;
|
|
66
|
+
public hooks: Hooks;
|
|
67
|
+
public routes: Routes;
|
|
68
|
+
public shortcuts: Shortcuts;
|
|
69
|
+
public modules: {
|
|
70
|
+
CommandBase: typeof CommandBase;
|
|
71
|
+
CommandInternalBase: typeof CommandInternalBase;
|
|
72
|
+
ComponentBase: typeof ComponentBase;
|
|
73
|
+
HookBreak: typeof HookBreak;
|
|
74
|
+
hookData: typeof hookData;
|
|
75
|
+
hookUI: typeof hookUI;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* In a development build, with `isDebug`.
|
|
80
|
+
*/
|
|
81
|
+
public devTools?: DevTools;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Create the API (Elementor's `$e`). The package's classes use the last one created.
|
|
85
|
+
*
|
|
86
|
+
* @param config
|
|
87
|
+
*/
|
|
88
|
+
public constructor( config: APIConfig = {} ) {
|
|
89
|
+
setAPI( this );
|
|
90
|
+
|
|
91
|
+
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
92
|
+
|
|
93
|
+
this.components = new Components();
|
|
94
|
+
|
|
95
|
+
this.commands = new Commands();
|
|
96
|
+
this.commandsInternal = new CommandsInternal();
|
|
97
|
+
|
|
98
|
+
this.hooks = new Hooks();
|
|
99
|
+
this.routes = new Routes();
|
|
100
|
+
this.shortcuts = new Shortcuts( window );
|
|
101
|
+
|
|
102
|
+
this.modules = {
|
|
103
|
+
CommandBase,
|
|
104
|
+
CommandInternalBase,
|
|
105
|
+
|
|
106
|
+
ComponentBase,
|
|
107
|
+
|
|
108
|
+
HookBreak,
|
|
109
|
+
|
|
110
|
+
hookData,
|
|
111
|
+
hookUI,
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// What runs, in the console, and what is registered (extras/dev-tools.ts): in a development build only. A
|
|
115
|
+
// production build makes the condition false (process.env.NODE_ENV is 'production' there), so it holds no dev
|
|
116
|
+
// tools at all, whatever its config says.
|
|
117
|
+
if ( "production" !== process.env.NODE_ENV && this.config.isDebug ) {
|
|
118
|
+
this.devTools = new DevTools();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Run a command of the app's CommandRegistry (`$e.commands.run()`, by name).
|
|
124
|
+
*
|
|
125
|
+
* @param command
|
|
126
|
+
* @param args Optional when the command takes none.
|
|
127
|
+
*
|
|
128
|
+
* @return its `apply()`'s result, or false when it did not run
|
|
129
|
+
*/
|
|
130
|
+
public run<TName extends CommandName>( command: TName, ...[ args ]: ArgsParam<ArgsOf<CommandRegistry[ TName ]>> ): RunResult<CommandRegistry[ TName ]> {
|
|
131
|
+
// The class registered under the name is the registry's: its run gives that class's result.
|
|
132
|
+
return this.commands.run( command, args ) as RunResult<CommandRegistry[ TName ]>;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Run a command of the app's CommandInternalRegistry (`$e.commandsInternal.run()`, by name).
|
|
137
|
+
*
|
|
138
|
+
* @param command
|
|
139
|
+
* @param args Optional when the command takes none.
|
|
140
|
+
*
|
|
141
|
+
* @return its `apply()`'s result, or false when it did not run
|
|
142
|
+
*/
|
|
143
|
+
public internal<TName extends CommandInternalName>( command: TName, ...[ args ]: ArgsParam<ArgsOf<CommandInternalRegistry[ TName ]>> ): RunResult<CommandInternalRegistry[ TName ]> {
|
|
144
|
+
// The class registered under the name is the registry's: its run gives that class's result.
|
|
145
|
+
return this.commandsInternal.run( command, args ) as RunResult<CommandInternalRegistry[ TName ]>;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Go to a route of the app's RouteRegistry (`$e.routes.to()`, by name).
|
|
150
|
+
*
|
|
151
|
+
* @param route
|
|
152
|
+
* @param args Optional when the route takes none.
|
|
153
|
+
* @param options { history: true } by default.
|
|
154
|
+
*/
|
|
155
|
+
public route<TName extends RouteName>( route: TName, ...[ args, options ]: RouteParams<RouteArgs<TName>> ): void {
|
|
156
|
+
return this.routes.to( route, args ?? {}, options ?? { history: true } );
|
|
157
|
+
}
|
|
158
|
+
}
|