@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
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import CommandCallbackBase from "./command-callback-base";
|
|
2
|
+
import Module from "./module";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
isCommandClass
|
|
6
|
+
|
|
7
|
+
} from "./command-infra";
|
|
8
|
+
|
|
9
|
+
import { $e } from "@zenflux/commands/src/instance";
|
|
10
|
+
|
|
11
|
+
import { createStore } from "@zenflux/commands/src/core/store";
|
|
12
|
+
|
|
13
|
+
import type { CommandCallback, CommandClass, RegisteredCallback, RegisterConfig } from "./command-infra";
|
|
14
|
+
import type { ModuleEvents } from "./module";
|
|
15
|
+
|
|
16
|
+
import type { RunnableHook } from "./hook-base";
|
|
17
|
+
import type { ShortcutConfig } from "@zenflux/commands/src/core/shortcuts";
|
|
18
|
+
import type { CommandArgs } from "@zenflux/commands/src/types";
|
|
19
|
+
|
|
20
|
+
import type { Store, StoreConfig } from "@zenflux/commands/src/core/store";
|
|
21
|
+
|
|
22
|
+
export type { StoreConfig };
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A component's commands by full name ('panel/close'): command classes, or functions. A component names its own with
|
|
26
|
+
* `CommandsOf< 'panel' >` (types.ts), which checks them against the app's registry.
|
|
27
|
+
*/
|
|
28
|
+
export interface CommandClasses {
|
|
29
|
+
[ command: string ]: CommandClass | CommandCallback;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A component's routes by full name ('library/category'), besides its tabs: each a function of its route's args
|
|
34
|
+
* (`RoutesOf< 'library' >`).
|
|
35
|
+
*/
|
|
36
|
+
export interface RouteCallbacks {
|
|
37
|
+
[ route: string ]: CommandCallback;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A component's hooks, by id.
|
|
42
|
+
*/
|
|
43
|
+
export interface HookInstances {
|
|
44
|
+
[ id: string ]: RunnableHook;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A hook class, as a hooks module exports it (`importHooks()`).
|
|
49
|
+
*/
|
|
50
|
+
export type HookClass = new () => RunnableHook;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A tab's config (`getActiveTabConfig()`).
|
|
54
|
+
*/
|
|
55
|
+
export type TabConfig = object;
|
|
56
|
+
|
|
57
|
+
export interface TabConfigs {
|
|
58
|
+
[ tab: string ]: TabConfig;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A component's shortcuts, by the full name of the command each runs (`ShortcutsOf< 'panel' >`).
|
|
63
|
+
*/
|
|
64
|
+
export interface ShortcutConfigs {
|
|
65
|
+
[ command: string ]: ShortcutConfig;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* What a component may be constructed with.
|
|
70
|
+
*/
|
|
71
|
+
export interface ComponentArgs {
|
|
72
|
+
manager?: ComponentBase;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface ComponentEvents extends ModuleEvents {
|
|
76
|
+
/**
|
|
77
|
+
* One of its routes opened.
|
|
78
|
+
*/
|
|
79
|
+
"route/open": ( route: string ) => void;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* One of its routes closed.
|
|
83
|
+
*/
|
|
84
|
+
"route/close": ( route: string ) => void;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* A function registered by name (a route's, a command's) is called with the args its name runs with (`$e.route()`
|
|
89
|
+
* checks them against the RouteRegistry), which its own args' type names: TS can't follow them to it through the name.
|
|
90
|
+
*
|
|
91
|
+
* @param callback
|
|
92
|
+
*
|
|
93
|
+
* @return the function, as the commands systems keep it
|
|
94
|
+
*/
|
|
95
|
+
const byName = ( callback: CommandCallback ): RegisteredCallback => callback as RegisteredCallback;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* What `__construct()` sets, which Module's constructor runs before this class's fields are defined: typed by this
|
|
99
|
+
* interface, merged with the class, and not as fields of it. A compiler that defines a class's fields (esbuild, vite,
|
|
100
|
+
* TypeScript itself for ES2022) would define them as undefined after `__construct()` set them.
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
interface ComponentBase<TStore extends StoreConfig = StoreConfig> {
|
|
104
|
+
manager?: ComponentBase;
|
|
105
|
+
commands: CommandClasses;
|
|
106
|
+
commandsInternal: CommandClasses;
|
|
107
|
+
hooks: HookInstances;
|
|
108
|
+
routes: RouteCallbacks;
|
|
109
|
+
tabs: TabConfigs;
|
|
110
|
+
shortcuts: ShortcutConfigs;
|
|
111
|
+
storeConfig: TStore | null;
|
|
112
|
+
currentTab: string;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* A component: a namespace ('panel', 'panel/pages') with its commands, routes, tabs, hooks, shortcuts and its own
|
|
117
|
+
* state (its store, `this.store`), registered by `$e.components.register( new Component() )`. `TStore`
|
|
118
|
+
* is its store's config (`defaultStore()`), which types `select( component.store )` and `dispatch( component.store )`.
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
abstract class ComponentBase<TStore extends StoreConfig = StoreConfig> extends Module<ComponentEvents> {
|
|
122
|
+
/**
|
|
123
|
+
* Whether it is open: set as its first route runs (`open()`), and by `close()`.
|
|
124
|
+
*/
|
|
125
|
+
public isOpen?: boolean;
|
|
126
|
+
|
|
127
|
+
#store: Store<TStore> | null = null;
|
|
128
|
+
|
|
129
|
+
#serviceName?: string;
|
|
130
|
+
|
|
131
|
+
public __construct( args: ComponentArgs = {} ): void {
|
|
132
|
+
if ( args.manager ) {
|
|
133
|
+
this.manager = args.manager;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
this.commands = this.defaultCommands();
|
|
137
|
+
this.commandsInternal = this.defaultCommandsInternal();
|
|
138
|
+
this.hooks = this.defaultHooks();
|
|
139
|
+
this.routes = this.defaultRoutes();
|
|
140
|
+
this.tabs = this.defaultTabs();
|
|
141
|
+
this.shortcuts = this.defaultShortcuts();
|
|
142
|
+
this.storeConfig = this.defaultStore();
|
|
143
|
+
|
|
144
|
+
this.currentTab = "";
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The component's store, once registered: `select( component.store )`, `dispatch( component.store )`.
|
|
149
|
+
*
|
|
150
|
+
* @throws {Error} before it is registered, or for a component without one (`defaultStore()` is null)
|
|
151
|
+
*/
|
|
152
|
+
public get store(): Store<TStore> {
|
|
153
|
+
if ( ! this.#store ) {
|
|
154
|
+
throw Error( `The '${ this.getNamespace() }' component has no store: it is not registered yet, or its defaultStore() is null.` );
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return this.#store;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* @return whether its store is registered
|
|
162
|
+
*/
|
|
163
|
+
public hasStore(): boolean {
|
|
164
|
+
return !! this.#store;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
public registerAPI(): void {
|
|
168
|
+
Object.keys( this.getTabs() ).forEach( ( tab ) => this.registerTabRoute( tab ) );
|
|
169
|
+
|
|
170
|
+
Object.entries( this.getRoutes() ).forEach( ( [ route, callback ] ) => this.registerRoute( route, callback ) );
|
|
171
|
+
|
|
172
|
+
Object.entries( this.getCommands() ).forEach( ( [ command, callback ] ) => this.registerCommand( command, callback ) );
|
|
173
|
+
|
|
174
|
+
Object.entries( this.getCommandsInternal() ).forEach( ( [ command, callback ] ) => this.registerCommandInternal( command, callback ) );
|
|
175
|
+
|
|
176
|
+
Object.values( this.getHooks() ).forEach( ( instance ) => this.registerHook( instance ) );
|
|
177
|
+
|
|
178
|
+
const storeConfig = this.getStoreConfig();
|
|
179
|
+
|
|
180
|
+
if ( storeConfig ) {
|
|
181
|
+
this.registerStore( storeConfig );
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @return namespace
|
|
187
|
+
*/
|
|
188
|
+
public abstract getNamespace(): string;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* @return the first part of the namespace: the container its routes and commands run in
|
|
192
|
+
*/
|
|
193
|
+
public getServiceName(): string {
|
|
194
|
+
// Its namespace's first part, which does not change: found once (every run reads it twice).
|
|
195
|
+
return this.#serviceName ??= this.getNamespace().split( "/" )[ 0 ];
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
public defaultTabs(): TabConfigs {
|
|
199
|
+
return {};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
public defaultRoutes(): RouteCallbacks {
|
|
203
|
+
return {};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
public defaultCommands(): CommandClasses {
|
|
207
|
+
return {};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
public defaultCommandsInternal(): CommandClasses {
|
|
211
|
+
return {};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
public defaultHooks(): HookInstances {
|
|
215
|
+
return {};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Shortcuts by command: { 'editor/save': { keys: 'ctrl+s', exclude: [ 'input' ], scopes: [ namespace ] } }.
|
|
220
|
+
*
|
|
221
|
+
* @return shortcuts
|
|
222
|
+
*/
|
|
223
|
+
public defaultShortcuts(): ShortcutConfigs {
|
|
224
|
+
return {};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* The component's own state: its store's config ({ reducer, actions, selectors }; a store of
|
|
229
|
+
* plain values: `valueStore()`), registered as `<storePrefix>/<namespace>`. Null for none.
|
|
230
|
+
*
|
|
231
|
+
* @return store config
|
|
232
|
+
*/
|
|
233
|
+
public defaultStore(): TStore | null {
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
public getCommands(): CommandClasses {
|
|
238
|
+
return this.commands;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
public getCommandsInternal(): CommandClasses {
|
|
242
|
+
return this.commandsInternal;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
public getHooks(): HookInstances {
|
|
246
|
+
return this.hooks;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
public getRoutes(): RouteCallbacks {
|
|
250
|
+
return this.routes;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
public getTabs(): TabConfigs {
|
|
254
|
+
return this.tabs;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
public getShortcuts(): ShortcutConfigs {
|
|
258
|
+
return this.shortcuts;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
public getStoreConfig(): TStore | null {
|
|
262
|
+
return this.storeConfig;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* @return the store's name: 'zenflux/site'
|
|
267
|
+
*/
|
|
268
|
+
public getStoreName(): string {
|
|
269
|
+
return `${ $e.config.storePrefix }/${ this.getNamespace() }`;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* @param command Its full name: 'panel/close'.
|
|
274
|
+
* @param context A command class, or a function.
|
|
275
|
+
* @param commandsType
|
|
276
|
+
*/
|
|
277
|
+
public registerCommand( command: string, context: CommandClass | CommandCallback, commandsType: "default" | "internal" = "default" ): void {
|
|
278
|
+
const commandsManager = "internal" === commandsType ? $e.commandsInternal : $e.commands;
|
|
279
|
+
|
|
280
|
+
const registerConfig: RegisterConfig = {
|
|
281
|
+
command,
|
|
282
|
+
component: this,
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
let Command: CommandClass;
|
|
286
|
+
|
|
287
|
+
if ( ! isCommandClass( context ) ) {
|
|
288
|
+
if ( $e.devTools ) {
|
|
289
|
+
$e.devTools.log.warn( `Attach command-callback-base, on command: '${ command }', context is unknown type.` );
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Support pure callback: a unique class.
|
|
293
|
+
registerConfig.callback = byName( context );
|
|
294
|
+
|
|
295
|
+
Command = class extends CommandCallbackBase {};
|
|
296
|
+
} else if ( Object.prototype.hasOwnProperty.call( context, "registerConfig" ) ) {
|
|
297
|
+
// A class that another component registered already: a unique subclass, so each one keeps its own command
|
|
298
|
+
// and component.
|
|
299
|
+
Command = class extends context {};
|
|
300
|
+
} else {
|
|
301
|
+
Command = context;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
Command.setRegisterConfig( registerConfig );
|
|
305
|
+
|
|
306
|
+
commandsManager.register( this, command, Command );
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* @param instance
|
|
311
|
+
*/
|
|
312
|
+
public registerHook( instance: RunnableHook ): void {
|
|
313
|
+
return instance.register();
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
public registerCommandInternal( command: string, context: CommandClass | CommandCallback ): void {
|
|
317
|
+
this.registerCommand( command, context, "internal" );
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
public registerRoute( route: string, callback: CommandCallback ): void {
|
|
321
|
+
$e.routes.register( this, route, byName( callback ) );
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Make the component's store, from its config.
|
|
326
|
+
*
|
|
327
|
+
* @param config - The store config.
|
|
328
|
+
*/
|
|
329
|
+
public registerStore( config: TStore ): void {
|
|
330
|
+
this.#store = createStore( this.getStoreName(), config );
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
public unregisterRoute( route: string ): void {
|
|
334
|
+
$e.routes.unregister( this, route );
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
public registerTabRoute( tab: string ): void {
|
|
338
|
+
this.registerRoute( this.getTabRoute( tab ), ( args: CommandArgs ) => this.activateTab( tab, args ) );
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Whether the component's commands and routes can run now (`$e.commands.validateRun()`).
|
|
343
|
+
*
|
|
344
|
+
* @param command The command or route to run.
|
|
345
|
+
* @param args Its args.
|
|
346
|
+
*
|
|
347
|
+
* @return dependency
|
|
348
|
+
*/
|
|
349
|
+
public dependency( _command: string, _args: CommandArgs ): boolean {
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Open the component, before its first route runs.
|
|
355
|
+
*
|
|
356
|
+
* @param args The route's args.
|
|
357
|
+
*
|
|
358
|
+
* @return whether it opened
|
|
359
|
+
*/
|
|
360
|
+
public open( _args: CommandArgs ): boolean {
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
public close(): boolean {
|
|
365
|
+
if ( ! this.isOpen ) {
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
this.isOpen = false;
|
|
370
|
+
|
|
371
|
+
this.inactivate();
|
|
372
|
+
|
|
373
|
+
$e.routes.clearCurrent( this.getNamespace() );
|
|
374
|
+
|
|
375
|
+
$e.routes.clearHistory( this.getServiceName() );
|
|
376
|
+
|
|
377
|
+
return true;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
public activate(): void {
|
|
381
|
+
$e.components.activate( this.getNamespace() );
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
public inactivate(): void {
|
|
385
|
+
$e.components.inactivate( this.getNamespace() );
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
public isActive(): boolean {
|
|
389
|
+
return $e.components.isActive( this.getNamespace() );
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* @param route
|
|
394
|
+
* @param args The route's args.
|
|
395
|
+
*/
|
|
396
|
+
public onRoute( route: string, _args?: CommandArgs ): void {
|
|
397
|
+
this.activate();
|
|
398
|
+
this.trigger( "route/open", route );
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
public onCloseRoute( route: string ): void {
|
|
402
|
+
this.inactivate();
|
|
403
|
+
this.trigger( "route/close", route );
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
public removeTab( tab: string ): void {
|
|
407
|
+
delete this.tabs[ tab ];
|
|
408
|
+
this.unregisterRoute( this.getTabRoute( tab ) );
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
public hasTab( tab: string ): boolean {
|
|
412
|
+
return !! this.tabs[ tab ];
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
public addTab( tab: string, args: TabConfig, position?: number ): void {
|
|
416
|
+
this.tabs[ tab ] = args;
|
|
417
|
+
|
|
418
|
+
// It can be 0.
|
|
419
|
+
if ( "undefined" !== typeof position ) {
|
|
420
|
+
const newTabs: TabConfigs = {};
|
|
421
|
+
const ids = Object.keys( this.tabs );
|
|
422
|
+
|
|
423
|
+
// Remove new tab.
|
|
424
|
+
ids.pop();
|
|
425
|
+
|
|
426
|
+
// Add it to position.
|
|
427
|
+
ids.splice( position, 0, tab );
|
|
428
|
+
|
|
429
|
+
ids.forEach( ( id ) => {
|
|
430
|
+
newTabs[ id ] = this.tabs[ id ];
|
|
431
|
+
} );
|
|
432
|
+
|
|
433
|
+
this.tabs = newTabs;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
this.registerTabRoute( tab );
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
public getTabRoute( tab: string ): string {
|
|
440
|
+
return this.getNamespace() + "/" + tab;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Show a tab: its route ran (`<namespace>/<tab>`).
|
|
445
|
+
*
|
|
446
|
+
* @param tab
|
|
447
|
+
* @param args
|
|
448
|
+
*/
|
|
449
|
+
public renderTab( _tab: string, _args: CommandArgs ): void {}
|
|
450
|
+
|
|
451
|
+
public activateTab( tab: string, args: CommandArgs ): void {
|
|
452
|
+
this.currentTab = tab;
|
|
453
|
+
|
|
454
|
+
this.renderTab( tab, args );
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
public getActiveTabConfig(): TabConfig {
|
|
458
|
+
return this.tabs[ this.currentTab ] || {};
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* @param hooksFromImport `import * as hooks from './hooks'`: class name => class.
|
|
463
|
+
*
|
|
464
|
+
* @return the hooks, by id
|
|
465
|
+
*/
|
|
466
|
+
public importHooks( hooksFromImport: { [ className: string ]: HookClass } ): HookInstances {
|
|
467
|
+
const hooks: HookInstances = {};
|
|
468
|
+
|
|
469
|
+
Object.values( hooksFromImport ).forEach( ( Hook ) => {
|
|
470
|
+
const hook = new Hook();
|
|
471
|
+
|
|
472
|
+
hooks[ hook.getId() ] = hook;
|
|
473
|
+
} );
|
|
474
|
+
|
|
475
|
+
return hooks;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
export default ComponentBase;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { $e } from "@zenflux/commands/src/instance";
|
|
2
|
+
|
|
3
|
+
import type { CommandArgs, HookArgs, HookReturn } from "@zenflux/commands/src/types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A hook's type: when it runs (core/hooks).
|
|
7
|
+
*/
|
|
8
|
+
export type HookType = "data" | "ui";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A hook as the hooks systems keep it, whatever its command's args and the value it takes.
|
|
12
|
+
*/
|
|
13
|
+
export interface RunnableHook {
|
|
14
|
+
type: HookType;
|
|
15
|
+
command: string;
|
|
16
|
+
id: string;
|
|
17
|
+
register(): void;
|
|
18
|
+
getType(): HookType;
|
|
19
|
+
getCommand(): string;
|
|
20
|
+
getId(): string;
|
|
21
|
+
getContainerType(): string | undefined;
|
|
22
|
+
run( args: CommandArgs, value: never ): HookReturn;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A hook on a command: `hookUI.Before`, `hookUI.After`, `hookUI.Catch`, `hookData.Dependency`, `hookData.After` or
|
|
27
|
+
* `hookData.Catch`, registered by a component's `defaultHooks()`. `TCommand` is the full name of the command it hooks
|
|
28
|
+
* (`getCommand()` returns it), which types the args its `apply()` gets (HookArgs); `TValue` is what it gets after them:
|
|
29
|
+
* the command's result for an 'after' hook, the error it failed with for a 'catch' hook, nothing before.
|
|
30
|
+
*/
|
|
31
|
+
export default abstract class HookBase<TCommand extends string = string, TValue = void> {
|
|
32
|
+
/**
|
|
33
|
+
* Callback type, eg ( hook, event ).
|
|
34
|
+
*/
|
|
35
|
+
public type: HookType;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Full command address, that will hook the callback.
|
|
39
|
+
*/
|
|
40
|
+
public command: TCommand;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Unique id of the callback.
|
|
44
|
+
*/
|
|
45
|
+
public id: string;
|
|
46
|
+
|
|
47
|
+
public constructor() {
|
|
48
|
+
this.initialize();
|
|
49
|
+
|
|
50
|
+
this.type = this.getType();
|
|
51
|
+
this.command = this.getCommand();
|
|
52
|
+
this.id = this.getId();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Called after creation of the base, used for initialize extras.
|
|
57
|
+
* Without extending constructor.
|
|
58
|
+
*/
|
|
59
|
+
public initialize(): void {}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Used to register the callback.
|
|
63
|
+
*/
|
|
64
|
+
public abstract register(): void;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Get type eg: ( hook, event, etc ... ).
|
|
68
|
+
*
|
|
69
|
+
* @return type
|
|
70
|
+
*/
|
|
71
|
+
public abstract getType(): HookType;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Returns the full command path for callback binding. TS: annotate the override's return with the name
|
|
75
|
+
* (`getCommand(): 'chat/send'`), as a method's literal return is widened to string.
|
|
76
|
+
*
|
|
77
|
+
* @return command
|
|
78
|
+
*/
|
|
79
|
+
public abstract getCommand(): TCommand;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Returns command id for the hook (should be unique).
|
|
83
|
+
*
|
|
84
|
+
* @return id
|
|
85
|
+
*/
|
|
86
|
+
public abstract getId(): string;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Bind the callback to commands whose `args.container` (or first of `args.containers`) has this `type`.
|
|
90
|
+
*
|
|
91
|
+
* Used to gain performance.
|
|
92
|
+
*
|
|
93
|
+
* @return type
|
|
94
|
+
*/
|
|
95
|
+
public getContainerType(): string | undefined {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Condition for running the callback, if true, call to apply().
|
|
101
|
+
*
|
|
102
|
+
* @param args
|
|
103
|
+
* @param value
|
|
104
|
+
*
|
|
105
|
+
* @return conditions
|
|
106
|
+
*/
|
|
107
|
+
public getConditions( _args: HookArgs<TCommand>, _value: TValue ): boolean {
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Apply the callback, ( The actual affect of the callback ).
|
|
113
|
+
*
|
|
114
|
+
* @param args
|
|
115
|
+
* @param value
|
|
116
|
+
*
|
|
117
|
+
* @return results
|
|
118
|
+
*/
|
|
119
|
+
public abstract apply( args: HookArgs<TCommand>, value: TValue ): HookReturn;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Run the callback.
|
|
123
|
+
*
|
|
124
|
+
* @param args
|
|
125
|
+
* @param value
|
|
126
|
+
*
|
|
127
|
+
* @return results
|
|
128
|
+
*/
|
|
129
|
+
public run( args: HookArgs<TCommand>, value: TValue ): HookReturn {
|
|
130
|
+
// What the system reads in any command's args.
|
|
131
|
+
const systemArgs: CommandArgs = args,
|
|
132
|
+
callbacks = systemArgs?.options?.callbacks;
|
|
133
|
+
|
|
134
|
+
// Disable callback if requested by args.options.
|
|
135
|
+
if ( callbacks && false === callbacks[ this.id ] ) {
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if ( this.getConditions( args, value ) ) {
|
|
140
|
+
if ( $e.devTools ) {
|
|
141
|
+
$e.devTools.log.callbacks().active( this.type, this.command, this.id );
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return this.apply( args, value );
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import Base from "./base";
|
|
2
|
+
|
|
3
|
+
import { $e } from "@zenflux/commands/src/instance";
|
|
4
|
+
|
|
5
|
+
import type { HookResult } from "@zenflux/commands/src/types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* After the command applied, with its result (an async command's, once it resolved; it waits for the promise the hook
|
|
9
|
+
* returns).
|
|
10
|
+
*/
|
|
11
|
+
export abstract class After<TCommand extends string = string> extends Base<TCommand, HookResult<TCommand>> {
|
|
12
|
+
public register(): void {
|
|
13
|
+
$e.hooks.registerDataAfter( this );
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import HookBase from "@zenflux/commands/src/modules/hook-base";
|
|
2
|
+
|
|
3
|
+
import type { HookType } from "@zenflux/commands/src/modules/hook-base";
|
|
4
|
+
|
|
5
|
+
export abstract class Base<TCommand extends string = string, TValue = void> extends HookBase<TCommand, TValue> {
|
|
6
|
+
public getType(): HookType {
|
|
7
|
+
return "data";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default Base;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import Base from "./base";
|
|
2
|
+
|
|
3
|
+
import { $e } from "@zenflux/commands/src/instance";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* When the command failed, with what it threw or rejected with (a data command's: a BaseError).
|
|
7
|
+
*/
|
|
8
|
+
export abstract class Catch<TCommand extends string = string, TError = Error> extends Base<TCommand, TError> {
|
|
9
|
+
public register(): void {
|
|
10
|
+
$e.hooks.registerDataCatch( this );
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import Base from "./base";
|
|
2
|
+
|
|
3
|
+
import { $e } from "@zenflux/commands/src/instance";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Before the command applies: false stops it (without an error).
|
|
7
|
+
*/
|
|
8
|
+
export abstract class Dependency<TCommand extends string = string> extends Base<TCommand> {
|
|
9
|
+
public register(): void {
|
|
10
|
+
$e.hooks.registerDataDependency( this );
|
|
11
|
+
}
|
|
12
|
+
}
|