@zenflux/react-commander 0.0.15 → 0.0.17
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 +21 -0
- package/package.json +7 -8
- package/readme.md +42 -1
- package/src/_internal/constants.ts +16 -4
- package/src/_internal/core.ts +14 -9
- package/src/command-base.ts +13 -19
- package/src/commands-manager.ts +492 -132
- package/src/hooks/index.ts +2 -1
- package/src/hooks/use-child-command/use-child-command-hook.ts +16 -19
- package/src/hooks/use-command/use-command-hook.ts +18 -6
- package/src/hooks/use-command/use-command-state-with-ref.ts +3 -54
- package/src/hooks/use-command/use-command-state.ts +6 -55
- package/src/hooks/use-command/use-command.ts +13 -32
- package/src/hooks/use-select.ts +73 -0
- package/src/hooks/utils.ts +60 -27
- package/src/index.ts +1 -2
- package/src/query/query-router.ts +0 -2
- package/src/with-commands.tsx +59 -79
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 ZenFlux
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
"$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/package.json",
|
|
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.17",
|
|
6
7
|
"type": "module",
|
|
7
8
|
"sideEffects": false,
|
|
8
9
|
"types": "src/index.ts",
|
|
@@ -38,7 +39,6 @@
|
|
|
38
39
|
"./query/definitions": "./src/query/query-definitions.ts",
|
|
39
40
|
"./query/list-diff": "./src/query/query-list-diff.ts",
|
|
40
41
|
"./query/module-base": "./src/query/query-module-base.ts",
|
|
41
|
-
"./query/item-module-base": "./src/query/query-item-module-base.ts",
|
|
42
42
|
"./query/list-module-base": "./src/query/query-list-module-base.ts",
|
|
43
43
|
"./query/router": "./src/query/query-router.ts",
|
|
44
44
|
"./query/utils": "./src/query/query-utils.ts",
|
|
@@ -48,19 +48,18 @@
|
|
|
48
48
|
"access": "public"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@zenflux/
|
|
51
|
+
"@zenflux/commands": "0.0.1",
|
|
52
|
+
"@zenflux/core": "0.1.33",
|
|
52
53
|
"eventemitter3": "^5.0.1"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|
|
55
56
|
"@types/react": "^19.2.0",
|
|
56
57
|
"@types/react-dom": "^19.2.0",
|
|
57
58
|
"react": "^19.2.0",
|
|
58
|
-
"react-dom": "^19.2.0"
|
|
59
|
-
"vite": "^8.2.2"
|
|
59
|
+
"react-dom": "^19.2.0"
|
|
60
60
|
},
|
|
61
61
|
"peerDependencies": {
|
|
62
|
-
"react": "
|
|
63
|
-
"react-dom": "
|
|
64
|
-
"vite": "^8.2.2"
|
|
62
|
+
"react": ">=18.3.0",
|
|
63
|
+
"react-dom": ">=18.3.0"
|
|
65
64
|
}
|
|
66
65
|
}
|
package/readme.md
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
# @zenflux/react-commander
|
|
2
2
|
|
|
3
|
-
A lightweight command orchestration layer for React that separates UI from behavior through typed Commands, per-instance component context, and
|
|
3
|
+
A lightweight command orchestration layer for React that separates UI from behavior through typed Commands, per-instance component context, and `$e` ([@zenflux/commands](../zenflux-commands/readme.md)) as its command manager. Includes a minimal Query abstraction for data fetching, caching, and lifecycle wiring.
|
|
4
4
|
|
|
5
5
|
## Table of contents
|
|
6
6
|
|
|
7
7
|
- [Installation](#installation)
|
|
8
8
|
- [Quick Start](#quick-start)
|
|
9
9
|
- [Core Concepts](#core-concepts)
|
|
10
|
+
- [The manager: `$e`](#the-manager-e)
|
|
10
11
|
- [API Reference](#api-reference)
|
|
11
12
|
- [Commands](#commands)
|
|
12
13
|
- [Hooks](#hooks)
|
|
@@ -157,6 +158,46 @@ A lightweight data fetching layer that provides:
|
|
|
157
158
|
- **Lifecycle Integration**: Automatic data loading and state updates
|
|
158
159
|
- **Type Safety**: End-to-end type safety from API to component
|
|
159
160
|
|
|
161
|
+
## The manager: `$e`
|
|
162
|
+
|
|
163
|
+
Every command runs through `$e`, the command system of [@zenflux/commands](../zenflux-commands/readme.md). As the first instance of a `withCommands()` component mounts, the component becomes an `$e` component of the same name, and each of its commands an `$e` command that runs the react-commander command on the instance its args name (`componentNameUnique`). So a command is run, hooked and logged as every other `$e` command:
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
import { API, $e } from "@zenflux/commands";
|
|
167
|
+
|
|
168
|
+
// Before the app renders: the API every command runs through (without one, react-commander makes it as the first
|
|
169
|
+
// component mounts). `storePrefix` names the `$e` components' stores.
|
|
170
|
+
new API( { isDebug: import.meta.env.DEV, storePrefix: "my-app" } );
|
|
171
|
+
|
|
172
|
+
// From anywhere, the console too: the instance by its unique name.
|
|
173
|
+
$e.run( "App/ItemsList/SetName", { componentNameUnique: "App/ItemsList-:r1:", id: "1", name: "New" } );
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
- `useCommand()`, `useComponent()` and `commandsManager.run()` add the instance for you; nothing in a component changes.
|
|
177
|
+
- A command's own name is its component's name and one more part (`App/ItemsList/SetName` of `App/ItemsList`), as `$e` names its commands.
|
|
178
|
+
- Every hook runs through `$e`'s hooks (`$e.hooks`), with the API it had: a command has one `$e` 'after' hook, registered once, which calls the hooks of the instance a run was on (`useCommandHook()`, `useChildCommandHook()`, `useCommand().hook()`, `useComponent().hook()`), found by its name, then the command's `globalHook()`s; `commandsManager.onBefore()`, `onBeforeUI()`, `onAfter()`, `onAfterUI()`, `onAfterOnce()` and `onAfterAffect()` are `$e` hooks on the command's name. A hook any other code registers in `$e` on the same name hears the same runs. A run's hook callbacks are called one after the other, each awaited, and the first that throws or rejects fails the run, as before.
|
|
179
|
+
- What it costs does not grow with the instances: a run finds its instance's hooks by name, a state change wakes only the instances whose own state changed, and a hook written inline is registered once for its instance, not at every render.
|
|
180
|
+
- An app that keeps its `$e` components itself (its user actions) declares one named as the React component, with the instances' slice in its store; react-commander then uses it, adds the commands to it and keeps the instances' state there, beside whatever else that store holds:
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
import { ComponentBase, instancesStore, withInstances, valueStore } from "@zenflux/commands";
|
|
184
|
+
|
|
185
|
+
class ItemsListComponent extends ComponentBase< ReturnType< typeof instancesStore > > {
|
|
186
|
+
getNamespace() { return "App/ItemsList"; }
|
|
187
|
+
defaultStore() { return instancesStore(); } // or withInstances( valueStore( { filter: "" } ) )
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
$e.components.register( new ItemsListComponent() ); // before the app renders
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
- Each instance's state is in its `$e` component's store (a reducer's state, @zenflux/commands' own: no @wordpress/data), by its unique name: `select( $e.components.get( "App/ItemsList" ).store ).getInstance( "App/ItemsList-:r1:" )`. The store's reducer finds what changed, so `useCommandState()` draws again only for a real change, and a selector's pick is kept while the state is the same.
|
|
194
|
+
- From outside a component, `useCommand()` finds its instance as it runs, one that is on the page first.
|
|
195
|
+
- `useSelect( ( selectFrom ) => selectFrom( store ).getX(), deps )` (from `hooks`) draws a component again as a store it read changes, for state outside an instance (an `$e` component's own); `useCommandState()` stays the hook for an instance's.
|
|
196
|
+
- A command added once its component's first instance mounted (a second `withCommands()` of the name, loaded later; a hot reload) is added to its `$e` component then.
|
|
197
|
+
- An instance's state stays across an unmount and mount of the same instance (StrictMode; a Suspense that hid it and shows it again).
|
|
198
|
+
- With `new API( { isDebug: true } )`, `$e.devTools` logs each run.
|
|
199
|
+
- React 18.3 or later, any bundler: the package reads vite's `import.meta.env` only where it is there.
|
|
200
|
+
|
|
160
201
|
## API Reference
|
|
161
202
|
|
|
162
203
|
### Commands
|
|
@@ -16,8 +16,6 @@ export const INTERNAL_ON_CONTEXT_STATE_UPDATED = `${ NAMESPACE }/__internalOnCon
|
|
|
16
16
|
|
|
17
17
|
export const INTERNAL_PROPS = `${ NAMESPACE }/__internalProps`;
|
|
18
18
|
|
|
19
|
-
export const INTERNAL_STATE_UPDATED_EVENT = `${ NAMESPACE }/__internalStateUpdated`;
|
|
20
|
-
|
|
21
19
|
export const DEBUG_ENABLED = ( () => {
|
|
22
20
|
if ( typeof window !== "undefined" ) {
|
|
23
21
|
const w = window as WindowWithDebug;
|
|
@@ -28,8 +26,22 @@ export const DEBUG_ENABLED = ( () => {
|
|
|
28
26
|
const meta = import.meta as ImportMeta & { env?: Record<string, string | undefined> };
|
|
29
27
|
if ( meta.env?.ZENFLUX_DEBUG !== undefined ) return !!meta.env.ZENFLUX_DEBUG;
|
|
30
28
|
}
|
|
31
|
-
if ( typeof process !== "undefined" && process.env
|
|
32
|
-
|
|
29
|
+
if ( typeof process !== "undefined" && process.env ) {
|
|
30
|
+
// Any name: a consumer's `ProcessEnv` may list only its own (`NODE_ENV`).
|
|
31
|
+
const env: Record<string, string | undefined> = { ... process.env };
|
|
32
|
+
if ( env.ZENFLUX_DEBUG !== undefined ) return !!env.ZENFLUX_DEBUG;
|
|
33
33
|
}
|
|
34
34
|
return false;
|
|
35
35
|
} )();
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* vite's `import.meta.env` and `import.meta.hot`, read where they may be missing: a bundler other than vite (webpack)
|
|
39
|
+
* gives neither, and a consumer's types may not declare them. Each read is written out as `import.meta.env` and
|
|
40
|
+
* `import.meta.hot`, which vite finds by their text to give them.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/** A development build: vite's `import.meta.env.DEV`. */
|
|
44
|
+
export const DEV_ENABLED = !! ( import.meta as ImportMeta & { env?: { DEV?: boolean } } ).env?.DEV;
|
|
45
|
+
|
|
46
|
+
/** vite's hot reload is on. */
|
|
47
|
+
export const HOT_RELOADING = !! ( import.meta as ImportMeta & { hot?: object } ).hot;
|
package/src/_internal/core.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
import {
|
|
3
3
|
DEBUG_ENABLED,
|
|
4
|
+
DEV_ENABLED,
|
|
4
5
|
GET_INTERNAL_MATCH_SYMBOL,
|
|
5
6
|
GET_INTERNAL_SYMBOL,
|
|
6
7
|
REGISTER_INTERNAL_SYMBOL,
|
|
@@ -10,7 +11,8 @@ import {
|
|
|
10
11
|
|
|
11
12
|
import { logger } from "./logger";
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
// The default export: a JSON module has no named ones to a bundler (webpack).
|
|
15
|
+
import packageJson from "../../package.json";
|
|
14
16
|
|
|
15
17
|
import type { DCoreContext, DCoreInterface, DCoreRegisterArgs } from "./definitions";
|
|
16
18
|
|
|
@@ -49,7 +51,7 @@ function setSortedContextKeys( keys: string[] ) {
|
|
|
49
51
|
* It provides methods to register, unregister, get and link components.
|
|
50
52
|
*/
|
|
51
53
|
class Core implements DCoreInterface {
|
|
52
|
-
public version = version;
|
|
54
|
+
public version = packageJson.version;
|
|
53
55
|
|
|
54
56
|
/**
|
|
55
57
|
* Registers the context for a React component.
|
|
@@ -203,13 +205,12 @@ class Core implements DCoreInterface {
|
|
|
203
205
|
return -1; // Symbol not found
|
|
204
206
|
}
|
|
205
207
|
|
|
206
|
-
|
|
207
|
-
public __devDebug( ... args: any[] ): void {
|
|
208
|
+
public __devDebug( ... _args: any[] ): void {
|
|
208
209
|
}
|
|
209
210
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
211
|
+
public __devGetContextLength!: () => number;
|
|
212
|
+
public __devGetContextKeys!: () => string[];
|
|
213
|
+
public __devGetContextValues!: () => DCommandSingleComponentContext[];
|
|
213
214
|
}
|
|
214
215
|
|
|
215
216
|
const CORE_INSTANCE_KEY = Symbol.for( "@zenflux/react-commander/core-instance" );
|
|
@@ -226,10 +227,14 @@ function getOrCreateCore(): DCoreInterface {
|
|
|
226
227
|
|
|
227
228
|
const core = getOrCreateCore();
|
|
228
229
|
|
|
229
|
-
if (
|
|
230
|
+
if ( DEV_ENABLED ) {
|
|
230
231
|
if ( DEBUG_ENABLED ) {
|
|
231
232
|
core.__devDebug = ( ... args: Parameters<DCoreInterface[ "__devDebug" ]> ) => {
|
|
232
|
-
|
|
233
|
+
// Spread into `debug( caller, message, ...params )`, whose message is not optional:
|
|
234
|
+
// the first argument was already filling it, and now says so.
|
|
235
|
+
const [ message = "", ... params ] = args;
|
|
236
|
+
|
|
237
|
+
logger.debug( core.__devDebug, String( message ), ... params );
|
|
233
238
|
};
|
|
234
239
|
}
|
|
235
240
|
|
package/src/command-base.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import EventEmitter from "eventemitter3";
|
|
2
|
-
|
|
3
1
|
import { CommandPublic } from "@zenflux/core/src/command-bases/command-public";
|
|
4
2
|
|
|
3
|
+
import commandsManager from "@zenflux/react-commander/commands-manager";
|
|
4
|
+
|
|
5
|
+
import type EventEmitter from "eventemitter3";
|
|
6
|
+
|
|
5
7
|
import type { DCommandOptions, DCommandArgs, DCommandRegisterArgs } from "@zenflux/react-commander/definitions";
|
|
6
8
|
|
|
7
9
|
import type React from "react";
|
|
@@ -15,8 +17,6 @@ import type React from "react";
|
|
|
15
17
|
* - `CommandPublic`: semantic marker for user-facing commands
|
|
16
18
|
*/
|
|
17
19
|
export abstract class CommandBase<TState = React.ComponentState, TArgs = DCommandArgs> extends CommandPublic {
|
|
18
|
-
private static globalEmitter: EventEmitter = new EventEmitter();
|
|
19
|
-
|
|
20
20
|
public readonly commandName: string;
|
|
21
21
|
|
|
22
22
|
private reactOptions: DCommandOptions<TState> = {};
|
|
@@ -29,21 +29,22 @@ export abstract class CommandBase<TState = React.ComponentState, TArgs = DComman
|
|
|
29
29
|
return "@zenflux/react-commander";
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Hooks the command on every instance: an `$e` 'after' hook on its name.
|
|
34
|
+
*/
|
|
32
35
|
public static globalHook( callback: ( result?: any, args?: DCommandArgs ) => any ) {
|
|
33
|
-
|
|
36
|
+
commandsManager.hookGlobal( this.getName(), callback );
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
public static globalUnhook() {
|
|
37
|
-
|
|
38
|
-
this.globalEmitter.off( this.getName(), listener );
|
|
39
|
-
} );
|
|
40
|
+
commandsManager.unhookGlobal( this.getName() );
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
/**
|
|
43
44
|
* React-commander creates command instances during registration (not per-execution),
|
|
44
45
|
* so we pass empty args/options to core's CommandBase constructor.
|
|
45
46
|
*/
|
|
46
|
-
public constructor(
|
|
47
|
+
public constructor( _registerArgs: DCommandRegisterArgs ) {
|
|
47
48
|
super( {}, {} );
|
|
48
49
|
|
|
49
50
|
this.commandName = ( new.target as typeof CommandBase ).getName();
|
|
@@ -55,7 +56,6 @@ export abstract class CommandBase<TState = React.ComponentState, TArgs = DComman
|
|
|
55
56
|
return global as unknown as {
|
|
56
57
|
getName: typeof CommandBase.getName;
|
|
57
58
|
|
|
58
|
-
globalEmitter: EventEmitter;
|
|
59
59
|
globalHook: typeof CommandBase.globalHook;
|
|
60
60
|
globalUnhook: typeof CommandBase.globalUnhook;
|
|
61
61
|
};
|
|
@@ -82,31 +82,25 @@ export abstract class CommandBase<TState = React.ComponentState, TArgs = DComman
|
|
|
82
82
|
// Core lifecycle: after
|
|
83
83
|
this.onAfterApply?.();
|
|
84
84
|
|
|
85
|
+
// Listeners on the emitter it was given: a caller's own (the hooks are `$e`'s, run by the manager's run).
|
|
85
86
|
const listeners = emitter.listeners( this.commandName );
|
|
86
87
|
for ( const listener of listeners ) {
|
|
87
88
|
await listener( result, args );
|
|
88
89
|
}
|
|
89
90
|
|
|
90
|
-
const globalListeners = this.global().globalEmitter.listeners( this.commandName );
|
|
91
|
-
for ( const globalListener of globalListeners ) {
|
|
92
|
-
await globalListener( result, args );
|
|
93
|
-
}
|
|
94
|
-
|
|
95
91
|
this.reactOptions = {};
|
|
96
92
|
|
|
97
93
|
return result;
|
|
98
94
|
}
|
|
99
95
|
|
|
100
|
-
|
|
101
|
-
protected validateArgs?( args: TArgs, options?: DCommandOptions<TState> ) {
|
|
96
|
+
protected validateArgs?( _args: TArgs, _options?: DCommandOptions<TState> ) {
|
|
102
97
|
}
|
|
103
98
|
|
|
104
99
|
/**
|
|
105
100
|
* Override core's `apply()` (which throws `ForceMethodImplementation`) to be
|
|
106
101
|
* optional — react-commander commands may or may not implement apply.
|
|
107
102
|
*/
|
|
108
|
-
|
|
109
|
-
protected apply( args?: any, _options?: any ): Promise<any> | any {
|
|
103
|
+
protected apply( _args?: any, _options?: any ): Promise<any> | any {
|
|
110
104
|
}
|
|
111
105
|
|
|
112
106
|
protected get state(): TState {
|