@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,14 @@
|
|
|
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 ran, with its result (an async command's, once it resolved).
|
|
9
|
+
*/
|
|
10
|
+
export abstract class After<TCommand extends string = string> extends Base<TCommand, HookResult<TCommand>> {
|
|
11
|
+
public register(): void {
|
|
12
|
+
$e.hooks.registerUIAfter( this );
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -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 "ui";
|
|
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
|
+
* Before the command runs.
|
|
7
|
+
*/
|
|
8
|
+
export abstract class Before<TCommand extends string = string> extends Base<TCommand> {
|
|
9
|
+
public register(): void {
|
|
10
|
+
$e.hooks.registerUIBefore( this );
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -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.
|
|
7
|
+
*/
|
|
8
|
+
export abstract class Catch<TCommand extends string = string, TError = Error> extends Base<TCommand, TError> {
|
|
9
|
+
public register(): void {
|
|
10
|
+
$e.hooks.registerUICatch( this );
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A class that names its type, whichever bundle it comes from.
|
|
3
|
+
*/
|
|
4
|
+
interface NamedType {
|
|
5
|
+
getInstanceType(): string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* An object of such a class: the types it matched so far, by name (recorded on it, so another bundle's classes see
|
|
10
|
+
* them too).
|
|
11
|
+
*/
|
|
12
|
+
interface TypedObject {
|
|
13
|
+
constructor: Function & NamedType;
|
|
14
|
+
instanceTypes?: string[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const namesType = ( value: Function | undefined ): value is Function & NamedType =>
|
|
18
|
+
!! value && "getInstanceType" in value && "function" === typeof value.getInstanceType;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param target
|
|
22
|
+
*
|
|
23
|
+
* @return whether the object is of a class that names its type. Only this class records types on such objects.
|
|
24
|
+
*/
|
|
25
|
+
const isTypedObject = ( target: object ): target is TypedObject =>
|
|
26
|
+
// Undefined for an object without a prototype.
|
|
27
|
+
namesType( target.constructor );
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* `instanceof` that also holds between copies of a class: every bundle that includes this package has its own
|
|
31
|
+
* classes, so a command class from one bundle is not an instance of another bundle's CommandBase by prototype.
|
|
32
|
+
* Classes name their type with `getInstanceType()`, and an object matches a class by that name too.
|
|
33
|
+
*/
|
|
34
|
+
/**
|
|
35
|
+
* The types an instance of a class records as it is made (its `instanceTypes`), by class.
|
|
36
|
+
*/
|
|
37
|
+
const instanceTypesOf = new WeakMap<Function, string[]>();
|
|
38
|
+
|
|
39
|
+
export default class InstanceType {
|
|
40
|
+
public static [ Symbol.hasInstance ]( target: object ): boolean {
|
|
41
|
+
// What `super[ Symbol.hasInstance ]` is for a class that extends nothing: the prototype chain.
|
|
42
|
+
let result = Function.prototype[ Symbol.hasInstance ].call( this, target );
|
|
43
|
+
|
|
44
|
+
// Act normal when validating a class which does not have an instance type.
|
|
45
|
+
if ( target && ! isTypedObject( target ) ) {
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if ( target ) {
|
|
50
|
+
if ( ! target.instanceTypes ) {
|
|
51
|
+
target.instanceTypes = [];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if ( ! result ) {
|
|
55
|
+
if ( this.getInstanceType() === target.constructor.getInstanceType() ) {
|
|
56
|
+
result = true;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if ( result && ! target.instanceTypes.includes( this.getInstanceType() ) ) {
|
|
61
|
+
target.instanceTypes.push( this.getInstanceType() );
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if ( ! result && target ) {
|
|
66
|
+
// Check if the given 'target' is an instance of known types.
|
|
67
|
+
result = Array.isArray( target.instanceTypes ) && target.instanceTypes.includes( this.getInstanceType() );
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The type's name, which each class that matches by name gives its own (TS has no abstract static methods): a
|
|
75
|
+
* subclass that gives none matches as its parent class.
|
|
76
|
+
*
|
|
77
|
+
* @return its type's name
|
|
78
|
+
*/
|
|
79
|
+
public static getInstanceType(): string {
|
|
80
|
+
return "BaseInstanceType";
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public constructor() {
|
|
84
|
+
// The types depend on the class alone: found once for it, a copy for each instance after (a command is made
|
|
85
|
+
// for every run, and the walk below costs most of its making).
|
|
86
|
+
const known = instanceTypesOf.get( new.target );
|
|
87
|
+
|
|
88
|
+
if ( known ) {
|
|
89
|
+
if ( isTypedObject( this ) ) {
|
|
90
|
+
this.instanceTypes = [ ... known ];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Anonymous classes are not always validated by Babel, so record every parent class's type now.
|
|
97
|
+
const prototypes: Function[] = [];
|
|
98
|
+
|
|
99
|
+
let target: Function = new.target,
|
|
100
|
+
// Typed as `any` by TS: a class's prototype is its parent class (Function.prototype, without a name, at the
|
|
101
|
+
// top).
|
|
102
|
+
parent: Function | null = Object.getPrototypeOf( target );
|
|
103
|
+
|
|
104
|
+
while ( parent?.name ) {
|
|
105
|
+
target = parent;
|
|
106
|
+
prototypes.push( target );
|
|
107
|
+
parent = Object.getPrototypeOf( target );
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
prototypes.reverse().forEach( ( proto ) => this instanceof proto );
|
|
111
|
+
|
|
112
|
+
instanceTypesOf.set( new.target, isTypedObject( this ) && this.instanceTypes ? [ ... this.instanceTypes ] : [] );
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { deepMerge, isJsonObject, isJsonObjectDeep } from "@zenflux/commands/src/utils/object";
|
|
2
|
+
|
|
3
|
+
import type { JsonObject, JsonValue } from "@zenflux/commands/src/utils/object";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A listener of a module's event.
|
|
7
|
+
*/
|
|
8
|
+
export type ModuleListener = ( ...params: never[] ) => void;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A module's events: each one's listener, by the event's name.
|
|
12
|
+
*/
|
|
13
|
+
export type ModuleEventMap<TEvents> = {
|
|
14
|
+
[ TEvent in keyof TEvents ]: ModuleListener;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The events of every module.
|
|
19
|
+
*/
|
|
20
|
+
export interface ModuleEvents {
|
|
21
|
+
/**
|
|
22
|
+
* Once it is constructed (after `onInit()`).
|
|
23
|
+
*/
|
|
24
|
+
init: () => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Elementor's Module (assets/dev/js/modules/imports/module.js) as a class, without jQuery. `__construct()` runs
|
|
29
|
+
* first, for children that set things up without overriding the constructor; then the settings
|
|
30
|
+
* (`getDefaultSettings()`, merged with the first argument when it is a plain object of JSON values) and the 'init'
|
|
31
|
+
* event (`onInit()`, then its listeners).
|
|
32
|
+
*/
|
|
33
|
+
export default class Module<TEvents extends ModuleEvents & ModuleEventMap<TEvents> = ModuleEvents> {
|
|
34
|
+
#events: { [ TEvent in keyof TEvents ]?: TEvents[ TEvent ][] } = {};
|
|
35
|
+
|
|
36
|
+
#settings: JsonObject = {};
|
|
37
|
+
|
|
38
|
+
public constructor( args?: object ) {
|
|
39
|
+
this.__construct( args );
|
|
40
|
+
|
|
41
|
+
this.#settings = deepMerge( this.getDefaultSettings(), args && isJsonObjectDeep( args ) ? args : {} );
|
|
42
|
+
|
|
43
|
+
// Its listeners take nothing (ModuleEvents): TS can't say so of a subclass's events, which extend them.
|
|
44
|
+
this.trigger( "init", ...( [] as Parameters<TEvents[ "init" ]> ) );
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Set the module up, before its settings: with the arguments it was constructed with.
|
|
49
|
+
*
|
|
50
|
+
* @param args
|
|
51
|
+
*/
|
|
52
|
+
public __construct( _args?: object ): void {}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Called on the 'init' event, before its listeners.
|
|
56
|
+
*/
|
|
57
|
+
public onInit(): void {}
|
|
58
|
+
|
|
59
|
+
public getDefaultSettings(): JsonObject {
|
|
60
|
+
return {};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @param setting Dot path ('a.b.c'); all the settings without one.
|
|
65
|
+
*
|
|
66
|
+
* @return the setting
|
|
67
|
+
*/
|
|
68
|
+
public getSettings(): JsonObject;
|
|
69
|
+
public getSettings( setting: string ): JsonValue | undefined;
|
|
70
|
+
public getSettings( setting?: string ): JsonValue | undefined {
|
|
71
|
+
if ( ! setting ) {
|
|
72
|
+
return this.#settings;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return setting.split( "." ).reduce<JsonValue | undefined>( ( items, key ) => {
|
|
76
|
+
if ( Array.isArray( items ) ) {
|
|
77
|
+
return items[ Number( key ) ];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return isJsonObject( items ) ? items[ key ] : undefined;
|
|
81
|
+
}, this.#settings );
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param settingKey Dot path, or an object of settings to assign.
|
|
86
|
+
* @param value
|
|
87
|
+
* @param settingsContainer Where the path starts.
|
|
88
|
+
*
|
|
89
|
+
* @return self
|
|
90
|
+
*/
|
|
91
|
+
public setSettings( settings: JsonObject ): this;
|
|
92
|
+
public setSettings( settingKey: string, value: JsonValue | undefined, settingsContainer?: JsonObject ): this;
|
|
93
|
+
public setSettings( settingKey: string | JsonObject, value?: JsonValue, settingsContainer: JsonObject = this.#settings ): this {
|
|
94
|
+
if ( "object" === typeof settingKey ) {
|
|
95
|
+
Object.assign( settingsContainer, settingKey );
|
|
96
|
+
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const [ currentKey, ...keyStack ] = settingKey.split( "." );
|
|
101
|
+
|
|
102
|
+
if ( ! keyStack.length ) {
|
|
103
|
+
settingsContainer[ currentKey ] = value;
|
|
104
|
+
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const current = settingsContainer[ currentKey ],
|
|
109
|
+
container = isJsonObject( current ) ? current : {};
|
|
110
|
+
|
|
111
|
+
settingsContainer[ currentKey ] = container;
|
|
112
|
+
|
|
113
|
+
return this.setSettings( keyStack.join( "." ), value, container );
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @param eventName Event (in JS, several separated by spaces), or an object of event => callback.
|
|
118
|
+
* @param callback
|
|
119
|
+
*
|
|
120
|
+
* @return self
|
|
121
|
+
*/
|
|
122
|
+
public on<TEvent extends keyof TEvents & string>( eventName: TEvent, callback: TEvents[ TEvent ] ): this;
|
|
123
|
+
public on( events: Partial<TEvents> ): this;
|
|
124
|
+
public on<TEvent extends keyof TEvents & string>( eventName: TEvent | Partial<TEvents>, callback?: TEvents[ TEvent ] ): this {
|
|
125
|
+
if ( "object" === typeof eventName ) {
|
|
126
|
+
for ( const singleEventName in eventName ) {
|
|
127
|
+
const singleCallback = eventName[ singleEventName ];
|
|
128
|
+
|
|
129
|
+
if ( singleCallback ) {
|
|
130
|
+
this.on( singleEventName, singleCallback );
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if ( ! callback ) {
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
this.eventNames( eventName ).forEach( ( singleEventName ) => {
|
|
142
|
+
const callbacks = this.#events[ singleEventName ] || [];
|
|
143
|
+
|
|
144
|
+
callbacks.push( callback );
|
|
145
|
+
|
|
146
|
+
this.#events[ singleEventName ] = callbacks;
|
|
147
|
+
} );
|
|
148
|
+
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @param eventName
|
|
154
|
+
* @param callback Without one, every callback of the event.
|
|
155
|
+
*
|
|
156
|
+
* @return self
|
|
157
|
+
*/
|
|
158
|
+
public off<TEvent extends keyof TEvents & string>( eventName: TEvent, callback?: TEvents[ TEvent ] ): this {
|
|
159
|
+
const callbacks = this.#events[ eventName ];
|
|
160
|
+
|
|
161
|
+
if ( ! callbacks ) {
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if ( ! callback ) {
|
|
166
|
+
delete this.#events[ eventName ];
|
|
167
|
+
|
|
168
|
+
return this;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
this.#events[ eventName ] = callbacks.filter( ( eventCallback ) => eventCallback !== callback );
|
|
172
|
+
|
|
173
|
+
return this;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Call the `on<EventName>()` method when there is one (`onInit()`), then the event's callbacks.
|
|
178
|
+
*
|
|
179
|
+
* @param eventName
|
|
180
|
+
* @param params
|
|
181
|
+
*
|
|
182
|
+
* @return self
|
|
183
|
+
*/
|
|
184
|
+
/**
|
|
185
|
+
* @param eventName
|
|
186
|
+
*
|
|
187
|
+
* @return whether the event has listeners: what a caller asks before it gathers an event's arguments
|
|
188
|
+
*/
|
|
189
|
+
public hasListeners<TEvent extends keyof TEvents & string>( eventName: TEvent ): boolean {
|
|
190
|
+
return !! this.#events[ eventName ]?.length;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
public trigger<TEvent extends keyof TEvents & string>( eventName: TEvent, ...params: Parameters<TEvents[ TEvent ]> ): this {
|
|
194
|
+
if ( "init" === eventName ) {
|
|
195
|
+
this.onInit();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const listeners = this.#events[ eventName ];
|
|
199
|
+
|
|
200
|
+
// Most events have none: no list to make for them.
|
|
201
|
+
if ( listeners ) {
|
|
202
|
+
listeners.forEach( ( callback ) => callback.apply( this, params ) );
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return this;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* @param eventName An event; in JS, several separated by spaces.
|
|
210
|
+
*
|
|
211
|
+
* @return each event's name. A typed name is a single event's: TS can't say that a split string is one.
|
|
212
|
+
*/
|
|
213
|
+
private eventNames<TEvent extends keyof TEvents & string>( eventName: TEvent ): TEvent[] {
|
|
214
|
+
return eventName.split( " " ) as TEvent[];
|
|
215
|
+
}
|
|
216
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The types an app names its commands, routes and components with, by declaration merging:
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* declare module '@zenflux/commands' {
|
|
6
|
+
* interface CommandRegistry {
|
|
7
|
+
* 'chat/send': typeof Send;
|
|
8
|
+
* }
|
|
9
|
+
* }
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* `$e.run()`, `$e.internal()`, `$e.route()` and `$e.components.get()` take only the names the registries have, with
|
|
13
|
+
* their args, and give their results.
|
|
14
|
+
*/
|
|
15
|
+
import type ComponentBase from "./modules/component-base";
|
|
16
|
+
import type { ShortcutConfig } from "./core/shortcuts";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What a hook bound to a container type looks at in the args (`HookBase.getContainerType()`).
|
|
20
|
+
*/
|
|
21
|
+
export interface HookContainer {
|
|
22
|
+
type?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* `args.options`: hooks turned off for the run, by id (`{ callbacks: { [ id ]: false } }`).
|
|
27
|
+
*/
|
|
28
|
+
export interface RunOptions {
|
|
29
|
+
callbacks?: { [ id: string ]: boolean };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* What the commands system reads in any command's args, when they have it.
|
|
34
|
+
*/
|
|
35
|
+
export interface SystemArgs {
|
|
36
|
+
/**
|
|
37
|
+
* Called before the command runs, with its component as `this`.
|
|
38
|
+
*/
|
|
39
|
+
onBefore?: ( this: ComponentBase, args: CommandArgs ) => void;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Called after it ran, with its result or the error it failed with.
|
|
43
|
+
*/
|
|
44
|
+
onAfter?: <TResult>( this: ComponentBase, args: CommandArgs, result: TResult ) => void;
|
|
45
|
+
|
|
46
|
+
options?: RunOptions;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The container a hook bound to a container type looks at (or the first of `containers`).
|
|
50
|
+
*/
|
|
51
|
+
container?: HookContainer;
|
|
52
|
+
containers?: HookContainer[];
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A route: run it even when it is the current route already; open its component again.
|
|
56
|
+
*/
|
|
57
|
+
refresh?: boolean;
|
|
58
|
+
reOpen?: boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Any command's args: an object, whose fields that the system reads (`SystemArgs`) have the types it reads them with.
|
|
63
|
+
* A keyboard event is one: a shortcut runs its command with the event as its args.
|
|
64
|
+
*/
|
|
65
|
+
export type CommandArgs = object & SystemArgs;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The args of a command that takes none: `{}` or nothing.
|
|
69
|
+
*/
|
|
70
|
+
export type EmptyArgs = Record<string, never>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Extra information about a command (its class's `getInfo()`): `secretArgs` (the args `$e.devTools` hides in the
|
|
74
|
+
* console: a key, a password) and `secretResult` (it hides the result too). An app adds its own fields by declaration
|
|
75
|
+
* merging.
|
|
76
|
+
*/
|
|
77
|
+
export interface CommandInfo {
|
|
78
|
+
secretArgs?: string[];
|
|
79
|
+
secretResult?: boolean;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The commands `$e.run()` runs, by full name: `'chat/send': typeof Send`.
|
|
84
|
+
*/
|
|
85
|
+
export interface CommandRegistry {}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The commands `$e.internal()` runs, by full name: `'agent/apply': typeof Apply`.
|
|
89
|
+
*/
|
|
90
|
+
export interface CommandInternalRegistry {}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The routes `$e.route()` goes to, by full name, each with its args' type: `'library/category': { slug: string }`,
|
|
94
|
+
* `'panel/chat': EmptyArgs`. A component's tabs are routes too.
|
|
95
|
+
*/
|
|
96
|
+
export interface RouteRegistry {}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The components `$e.components.get()` gives, by namespace, each its class's instance type: `chat: ChatComponent`.
|
|
100
|
+
*/
|
|
101
|
+
export interface ComponentRegistry {}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* A command class's args: what its `CommandBase<TArgs>` takes.
|
|
105
|
+
*/
|
|
106
|
+
export type ArgsOf<TCommand> = TCommand extends { prototype: { args: infer TArgs extends CommandArgs } } ? TArgs : never;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A command class's result: what its `apply()` returns.
|
|
110
|
+
*/
|
|
111
|
+
export type ResultOf<TCommand> = TCommand extends { prototype: { apply( ...args: never[] ): infer TResult } } ? TResult : never;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* What a run gives: the command's result (an async command's promise resolves once its 'after' hooks are done), or
|
|
115
|
+
* false when it did not run: its component's `dependency()` or a data dependency hook stopped it.
|
|
116
|
+
*/
|
|
117
|
+
export type RunResult<TCommand> = ResultOf<TCommand> | false;
|
|
118
|
+
|
|
119
|
+
export type CommandName = keyof CommandRegistry & string;
|
|
120
|
+
export type CommandInternalName = keyof CommandInternalRegistry & string;
|
|
121
|
+
export type RouteName = keyof RouteRegistry & string;
|
|
122
|
+
export type ComponentName = keyof ComponentRegistry & string;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The args of a route, by its name.
|
|
126
|
+
*/
|
|
127
|
+
export type RouteArgs<TRoute extends RouteName> = RouteRegistry[ TRoute ] extends infer TArgs extends CommandArgs ? TArgs : never;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Of these names, those a component of this namespace registers: '<namespace>/<name>', not those of a component inside
|
|
131
|
+
* it ('editor/elements/select' is the 'editor/elements' component's, not the editor's).
|
|
132
|
+
*/
|
|
133
|
+
export type OwnNames<TNamespace extends string, TName extends string> = TName extends `${ TNamespace }/${ infer TOwn }`
|
|
134
|
+
? TOwn extends `${ string }/${ string }` ? never : TName
|
|
135
|
+
: never;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* A component's commands (`defaultCommands()`), by full name: each command of the CommandRegistry in its namespace,
|
|
139
|
+
* with the class the registry names, and no other.
|
|
140
|
+
*/
|
|
141
|
+
export type CommandsOf<TNamespace extends string> = {
|
|
142
|
+
[ TName in OwnNames<TNamespace, CommandName> ]: CommandRegistry[ TName ];
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* A component's internal commands (`defaultCommandsInternal()`), as `CommandsOf` for the CommandInternalRegistry.
|
|
147
|
+
*/
|
|
148
|
+
export type CommandsInternalOf<TNamespace extends string> = {
|
|
149
|
+
[ TName in OwnNames<TNamespace, CommandInternalName> ]: CommandInternalRegistry[ TName ];
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* A component's routes (`defaultRoutes()`), by full name: routes of the RouteRegistry in its namespace (its tabs are
|
|
154
|
+
* routes too, registered by `defaultTabs()`), each a function of its args.
|
|
155
|
+
*/
|
|
156
|
+
export type RoutesOf<TNamespace extends string> = {
|
|
157
|
+
[ TName in OwnNames<TNamespace, RouteName> ]?: ( args: RouteArgs<TName> ) => void;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* A component's shortcuts (`defaultShortcuts()`), by the full name of the command each runs: one of its commands.
|
|
162
|
+
*/
|
|
163
|
+
export type ShortcutsOf<TNamespace extends string> = {
|
|
164
|
+
[ TName in OwnNames<TNamespace, CommandName | CommandInternalName> ]?: ShortcutConfig;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* A run's args, after its name: optional when the command takes none.
|
|
169
|
+
*/
|
|
170
|
+
export type ArgsParam<TArgs> = EmptyArgs extends TArgs ? [ args?: TArgs ] : [ args: TArgs ];
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* The commands of every kind that hooks can hook (routes have none).
|
|
174
|
+
*/
|
|
175
|
+
export type HookableRegistry = CommandRegistry & CommandInternalRegistry;
|
|
176
|
+
|
|
177
|
+
export type HookableName = keyof HookableRegistry & string;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* The args of the command a hook hooks, by its name; any command's for a name no registry has.
|
|
181
|
+
*/
|
|
182
|
+
export type HookArgs<TCommand extends string> = TCommand extends HookableName ? ArgsOf<HookableRegistry[ TCommand ]> : CommandArgs;
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The result an 'after' hook gets from the command it hooks, by its name (an async command's, once it resolved).
|
|
186
|
+
*/
|
|
187
|
+
export type HookResult<TCommand extends string> = TCommand extends HookableName ? Awaited<ResultOf<HookableRegistry[ TCommand ]>> : void;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* What a hook's `apply()` returns: whether it holds (a dependency hook stops its command with false; a catch hook
|
|
191
|
+
* that returns nothing is reported), a promise the command waits for (an 'after' data hook of an async command), or
|
|
192
|
+
* nothing.
|
|
193
|
+
*/
|
|
194
|
+
export type HookReturn = boolean | object | void;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import HookBreak from "@zenflux/commands/src/modules/hook-break";
|
|
2
|
+
|
|
3
|
+
export default class Console {
|
|
4
|
+
/**
|
|
5
|
+
* @param message What failed: an error, or whatever a command threw or rejected with.
|
|
6
|
+
*/
|
|
7
|
+
public static error<TMessage>( message: TMessage ): void {
|
|
8
|
+
// A 'Hook-Break' is how a dependency hook stops a command, not an error.
|
|
9
|
+
if ( ! ( message instanceof HookBreak ) ) {
|
|
10
|
+
|
|
11
|
+
console.error( message );
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public static warn( ...args: Parameters<typeof console.warn> ): void {
|
|
16
|
+
|
|
17
|
+
console.warn( ...args );
|
|
18
|
+
}
|
|
19
|
+
}
|