@zenflux/react-commander 0.0.16 → 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/LICENSE +21 -0
- package/package.json +7 -8
- package/readme.md +42 -1
- package/src/_internal/command-not-found.ts +8 -0
- package/src/_internal/constants.ts +16 -4
- package/src/_internal/core.ts +8 -6
- package/src/command-base.ts +23 -30
- package/src/commands-manager.ts +506 -140
- package/src/definitions.ts +5 -0
- 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/with-commands.tsx +58 -78
- package/src/vite-env.d.ts +0 -6
package/src/commands-manager.ts
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
|
|
2
|
-
import {
|
|
2
|
+
import { $e, API, CommandBase as ManagedCommandBase, ComponentBase, dispatch, hookData, hookUI, instancesStore, select, subscribe } from "@zenflux/commands";
|
|
3
3
|
|
|
4
|
-
import { CommandNotFound } from "
|
|
5
|
-
|
|
6
|
-
import { DEBUG_ENABLED, GET_INTERNAL_SYMBOL } from "./_internal/constants";
|
|
4
|
+
import { CommandNotFound } from "./_internal/command-not-found";
|
|
5
|
+
import { DEBUG_ENABLED, DEV_ENABLED, GET_INTERNAL_SYMBOL, HOT_RELOADING } from "./_internal/constants";
|
|
7
6
|
import { logger } from "./_internal/logger";
|
|
8
7
|
|
|
9
|
-
import core from "./_internal/core
|
|
8
|
+
import core from "./_internal/core";
|
|
10
9
|
|
|
11
|
-
import type {
|
|
10
|
+
import type { CommandArgs, InstancesStoreConfig, SystemArgs } from "@zenflux/commands";
|
|
12
11
|
|
|
13
12
|
import type { CommandBase } from "@zenflux/react-commander/command-base";
|
|
14
|
-
import type { CommandBase as CoreCommandBase } from "@zenflux/core/src/command-bases/command-base";
|
|
15
13
|
|
|
16
14
|
import type {
|
|
17
15
|
DCommandArgs,
|
|
16
|
+
DCommandCallback,
|
|
18
17
|
DCommandHookHandle,
|
|
19
18
|
DCommandIdArgs,
|
|
20
19
|
DCommandNewInstanceWithArgs,
|
|
@@ -23,32 +22,62 @@ import type {
|
|
|
23
22
|
} from "@zenflux/react-commander/definitions";
|
|
24
23
|
|
|
25
24
|
/**
|
|
26
|
-
*
|
|
25
|
+
* The args `$e` runs a react-commander command with: its own, and the instance it runs on.
|
|
27
26
|
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return "@zenflux/react-commander/CoreCommandsAdapter";
|
|
31
|
-
}
|
|
27
|
+
interface ManagedArgs extends SystemArgs {
|
|
28
|
+
componentNameUnique: string;
|
|
32
29
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
30
|
+
[ key: string ]: unknown;
|
|
31
|
+
}
|
|
36
32
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
/**
|
|
34
|
+
* An `$e` component that keeps its React component's instances' state: its store has the instances' slice.
|
|
35
|
+
*/
|
|
36
|
+
type InstancesComponent = ComponentBase<InstancesStoreConfig>;
|
|
40
37
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
/**
|
|
39
|
+
* @return whether a component's store has the instances' slice (`withInstances()`), by its actions.
|
|
40
|
+
*/
|
|
41
|
+
const keepsInstances = ( component: ComponentBase ): component is InstancesComponent =>
|
|
42
|
+
!! component.storeConfig?.actions && "patchInstance" in component.storeConfig.actions && "removeInstance" in component.storeConfig.actions;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @return the `$e` API the commands run through: the app's, or one made here for an app that made none.
|
|
46
|
+
*/
|
|
47
|
+
function managerAPI(): API {
|
|
48
|
+
return $e ?? new API();
|
|
44
49
|
}
|
|
45
50
|
|
|
51
|
+
/**
|
|
52
|
+
* A hook on an instance's command: who owns it (a scoped hook's), and what it calls.
|
|
53
|
+
*/
|
|
54
|
+
type InstanceHook = {
|
|
55
|
+
ownerId?: string;
|
|
56
|
+
callback: ( result?: any, args?: DCommandArgs ) => any;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** The instance a run's args name. */
|
|
60
|
+
const instanceOf = ( args: object ): string | undefined =>
|
|
61
|
+
"componentNameUnique" in args && typeof args.componentNameUnique === "string" ? args.componentNameUnique : undefined;
|
|
62
|
+
|
|
63
|
+
/** A run's args as the command took them: without the instance `$e` ran it on. */
|
|
64
|
+
const ownArgsOf = ( { componentNameUnique: _instance, ... ownArgs }: CommandArgs & { componentNameUnique?: unknown } ): DCommandArgs => ownArgs;
|
|
65
|
+
|
|
66
|
+
/** How many hooks were registered on `$e` so far: each takes a new id. */
|
|
67
|
+
let hooksCount = 0;
|
|
68
|
+
|
|
69
|
+
const hookId = ( kind: string, command: string ) => `@zenflux/react-commander/${ kind }/${ command }/${ ++ hooksCount }`;
|
|
70
|
+
|
|
46
71
|
class CommandsManager {
|
|
47
72
|
/**
|
|
48
|
-
*
|
|
49
|
-
* (onBefore, onAfter, onAfterUI, onAfterOnce, onAfterAffect).
|
|
73
|
+
* The components whose commands `$e` runs already: each its own `$e` component (manage()).
|
|
50
74
|
*/
|
|
51
|
-
private
|
|
75
|
+
private managed = new Set<string>();
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Those `$e` components, by name: each keeps its instances' state in its store.
|
|
79
|
+
*/
|
|
80
|
+
private components: { [ componentName: string ]: InstancesComponent } = {};
|
|
52
81
|
|
|
53
82
|
private commands: {
|
|
54
83
|
[ componentName: string ]: {
|
|
@@ -56,12 +85,31 @@ class CommandsManager {
|
|
|
56
85
|
};
|
|
57
86
|
} = {};
|
|
58
87
|
|
|
59
|
-
|
|
88
|
+
/**
|
|
89
|
+
* The hooks on instances' commands: by instance, then command. A run finds its own by those two names, however
|
|
90
|
+
* many instances there are.
|
|
91
|
+
*/
|
|
92
|
+
private instanceHooks: {
|
|
60
93
|
[ componentNameUnique: string ]: {
|
|
61
|
-
[ commandName: string ]:
|
|
94
|
+
[ commandName: string ]: InstanceHook[]
|
|
62
95
|
}
|
|
63
96
|
} = {};
|
|
64
97
|
|
|
98
|
+
/**
|
|
99
|
+
* The hooks on a command of any instance (`CommandBase.globalHook()`), by command.
|
|
100
|
+
*/
|
|
101
|
+
private globalHooks: { [ commandName: string ]: Array<( result?: any, args?: DCommandArgs ) => any> } = {};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Who draws from an instance's state (`useCommandState()`): by component, then instance.
|
|
105
|
+
*/
|
|
106
|
+
private stateListeners: { [ componentName: string ]: { [ componentNameUnique: string ]: Set<() => void> } } = {};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The commands that have their `$e` hook: one 'after' hook each, registered once, which calls the hooks above.
|
|
110
|
+
*/
|
|
111
|
+
private hookedCommands = new Set<string>();
|
|
112
|
+
|
|
65
113
|
private commandToComponentMap: {
|
|
66
114
|
[ commandName: string ]: string;
|
|
67
115
|
} = {};
|
|
@@ -69,9 +117,6 @@ class CommandsManager {
|
|
|
69
117
|
public register( args: DCommandRegisterArgs ) {
|
|
70
118
|
const { componentName, commands } = args;
|
|
71
119
|
|
|
72
|
-
if ( this.commands[ componentName ] ) {
|
|
73
|
-
}
|
|
74
|
-
|
|
75
120
|
const createdCommands: CommandBase[] = [];
|
|
76
121
|
|
|
77
122
|
if ( ! this.commands[ componentName ] ) {
|
|
@@ -88,44 +133,240 @@ class CommandsManager {
|
|
|
88
133
|
createdCommands.push( commandInstance );
|
|
89
134
|
} );
|
|
90
135
|
|
|
136
|
+
// Commands that come once the component is `$e`'s (a second `withCommands()` of the name, loaded later, or
|
|
137
|
+
// a hot reload) are added to its `$e` component, as its first ones were.
|
|
138
|
+
const component = this.components[ componentName ];
|
|
139
|
+
|
|
140
|
+
if ( component ) {
|
|
141
|
+
const known = new Set( managerAPI().commands.getAll() );
|
|
142
|
+
|
|
143
|
+
Object.keys( this.commands[ componentName ] )
|
|
144
|
+
.filter( ( commandName ) => ! known.has( commandName ) )
|
|
145
|
+
.forEach( ( commandName ) => component.registerCommand( commandName, this.managedCommand( componentName, commandName ) ) );
|
|
146
|
+
}
|
|
147
|
+
|
|
91
148
|
return createdCommands;
|
|
92
149
|
}
|
|
93
150
|
|
|
94
|
-
|
|
151
|
+
/**
|
|
152
|
+
* The `$e` command of a react-commander command: it runs `execute()` on the instance its args name.
|
|
153
|
+
*/
|
|
154
|
+
private managedCommand( componentName: string, commandName: string ) {
|
|
155
|
+
const manager = this;
|
|
156
|
+
|
|
157
|
+
return class extends ManagedCommandBase<ManagedArgs, Promise<unknown>> {
|
|
158
|
+
public static getName() {
|
|
159
|
+
return commandName;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public apply( { componentNameUnique, ... args }: ManagedArgs ) {
|
|
163
|
+
return manager.execute( { commandName, componentName, componentNameUnique }, args );
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Runs a command on a component's instance, through `$e`: the command `manage()` registered for it there, with the
|
|
170
|
+
* instance in its args, so `$e`'s hooks and dev tools see it as every other `$e` command. That command calls
|
|
171
|
+
* `execute()`, which is react-commander's own run of it.
|
|
172
|
+
*/
|
|
173
|
+
public run( id: DCommandIdArgs, args: DCommandArgs, callback?: ( result: any ) => any ): Promise<any> {
|
|
174
|
+
const { componentNameUnique, componentName, commandName } = id;
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
if ( ! this.commands[ componentName ]?.[ commandName ] ) {
|
|
178
|
+
throw new CommandNotFound( commandName );
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if ( DEBUG_ENABLED ) logger.debug( this.run, `Commands.run() '${ commandName }' for component '${ componentNameUnique }'`, args );
|
|
182
|
+
|
|
183
|
+
this.manage( componentName );
|
|
184
|
+
|
|
185
|
+
const managedArgs: ManagedArgs = { ... args, componentNameUnique };
|
|
186
|
+
|
|
187
|
+
// `$e`'s promise itself (or a sync command's result, made one): no `async` layer around it.
|
|
188
|
+
const result = Promise.resolve( managerAPI().commands.run( commandName, managedArgs ) );
|
|
189
|
+
|
|
190
|
+
return callback ? result.then( ( value ) => {
|
|
191
|
+
callback( value );
|
|
192
|
+
|
|
193
|
+
return value;
|
|
194
|
+
} ) : result;
|
|
195
|
+
} catch( e ) {
|
|
196
|
+
// Fails as a promise, as an `async` run did.
|
|
197
|
+
return Promise.reject( e );
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Runs a command on a component's instance, with the instance's state: what `$e` calls as it runs the command.
|
|
203
|
+
*/
|
|
204
|
+
public execute( id: DCommandIdArgs, args: DCommandArgs ): Promise<any> {
|
|
95
205
|
const { componentNameUnique, componentName, commandName } = id;
|
|
96
206
|
|
|
97
207
|
const command = this.commands[ componentName ]?.[ commandName ];
|
|
98
208
|
|
|
99
209
|
if ( ! command ) {
|
|
100
|
-
|
|
210
|
+
return Promise.reject( new CommandNotFound( commandName ) );
|
|
101
211
|
}
|
|
102
212
|
|
|
103
|
-
|
|
213
|
+
try {
|
|
214
|
+
const singleComponentContext = core[ GET_INTERNAL_SYMBOL ]( componentNameUnique );
|
|
104
215
|
|
|
105
|
-
|
|
106
|
-
|
|
216
|
+
if ( singleComponentContext.getState ) {
|
|
217
|
+
return command.execute( singleComponentContext.emitter, args, {
|
|
218
|
+
state: singleComponentContext.getState(),
|
|
219
|
+
setState: singleComponentContext.setState,
|
|
220
|
+
} );
|
|
221
|
+
}
|
|
107
222
|
|
|
108
|
-
|
|
223
|
+
return command.execute( singleComponentContext.emitter, args );
|
|
224
|
+
} catch( e ) {
|
|
225
|
+
return Promise.reject( e );
|
|
226
|
+
}
|
|
227
|
+
}
|
|
109
228
|
|
|
110
|
-
|
|
229
|
+
/**
|
|
230
|
+
* Makes a component's commands `$e` commands, once: an `$e` component named as the React component, whose commands
|
|
231
|
+
* run `execute()` on the instance their args name (`componentNameUnique`). From then on they run from anywhere
|
|
232
|
+
* `$e` does: `$e.run( "App/ItemsList/SetName", { componentNameUnique, id, name } )`.
|
|
233
|
+
*/
|
|
234
|
+
public manage( componentName: string ) {
|
|
235
|
+
if ( this.managed.has( componentName ) || ! this.commands[ componentName ] ) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
111
238
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
239
|
+
const managedCommands = Object.fromEntries( Object.keys( this.commands[ componentName ] ).map( ( commandName ) => [
|
|
240
|
+
commandName,
|
|
241
|
+
this.managedCommand( componentName, commandName ),
|
|
242
|
+
] ) );
|
|
243
|
+
|
|
244
|
+
const api = managerAPI();
|
|
245
|
+
|
|
246
|
+
// The app's own component of that name (its user-actions'), when it registered one: the commands are added to
|
|
247
|
+
// it and the instances' state is a slice of its store. Else one made here, which holds nothing more.
|
|
248
|
+
if ( api.components.getAll().includes( componentName ) ) {
|
|
249
|
+
const component: ComponentBase = api.components.get( componentName as never );
|
|
250
|
+
|
|
251
|
+
if ( ! keepsInstances( component ) ) {
|
|
252
|
+
throw new Error( `The '${ componentName }' component's store has no instances' slice: wrap its defaultStore() with withInstances()` );
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
Object.entries( managedCommands ).forEach( ( [ commandName, Command ] ) => component.registerCommand( commandName, Command ) );
|
|
256
|
+
|
|
257
|
+
this.components[ componentName ] = component;
|
|
117
258
|
} else {
|
|
118
|
-
|
|
259
|
+
this.components[ componentName ] = api.components.register( new ( class extends ComponentBase<InstancesStoreConfig> {
|
|
260
|
+
public static getName() {
|
|
261
|
+
return componentName;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
public getNamespace() {
|
|
265
|
+
return componentName;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
public defaultCommands() {
|
|
269
|
+
return managedCommands;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
public defaultStore() {
|
|
273
|
+
return instancesStore();
|
|
274
|
+
}
|
|
275
|
+
} )() );
|
|
119
276
|
}
|
|
120
277
|
|
|
121
|
-
|
|
122
|
-
|
|
278
|
+
this.managed.add( componentName );
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* An instance's state, from its `$e` component's store: `initial` until it was first set.
|
|
283
|
+
*/
|
|
284
|
+
public getState<TState extends object>( componentName: string, componentNameUnique: string, initial: TState ): TState {
|
|
285
|
+
const component = this.components[ componentName ];
|
|
286
|
+
|
|
287
|
+
// What the instance's commands and `setState()` put there: its own TState, which the store keeps as an object.
|
|
288
|
+
return ( ( component && select( component.store ).getInstance( componentNameUnique ) ) ?? initial ) as TState;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Sets some of an instance's state, in its `$e` component's store.
|
|
293
|
+
*
|
|
294
|
+
* @return whether anything changed: the store's answer, by the state it now holds.
|
|
295
|
+
*/
|
|
296
|
+
public setState<TState extends object>( componentName: string, componentNameUnique: string, patch: object, initial: TState ): boolean {
|
|
297
|
+
const component = this.components[ componentName ];
|
|
123
298
|
|
|
124
|
-
if (
|
|
125
|
-
|
|
299
|
+
if ( ! component ) {
|
|
300
|
+
throw new Error( `Component '${ componentName }' is not managed yet` );
|
|
126
301
|
}
|
|
127
302
|
|
|
128
|
-
|
|
303
|
+
const previous = this.getState( componentName, componentNameUnique, initial );
|
|
304
|
+
|
|
305
|
+
dispatch( component.store ).patchInstance( componentNameUnique, patch, initial );
|
|
306
|
+
|
|
307
|
+
return previous !== this.getState( componentName, componentNameUnique, initial );
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Drops an instance's state, as it unmounts.
|
|
312
|
+
*/
|
|
313
|
+
public removeState( componentName: string, componentNameUnique: string ) {
|
|
314
|
+
const component = this.components[ componentName ];
|
|
315
|
+
|
|
316
|
+
if ( component ) {
|
|
317
|
+
dispatch( component.store ).removeInstance( componentNameUnique );
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Calls the listener as the state of one instance changes. The component's store is listened to once, and each
|
|
323
|
+
* change of it wakes only the instances whose own state is another object than before, not every instance that
|
|
324
|
+
* reads the store.
|
|
325
|
+
*
|
|
326
|
+
* @return what stops it.
|
|
327
|
+
*/
|
|
328
|
+
public subscribeState( componentName: string, componentNameUnique: string, listener: () => void ): () => void {
|
|
329
|
+
const component = this.components[ componentName ];
|
|
330
|
+
|
|
331
|
+
if ( ! component ) {
|
|
332
|
+
throw new Error( `Component '${ componentName }' is not managed yet` );
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if ( ! this.stateListeners[ componentName ] ) {
|
|
336
|
+
const listeners: { [ componentNameUnique: string ]: Set<() => void> } = this.stateListeners[ componentName ] = {};
|
|
337
|
+
|
|
338
|
+
let last = select( component.store ).getInstances();
|
|
339
|
+
|
|
340
|
+
subscribe( () => {
|
|
341
|
+
const next = select( component.store ).getInstances();
|
|
342
|
+
|
|
343
|
+
if ( next === last ) return;
|
|
344
|
+
|
|
345
|
+
const previous = last;
|
|
346
|
+
|
|
347
|
+
last = next;
|
|
348
|
+
|
|
349
|
+
for ( const instance in listeners ) {
|
|
350
|
+
if ( next[ instance ] !== previous[ instance ] ) {
|
|
351
|
+
// A copy: a listener may stop itself as it is called.
|
|
352
|
+
[ ... listeners[ instance ] ].forEach( ( wake ) => wake() );
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}, component.store );
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const listeners = this.stateListeners[ componentName ],
|
|
359
|
+
ofInstance = listeners[ componentNameUnique ] ??= new Set();
|
|
360
|
+
|
|
361
|
+
ofInstance.add( listener );
|
|
362
|
+
|
|
363
|
+
return () => {
|
|
364
|
+
ofInstance.delete( listener );
|
|
365
|
+
|
|
366
|
+
if ( ! ofInstance.size && listeners[ componentNameUnique ] === ofInstance ) {
|
|
367
|
+
delete listeners[ componentNameUnique ];
|
|
368
|
+
}
|
|
369
|
+
};
|
|
129
370
|
}
|
|
130
371
|
|
|
131
372
|
public unregister( componentName: string ) {
|
|
@@ -141,6 +382,94 @@ class CommandsManager {
|
|
|
141
382
|
delete this.commands[ componentName ];
|
|
142
383
|
}
|
|
143
384
|
|
|
385
|
+
/**
|
|
386
|
+
* Gives a command its `$e` 'after' hook, once: after each run it calls the hooks of the instance the run was on,
|
|
387
|
+
* found by its name, then the command's global hooks, with the run's result and own args. Adding or taking back a
|
|
388
|
+
* hook is then a change to a list here, and `$e` runs one hook per command however many instances listen.
|
|
389
|
+
*/
|
|
390
|
+
private hookCommand( commandName: string ) {
|
|
391
|
+
if ( this.hookedCommands.has( commandName ) ) {
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
this.hookedCommands.add( commandName );
|
|
396
|
+
|
|
397
|
+
const manager = this;
|
|
398
|
+
|
|
399
|
+
managerAPI().hooks.registerDataAfter( new ( class extends hookData.After {
|
|
400
|
+
public getCommand() { return commandName; }
|
|
401
|
+
|
|
402
|
+
public getId() { return `@zenflux/react-commander/after/${ commandName }`; }
|
|
403
|
+
|
|
404
|
+
public apply( args: CommandArgs, result: unknown ) {
|
|
405
|
+
const instance = instanceOf( args ),
|
|
406
|
+
instanceHooks = instance ? manager.instanceHooks[ instance ]?.[ commandName ] : undefined,
|
|
407
|
+
globalHooks = manager.globalHooks[ commandName ];
|
|
408
|
+
|
|
409
|
+
if ( ! instanceHooks?.length && ! globalHooks?.length ) {
|
|
410
|
+
return true;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const ownArgs = ownArgsOf( args ),
|
|
414
|
+
// Copies: a callback may add or take back a hook.
|
|
415
|
+
callbacks: Array<( result?: any, args?: DCommandArgs ) => any> = [];
|
|
416
|
+
|
|
417
|
+
if ( instanceHooks ) {
|
|
418
|
+
for ( const hook of instanceHooks ) callbacks.push( hook.callback );
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if ( globalHooks ) {
|
|
422
|
+
for ( const callback of globalHooks ) callbacks.push( callback );
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// The callbacks one after the other, each awaited when it answers with a promise, as react-commander's
|
|
426
|
+
// were: the run waits for what this answers, and fails on the first that throws or rejects. Plain
|
|
427
|
+
// callbacks are called right away, with no promise for `$e` to wait for.
|
|
428
|
+
const callFrom = ( index: number ): true | Promise<true> => {
|
|
429
|
+
for ( let i = index; i < callbacks.length; i++ ) {
|
|
430
|
+
const answer = callbacks[ i ]( result, ownArgs );
|
|
431
|
+
|
|
432
|
+
if ( answer instanceof Promise ) {
|
|
433
|
+
return answer.then( () => callFrom( i + 1 ) );
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
return true;
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
try {
|
|
441
|
+
return callFrom( 0 );
|
|
442
|
+
} catch( e ) {
|
|
443
|
+
// A rejection `$e` waits for: a throw here it would only log.
|
|
444
|
+
return Promise.reject( e );
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
} )() );
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
private addInstanceHook( id: DCommandIdArgs, callback: ( result?: any, args?: DCommandArgs ) => any, ownerId?: string ) {
|
|
451
|
+
const { componentNameUnique, commandName } = id;
|
|
452
|
+
|
|
453
|
+
this.hookCommand( commandName );
|
|
454
|
+
|
|
455
|
+
const hook: InstanceHook = { ownerId, callback };
|
|
456
|
+
|
|
457
|
+
( ( this.instanceHooks[ componentNameUnique ] ??= {} )[ commandName ] ??= [] ).push( hook );
|
|
458
|
+
|
|
459
|
+
return hook;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
private removeInstanceHooks( componentNameUnique: string, commandName: string, remove: ( hook: InstanceHook ) => boolean ) {
|
|
463
|
+
const hooks = this.instanceHooks[ componentNameUnique ]?.[ commandName ];
|
|
464
|
+
|
|
465
|
+
if ( ! hooks ) return;
|
|
466
|
+
|
|
467
|
+
this.instanceHooks[ componentNameUnique ][ commandName ] = hooks.filter( ( hook ) => ! remove( hook ) );
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Hooks a command of one instance: called after that instance's runs, by the command's `$e` 'after' hook.
|
|
472
|
+
*/
|
|
144
473
|
public hook( id: DCommandIdArgs, callback: ( result?: any, args?: DCommandArgs ) => any, options?: {
|
|
145
474
|
__ignoreDuplicatedHookError?: boolean;
|
|
146
475
|
} ) {
|
|
@@ -157,11 +486,12 @@ class CommandsManager {
|
|
|
157
486
|
throw new CommandNotFound( commandName );
|
|
158
487
|
}
|
|
159
488
|
|
|
160
|
-
const
|
|
489
|
+
const hooks = this.instanceHooks[ componentNameUnique ]?.[ commandName ] ?? [];
|
|
161
490
|
|
|
162
491
|
if ( ! options?.__ignoreDuplicatedHookError ) {
|
|
163
492
|
// Check if the same callback is already registered
|
|
164
|
-
|
|
493
|
+
// Among the plain hooks only, as before: a scoped hook (useCommand's) is its owner's, whatever its name.
|
|
494
|
+
if ( hooks.find( ( hook ) => ! hook.ownerId && hook.callback.name === callback.name ) ) {
|
|
165
495
|
logger.warn( this.hook,
|
|
166
496
|
`Probably duplicated hook in '${ commandName }'\n` +
|
|
167
497
|
`callback '${ callback.name }()' already hooked for component '${ componentNameUnique }'` +
|
|
@@ -172,15 +502,13 @@ class CommandsManager {
|
|
|
172
502
|
}
|
|
173
503
|
}
|
|
174
504
|
|
|
175
|
-
|
|
505
|
+
const hook = this.addInstanceHook( id, callback );
|
|
176
506
|
|
|
177
507
|
return {
|
|
178
508
|
componentNameUnique,
|
|
179
509
|
commandName,
|
|
180
510
|
callback,
|
|
181
|
-
dispose: () =>
|
|
182
|
-
singleComponentContext.emitter.off( commandName, callback );
|
|
183
|
-
}
|
|
511
|
+
dispose: () => this.removeInstanceHooks( componentNameUnique, commandName, ( candidate ) => candidate === hook ),
|
|
184
512
|
};
|
|
185
513
|
}
|
|
186
514
|
|
|
@@ -196,13 +524,8 @@ class CommandsManager {
|
|
|
196
524
|
if ( ! singleComponentContext.commands[ commandName ] ) {
|
|
197
525
|
throw new CommandNotFound( commandName );
|
|
198
526
|
}
|
|
199
|
-
const wrapped = ( result?: any, args?: DCommandArgs ) => callback( result, args );
|
|
200
|
-
singleComponentContext.emitter.on( commandName, wrapped );
|
|
201
|
-
|
|
202
|
-
if ( ! this.scopedHooks[ componentNameUnique ] ) this.scopedHooks[ componentNameUnique ] = {} as any;
|
|
203
|
-
if ( ! this.scopedHooks[ componentNameUnique ][ commandName ] ) this.scopedHooks[ componentNameUnique ][ commandName ] = [];
|
|
204
527
|
|
|
205
|
-
this.
|
|
528
|
+
this.addInstanceHook( id, callback, ownerId );
|
|
206
529
|
|
|
207
530
|
const handle: DCommandHookHandle = {
|
|
208
531
|
componentNameUnique,
|
|
@@ -222,7 +545,7 @@ class CommandsManager {
|
|
|
222
545
|
}
|
|
223
546
|
|
|
224
547
|
// If HMR is active, suppress errors for missing contexts during hot reload
|
|
225
|
-
const shouldSilentError =
|
|
548
|
+
const shouldSilentError = HOT_RELOADING;
|
|
226
549
|
|
|
227
550
|
const singleComponentContext = core[ GET_INTERNAL_SYMBOL ]( componentNameUnique, shouldSilentError ) as DCommandSingleComponentContext;
|
|
228
551
|
|
|
@@ -235,41 +558,37 @@ class CommandsManager {
|
|
|
235
558
|
throw new CommandNotFound( commandName );
|
|
236
559
|
}
|
|
237
560
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
if ( this.scopedHooks[ componentNameUnique ] && this.scopedHooks[ componentNameUnique ][ commandName ] ) {
|
|
241
|
-
delete this.scopedHooks[ componentNameUnique ][ commandName ];
|
|
242
|
-
}
|
|
561
|
+
this.removeInstanceHooks( componentNameUnique, commandName, () => true );
|
|
243
562
|
}
|
|
244
563
|
|
|
245
564
|
public unhookHandle( handle: DCommandHookHandle ) {
|
|
246
565
|
const { componentNameUnique, commandName, ownerId } = handle;
|
|
247
|
-
const singleComponentContext = core[ GET_INTERNAL_SYMBOL ]( componentNameUnique, true ) as DCommandSingleComponentContext;
|
|
248
|
-
if ( ! singleComponentContext ) return;
|
|
249
|
-
|
|
250
|
-
const records = this.scopedHooks[ componentNameUnique ]?.[ commandName ];
|
|
251
|
-
if ( ! records ) return;
|
|
252
|
-
|
|
253
|
-
const remaining: typeof records = [];
|
|
254
|
-
records.forEach( ( record ) => {
|
|
255
|
-
if ( record.ownerId === ownerId ) {
|
|
256
|
-
singleComponentContext.emitter.off( commandName, record.wrapped );
|
|
257
|
-
} else {
|
|
258
|
-
remaining.push( record );
|
|
259
|
-
}
|
|
260
|
-
} );
|
|
261
566
|
|
|
262
|
-
this.
|
|
567
|
+
this.removeInstanceHooks( componentNameUnique, commandName, ( hook ) => hook.ownerId === ownerId );
|
|
263
568
|
}
|
|
264
569
|
|
|
570
|
+
/**
|
|
571
|
+
* Takes back every hook on an instance's commands: as it unmounts.
|
|
572
|
+
*/
|
|
265
573
|
public unhookWithinComponent( componentNameUnique: string ) {
|
|
266
|
-
|
|
574
|
+
Object.keys( this.instanceHooks[ componentNameUnique ] ?? {} ).forEach( ( commandName ) =>
|
|
575
|
+
this.removeInstanceHooks( componentNameUnique, commandName, () => true ) );
|
|
267
576
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
577
|
+
delete this.instanceHooks[ componentNameUnique ];
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* Hooks a command of every instance (`CommandBase.globalHook()`): called after any run of it, by the command's
|
|
582
|
+
* `$e` 'after' hook.
|
|
583
|
+
*/
|
|
584
|
+
public hookGlobal( commandName: string, callback: ( result?: any, args?: DCommandArgs ) => any ) {
|
|
585
|
+
this.hookCommand( commandName );
|
|
586
|
+
|
|
587
|
+
( this.globalHooks[ commandName ] ??= [] ).push( callback );
|
|
588
|
+
}
|
|
271
589
|
|
|
272
|
-
|
|
590
|
+
public unhookGlobal( commandName: string ) {
|
|
591
|
+
delete this.globalHooks[ commandName ];
|
|
273
592
|
}
|
|
274
593
|
|
|
275
594
|
public get( componentName: string, shouldSilentError = false ) {
|
|
@@ -289,17 +608,7 @@ class CommandsManager {
|
|
|
289
608
|
}
|
|
290
609
|
|
|
291
610
|
public isHooked( id: DCommandIdArgs ) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
const singleComponentContext = core[ GET_INTERNAL_SYMBOL ]( componentNameUnique, true ) as DCommandSingleComponentContext;
|
|
295
|
-
|
|
296
|
-
if ( ! singleComponentContext ) {
|
|
297
|
-
return false;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
const listeners = singleComponentContext.emitter.listeners( commandName );
|
|
301
|
-
|
|
302
|
-
return listeners.length > 0;
|
|
611
|
+
return !! this.instanceHooks[ id.componentNameUnique ]?.[ id.commandName ]?.length;
|
|
303
612
|
}
|
|
304
613
|
|
|
305
614
|
public resolveId( commandName: string, componentName: string, index = 0 ): DCommandIdArgs {
|
|
@@ -324,48 +633,127 @@ class CommandsManager {
|
|
|
324
633
|
return !! core[ GET_INTERNAL_SYMBOL ]( componentNameUnique, true );
|
|
325
634
|
}
|
|
326
635
|
|
|
327
|
-
// ---
|
|
636
|
+
// --- Hooks on any command, through `$e`'s hooks ---
|
|
328
637
|
|
|
329
638
|
/**
|
|
330
|
-
* Register a data-only before hook for a command.
|
|
639
|
+
* Register a data-only before hook for a command: called with its args, before it applies.
|
|
331
640
|
*/
|
|
332
|
-
public onBefore( hookCommand: string, callback:
|
|
333
|
-
|
|
641
|
+
public onBefore( hookCommand: string, callback: DCommandCallback ) {
|
|
642
|
+
// Outside the class: HookBase reads `getId()` in its constructor, before a subclass's fields are set.
|
|
643
|
+
const id = hookId( "before", hookCommand );
|
|
644
|
+
|
|
645
|
+
managerAPI().hooks.registerDataDependency( new ( class extends hookData.Dependency {
|
|
646
|
+
public getCommand() { return hookCommand; }
|
|
647
|
+
|
|
648
|
+
public getId() { return id; }
|
|
649
|
+
|
|
650
|
+
public apply( args: CommandArgs ) {
|
|
651
|
+
callback( args );
|
|
652
|
+
|
|
653
|
+
return true;
|
|
654
|
+
}
|
|
655
|
+
} )() );
|
|
334
656
|
}
|
|
335
657
|
|
|
336
658
|
/**
|
|
337
|
-
* Register a UI-only before hook for a command.
|
|
659
|
+
* Register a UI-only before hook for a command: called with its args, before it runs.
|
|
338
660
|
*/
|
|
339
|
-
public onBeforeUI( hookCommand: string, callback:
|
|
340
|
-
|
|
661
|
+
public onBeforeUI( hookCommand: string, callback: DCommandCallback ) {
|
|
662
|
+
const id = hookId( "before-ui", hookCommand );
|
|
663
|
+
|
|
664
|
+
managerAPI().hooks.registerUIBefore( new ( class extends hookUI.Before {
|
|
665
|
+
public getCommand() { return hookCommand; }
|
|
666
|
+
|
|
667
|
+
public getId() { return id; }
|
|
668
|
+
|
|
669
|
+
public apply( args: CommandArgs ) {
|
|
670
|
+
callback( args );
|
|
671
|
+
|
|
672
|
+
return true;
|
|
673
|
+
}
|
|
674
|
+
} )() );
|
|
341
675
|
}
|
|
342
676
|
|
|
343
677
|
/**
|
|
344
|
-
* Register a data-only after hook for a command.
|
|
678
|
+
* Register a data-only after hook for a command: called with its args and `result`, after it applied.
|
|
345
679
|
*/
|
|
346
|
-
public onAfter( hookCommand: string, callback:
|
|
347
|
-
|
|
680
|
+
public onAfter( hookCommand: string, callback: DCommandCallback ) {
|
|
681
|
+
const id = hookId( "after", hookCommand );
|
|
682
|
+
|
|
683
|
+
managerAPI().hooks.registerDataAfter( new ( class extends hookData.After {
|
|
684
|
+
public getCommand() { return hookCommand; }
|
|
685
|
+
|
|
686
|
+
public getId() { return id; }
|
|
687
|
+
|
|
688
|
+
public apply( args: CommandArgs, result: unknown ) {
|
|
689
|
+
callback( { ... args, result } );
|
|
690
|
+
|
|
691
|
+
return true;
|
|
692
|
+
}
|
|
693
|
+
} )() );
|
|
348
694
|
}
|
|
349
695
|
|
|
350
696
|
/**
|
|
351
|
-
* Register a UI-only after hook for a command.
|
|
697
|
+
* Register a UI-only after hook for a command: called with its args and `result`, after it ran.
|
|
352
698
|
*/
|
|
353
|
-
public onAfterUI( command: string, callback:
|
|
354
|
-
|
|
699
|
+
public onAfterUI( command: string, callback: DCommandCallback ) {
|
|
700
|
+
const id = hookId( "after-ui", command );
|
|
701
|
+
|
|
702
|
+
managerAPI().hooks.registerUIAfter( new ( class extends hookUI.After {
|
|
703
|
+
public getCommand() { return command; }
|
|
704
|
+
|
|
705
|
+
public getId() { return id; }
|
|
706
|
+
|
|
707
|
+
public apply( args: CommandArgs, result: unknown ) {
|
|
708
|
+
callback( { ... args, result } );
|
|
709
|
+
|
|
710
|
+
return true;
|
|
711
|
+
}
|
|
712
|
+
} )() );
|
|
355
713
|
}
|
|
356
714
|
|
|
357
715
|
/**
|
|
358
|
-
* Register a one-time after hook for a command.
|
|
716
|
+
* Register a one-time after hook for a command: called after its next run only.
|
|
359
717
|
*/
|
|
360
|
-
public onAfterOnce( command: string, callback:
|
|
361
|
-
|
|
718
|
+
public onAfterOnce( command: string, callback: DCommandCallback ) {
|
|
719
|
+
const api = managerAPI(),
|
|
720
|
+
id = hookId( "after-once", command );
|
|
721
|
+
|
|
722
|
+
api.hooks.registerDataAfter( new ( class extends hookData.After {
|
|
723
|
+
public getCommand() { return command; }
|
|
724
|
+
|
|
725
|
+
public getId() { return id; }
|
|
726
|
+
|
|
727
|
+
public apply( args: CommandArgs, result: unknown ) {
|
|
728
|
+
api.hooks.unregister( "data", id );
|
|
729
|
+
|
|
730
|
+
callback( { ... args, result } );
|
|
731
|
+
|
|
732
|
+
return true;
|
|
733
|
+
}
|
|
734
|
+
} )() );
|
|
362
735
|
}
|
|
363
736
|
|
|
364
737
|
/**
|
|
365
|
-
* Register a trigger that runs a command after another command runs
|
|
738
|
+
* Register a trigger that runs a command after another command runs, with its args and `result`.
|
|
366
739
|
*/
|
|
367
740
|
public onAfterAffect( hookCommand: string, affectCommand: string ) {
|
|
368
|
-
|
|
741
|
+
const api = managerAPI(),
|
|
742
|
+
id = hookId( "after-affect", hookCommand );
|
|
743
|
+
|
|
744
|
+
api.hooks.registerDataAfter( new ( class extends hookData.After {
|
|
745
|
+
public getCommand() { return hookCommand; }
|
|
746
|
+
|
|
747
|
+
public getId() { return id; }
|
|
748
|
+
|
|
749
|
+
public apply( args: CommandArgs, result: unknown ) {
|
|
750
|
+
const affectArgs: CommandArgs & { result: unknown } = { ... args, result };
|
|
751
|
+
|
|
752
|
+
api.commands.run( affectCommand, affectArgs );
|
|
753
|
+
|
|
754
|
+
return true;
|
|
755
|
+
}
|
|
756
|
+
} )() );
|
|
369
757
|
}
|
|
370
758
|
|
|
371
759
|
public devShowComponents() {
|
|
@@ -385,41 +773,19 @@ class CommandsManager {
|
|
|
385
773
|
console.log( ` ${ commandName } → ${ componentName }` );
|
|
386
774
|
} );
|
|
387
775
|
|
|
388
|
-
console.log( "🔗
|
|
389
|
-
Object.entries( this.
|
|
776
|
+
console.log( "🔗 Hooks (called by each command's $e hook):" );
|
|
777
|
+
Object.entries( this.instanceHooks ).forEach( ( [ componentNameUnique, commandHooks ] ) => {
|
|
390
778
|
console.group( ` ${ componentNameUnique }` );
|
|
391
779
|
Object.entries( commandHooks ).forEach( ( [ commandName, hooks ] ) => {
|
|
392
780
|
console.log( ` ${ commandName }: ${ hooks.length } hook(s)` );
|
|
393
781
|
hooks.forEach( ( hook, index ) => {
|
|
394
|
-
console.log( ` [${ index }] owner: ${ hook.ownerId }` );
|
|
782
|
+
console.log( ` [${ index }] owner: ${ hook.ownerId ?? "none" }` );
|
|
395
783
|
} );
|
|
396
784
|
} );
|
|
397
785
|
console.groupEnd();
|
|
398
786
|
} );
|
|
399
787
|
|
|
400
|
-
console.log( "🌐 Non-Scoped Hooks:" );
|
|
401
788
|
const contextKeys = core.__devGetContextKeys();
|
|
402
|
-
contextKeys.forEach( key => {
|
|
403
|
-
const context = core[ GET_INTERNAL_SYMBOL ]( key, true );
|
|
404
|
-
const hasNonScopedHooks = Object.keys( context.commands ).some( commandName => {
|
|
405
|
-
const listeners = context.emitter.listeners( commandName );
|
|
406
|
-
return listeners.length > 0;
|
|
407
|
-
} );
|
|
408
|
-
|
|
409
|
-
if ( hasNonScopedHooks ) {
|
|
410
|
-
console.group( ` ${ key }` );
|
|
411
|
-
Object.keys( context.commands ).forEach( commandName => {
|
|
412
|
-
const listeners = context.emitter.listeners( commandName );
|
|
413
|
-
if ( listeners.length > 0 ) {
|
|
414
|
-
console.log( ` ${ commandName }: ${ listeners.length } listener(s)` );
|
|
415
|
-
listeners.forEach( ( listener, index ) => {
|
|
416
|
-
console.log( ` [${ index }] ${ listener.name || "anonymous" }` );
|
|
417
|
-
} );
|
|
418
|
-
}
|
|
419
|
-
} );
|
|
420
|
-
console.groupEnd();
|
|
421
|
-
}
|
|
422
|
-
} );
|
|
423
789
|
|
|
424
790
|
console.log( "🏗️ Core Context:" );
|
|
425
791
|
console.log( ` Total components: ${ contextKeys.length }` );
|
|
@@ -446,7 +812,7 @@ function getOrCreateCommandsManager(): CommandsManager {
|
|
|
446
812
|
|
|
447
813
|
export const commands = getOrCreateCommandsManager();
|
|
448
814
|
|
|
449
|
-
if (
|
|
815
|
+
if ( DEV_ENABLED ) {
|
|
450
816
|
( window as any ).$$commands = commands;
|
|
451
817
|
( window as any ).$$dev = () => commands.devShowComponents();
|
|
452
818
|
}
|