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