@zenflux/core 0.1.12 → 0.1.14
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/dist/zenflux-core.cjs +6 -7
- package/dist/zenflux-core.mjs +6 -7
- package/package.json +3 -4
- package/src/bases/controller-base.ts +0 -96
- package/src/bases/index.ts +0 -6
- package/src/bases/initialize-base.ts +0 -32
- package/src/bases/object-base.ts +0 -55
- package/src/clients/http.ts +0 -152
- package/src/command-bases/command-base.ts +0 -82
- package/src/command-bases/command-internal.ts +0 -11
- package/src/command-bases/command-public.ts +0 -12
- package/src/command-bases/command-restful.ts +0 -55
- package/src/command-bases/index.ts +0 -8
- package/src/errors/command-already-registered.ts +0 -13
- package/src/errors/command-not-found.ts +0 -12
- package/src/errors/controller-already-registered.ts +0 -13
- package/src/errors/controller-already-set.ts +0 -13
- package/src/errors/error-with-metadata.ts +0 -14
- package/src/errors/force-method-implementation.ts +0 -15
- package/src/errors/index-public.ts +0 -1
- package/src/errors/index.ts +0 -10
- package/src/exports-index.ts +0 -13
- package/src/exports.ts +0 -58
- package/src/index.ts +0 -18
- package/src/interfaces/commands.ts +0 -17
- package/src/interfaces/config.ts +0 -23
- package/src/interfaces/index.ts +0 -8
- package/src/interfaces/logger.ts +0 -19
- package/src/interfaces/restful.ts +0 -23
- package/src/managers/commands.ts +0 -268
- package/src/managers/controllers.ts +0 -40
- package/src/managers/export.ts +0 -30
- package/src/managers/index.ts +0 -8
- package/src/managers/internal.ts +0 -11
- package/src/managers/restful.ts +0 -117
- package/src/modules/debugger.ts +0 -89
- package/src/modules/event-bus/event-bus.ts +0 -204
- package/src/modules/logger/config.ts +0 -44
- package/src/modules/logger/implementation.ts +0 -287
- package/src/modules/logger/index.ts +0 -2
- package/src/modules/logger/logger-browser-infra.ts +0 -344
- package/src/modules/logger/types/picocolors.d.ts +0 -15
- package/src/modules/logger/utils/index.ts +0 -3
- package/src/modules/logger/utils/object.ts +0 -19
- package/src/modules/logger/utils/rgb.ts +0 -34
- package/src/modules/mcp-server/mcp-server.ts +0 -139
- package/src/modules/mcp-server/mcp-service.ts +0 -80
- package/src/modules/service/service-base.ts +0 -55
- package/src/modules/service/service-locator.ts +0 -205
- package/src/modules/service/service-with-dependencies-base.ts +0 -51
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
* @description Each rest command should represent a final REST endpoint.
|
|
4
|
-
*/
|
|
5
|
-
import { CommandBase } from "./command-base";
|
|
6
|
-
|
|
7
|
-
import { ForceMethodImplementation } from "../errors";
|
|
8
|
-
|
|
9
|
-
import * as managers from "../managers/export";
|
|
10
|
-
|
|
11
|
-
export abstract class CommandRestful extends CommandBase {
|
|
12
|
-
public static getName() {
|
|
13
|
-
return "ZenFlux/Core/CommandBases/CommandRestful";
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Override this method is required to determine endpoint and magic query params.
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```
|
|
21
|
-
* args = { query: { id: 1 } };
|
|
22
|
-
* getEndpoint() = '/api/v1/users/{id}';
|
|
23
|
-
* result = '/api/v1/users/1';
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
public getEndpoint(): string {
|
|
27
|
-
throw new ForceMethodImplementation( this, "getEndpoint" );
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
protected apply( args = this.args, options = this.options ) {// eslint-disable-line @typescript-eslint/no-unused-vars
|
|
31
|
-
const endpoint = this.applyEndpointFormat( this.getEndpoint(), args );
|
|
32
|
-
|
|
33
|
-
return managers.restful.getClient().fetch(
|
|
34
|
-
endpoint, managers.restful.currentHttpMethod,
|
|
35
|
-
args || null
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
private applyEndpointFormat( endpoint: string, data: any = {} ): string {
|
|
40
|
-
// Replace query with `magic` placeholders.
|
|
41
|
-
if ( endpoint.includes( "{" ) ) {
|
|
42
|
-
endpoint = endpoint.split( "/" ).map( ( endpointPart ) => {
|
|
43
|
-
const match = endpointPart.match( "\\{(.*?)\\}" );
|
|
44
|
-
|
|
45
|
-
if ( match?.length && "undefined" !== typeof data[ match[ 1 ] ] ) {
|
|
46
|
-
return data[ match[ 1 ] ];
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return endpointPart;
|
|
50
|
-
} ).join( "/" );
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return endpoint;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export { CommandPublic } from "./command-public";
|
|
6
|
-
export { CommandRestful } from "./command-restful";
|
|
7
|
-
export { CommandBase } from "./command-base";
|
|
8
|
-
export { CommandInternal } from "./command-internal";
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
import type { CommandBase } from "../command-bases/command-base";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @internal
|
|
8
|
-
*/
|
|
9
|
-
export class CommandAlreadyRegistered extends Error {
|
|
10
|
-
public constructor( command: typeof CommandBase ) {
|
|
11
|
-
super( `Command: '${ command.getName() }' is already registered` );
|
|
12
|
-
}
|
|
13
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
import type { ControllerBase } from "../bases";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @internal
|
|
8
|
-
*/
|
|
9
|
-
export class ControllerAlreadyRegistered extends Error {
|
|
10
|
-
public constructor( controller: ControllerBase ) {
|
|
11
|
-
super( `Controller: '${ controller.getName() }' is already registered` );
|
|
12
|
-
}
|
|
13
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
import type { ControllerBase } from "../bases";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @internal
|
|
8
|
-
*/
|
|
9
|
-
export class ControllerAlreadySet extends Error {
|
|
10
|
-
public constructor( controller: ControllerBase ) {
|
|
11
|
-
super( `Controller: '${ controller.getName() }' is already set` );
|
|
12
|
-
}
|
|
13
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import util from "node:util";
|
|
2
|
-
|
|
3
|
-
export class ErrorWithMetadata extends Error {
|
|
4
|
-
public metadata: any;
|
|
5
|
-
|
|
6
|
-
public constructor( message: string, metadata: any | null = null ) {
|
|
7
|
-
super( message );
|
|
8
|
-
this.metadata = metadata;
|
|
9
|
-
|
|
10
|
-
this.message = null !== metadata ? `${ message } - Metadata: ${ util.inspect( metadata ) }` : message;
|
|
11
|
-
|
|
12
|
-
Object.setPrototypeOf( this, new.target.prototype );
|
|
13
|
-
}
|
|
14
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { ObjectBase } from "../bases";
|
|
2
|
-
|
|
3
|
-
export class ForceMethodBase extends Error {
|
|
4
|
-
public constructor( className: string, methodName: string ) {
|
|
5
|
-
super( `ForceMethod implementation: at '${ className }' method: '${ methodName }'` );
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export class ForceMethodImplementation extends Error {
|
|
10
|
-
public constructor( context: ObjectBase | typeof ObjectBase | string, methodName: string ) {
|
|
11
|
-
super(
|
|
12
|
-
`ForceMethod implementation: at '${ "string" === typeof context ? context : context.getName() }' method: '${ methodName }'`
|
|
13
|
-
);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { ForceMethodImplementation } from "./force-method-implementation";
|
package/src/errors/index.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export { CommandAlreadyRegistered } from "./command-already-registered";
|
|
6
|
-
export { CommandNotFound } from "./command-not-found";
|
|
7
|
-
export { ControllerAlreadyRegistered } from "./controller-already-registered";
|
|
8
|
-
export { ControllerAlreadySet } from "./controller-already-set";
|
|
9
|
-
export { ForceMethodImplementation, ForceMethodBase } from "./force-method-implementation";
|
|
10
|
-
export { ErrorWithMetadata } from "./error-with-metadata";
|
package/src/exports-index.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import * as bases from "./bases";
|
|
2
|
-
import * as commandBases from "./command-bases";
|
|
3
|
-
import * as errors from "./errors/index-public";
|
|
4
|
-
import * as interfaces from "./interfaces";
|
|
5
|
-
import * as managers from "./managers/export";
|
|
6
|
-
|
|
7
|
-
export {
|
|
8
|
-
bases,
|
|
9
|
-
commandBases,
|
|
10
|
-
errors,
|
|
11
|
-
interfaces,
|
|
12
|
-
managers,
|
|
13
|
-
};
|
package/src/exports.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
// @ts-ignore - TODO handle error in jest
|
|
5
|
-
import { bases, commandBases, errors, interfaces, managers, } from "./exports-index";
|
|
6
|
-
|
|
7
|
-
import { Logger } from "./modules/logger";
|
|
8
|
-
|
|
9
|
-
import * as pkg from "../package.json" assert { type: "json" };
|
|
10
|
-
|
|
11
|
-
declare global {
|
|
12
|
-
var __ZEN_CORE__IS_INITIALIZED__: boolean;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const classes = {
|
|
16
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
17
|
-
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
|
|
18
|
-
|
|
19
|
-
Logger,
|
|
20
|
-
|
|
21
|
-
/* eslint-enable */
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
let exportedConfig: interfaces.IAPIConfig = {
|
|
25
|
-
version: pkg.version,
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export function initialize( config?: Partial<interfaces.IAPIConfig> ) {
|
|
29
|
-
if ( "undefined" !== typeof __ZEN_CORE__IS_INITIALIZED__ && __ZEN_CORE__IS_INITIALIZED__ ) {
|
|
30
|
-
throw new Error( "ZenCore is already initialized." );
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
exportedConfig = ( config || exportedConfig ) as interfaces.IAPIConfig;
|
|
34
|
-
|
|
35
|
-
managers.initialize( exportedConfig );
|
|
36
|
-
|
|
37
|
-
globalThis.__ZEN_CORE__IS_INITIALIZED__ = true;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function destroy() {
|
|
41
|
-
managers.destroy();
|
|
42
|
-
|
|
43
|
-
globalThis.__ZEN_CORE__IS_INITIALIZED__ = false;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function onAfterInitialize( callback: () => void ) {
|
|
47
|
-
managers.afterInitializeCallbacks.push( callback );
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export const config = exportedConfig;
|
|
51
|
-
|
|
52
|
-
export {
|
|
53
|
-
bases,
|
|
54
|
-
commandBases,
|
|
55
|
-
errors,
|
|
56
|
-
interfaces,
|
|
57
|
-
managers
|
|
58
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import * as ZenCore from "./exports";
|
|
6
|
-
|
|
7
|
-
// TODO: Only in development mode
|
|
8
|
-
if ( ! globalThis?.zCore ) {
|
|
9
|
-
globalThis.zCore = ZenCore;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
declare global {
|
|
13
|
-
var zCore: typeof ZenCore;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export * from "./exports-index";
|
|
17
|
-
|
|
18
|
-
export default ZenCore;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export type TCommandCallbackType = ( args?: any, options?: any ) => any;
|
|
2
|
-
|
|
3
|
-
export interface ICommandArgsInterface {
|
|
4
|
-
[ key: string ]: any;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export interface ICommandOptionsInterface {
|
|
8
|
-
[ key: string ]: any;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface IOnHookAffectInterface {
|
|
12
|
-
[ key: string ]: Array<string>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface IOnHookInterface {
|
|
16
|
-
[ key: string ]: Array<TCommandCallbackType>;
|
|
17
|
-
}
|
package/src/interfaces/config.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
*
|
|
7
|
-
*/
|
|
8
|
-
export interface IAPIConfig {
|
|
9
|
-
/**
|
|
10
|
-
* @description API version, from `package.json`
|
|
11
|
-
*/
|
|
12
|
-
version: string;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @description API base url for http requests.
|
|
16
|
-
*/
|
|
17
|
-
baseURL?: string;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* @description Request Init for fetch API.
|
|
21
|
-
*/
|
|
22
|
-
requestInit?: RequestInit;
|
|
23
|
-
}
|
package/src/interfaces/index.ts
DELETED
package/src/interfaces/logger.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
export type TCaller = string | Function;
|
|
5
|
-
|
|
6
|
-
export interface ILogger {
|
|
7
|
-
log( caller: TCaller, message: string, ...params: any[] ): void;
|
|
8
|
-
warn( caller: TCaller, message: string, ...params: any[] ): void;
|
|
9
|
-
error( caller: TCaller, message: string, ...params: any[] ): void;
|
|
10
|
-
info( caller: TCaller, message: string, ...params: any[] ): void;
|
|
11
|
-
debug( caller: TCaller, message: string, ...params: any[] ): void;
|
|
12
|
-
addMessagePrefix( prefix: string ): void;
|
|
13
|
-
|
|
14
|
-
startsEmpty( caller: TCaller ): void;
|
|
15
|
-
startsWith( caller: TCaller, params: object | string ): void;
|
|
16
|
-
|
|
17
|
-
dump( caller: TCaller, params: { [ key: string ]: object|string }, notice?: string ): void;
|
|
18
|
-
drop( caller: TCaller, according: { [ key: string ]: string }, data: any ): void;
|
|
19
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
export type TErrorHandlerCallbackType = ( data: any ) => boolean;
|
|
2
|
-
|
|
3
|
-
export type TResponseFilterCallbackType = ( text: string ) => string;
|
|
4
|
-
|
|
5
|
-
export type TResponseHandlerCallbackType = ( text: string ) => boolean;
|
|
6
|
-
|
|
7
|
-
export type TPossibleHandlersType = TErrorHandlerCallbackType | TResponseFilterCallbackType | TResponseHandlerCallbackType;
|
|
8
|
-
|
|
9
|
-
export enum E_RESPONSE_HANDLER_TYPE {
|
|
10
|
-
ERROR_HANDLER = "error_handler",
|
|
11
|
-
RESPONSE_FILTER = "response_filter",
|
|
12
|
-
RESPONSE_HANDLER = "response_handler",
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export enum E_HTTP_METHOD_TYPE {
|
|
16
|
-
DELETE = "DELETE", // Delete.
|
|
17
|
-
GET = "GET", // Read.
|
|
18
|
-
OPTIONS = "OPTIONS",// Options about the request
|
|
19
|
-
PATCH = "PATCH", // Update/Modify.
|
|
20
|
-
POST = "POST", // Create.
|
|
21
|
-
PUT = "PUT", // Update/Replace.
|
|
22
|
-
"__EMPTY__" = ""
|
|
23
|
-
};
|
package/src/managers/commands.ts
DELETED
|
@@ -1,268 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
* @description Responsible for manging/running/hooking commands.
|
|
4
|
-
*/
|
|
5
|
-
import { ObjectBase } from "../bases/object-base";
|
|
6
|
-
|
|
7
|
-
import { CommandNotFound } from "../errors/command-not-found";
|
|
8
|
-
import { CommandAlreadyRegistered } from "../errors/command-already-registered";
|
|
9
|
-
|
|
10
|
-
import type {
|
|
11
|
-
TCommandCallbackType,
|
|
12
|
-
|
|
13
|
-
IOnHookAffectInterface,
|
|
14
|
-
IOnHookInterface,
|
|
15
|
-
|
|
16
|
-
ICommandArgsInterface,
|
|
17
|
-
ICommandOptionsInterface,
|
|
18
|
-
|
|
19
|
-
ILogger,
|
|
20
|
-
} from "../interfaces";
|
|
21
|
-
|
|
22
|
-
import type { ControllerBase } from "../bases/controller-base";
|
|
23
|
-
import type { CommandBase } from "../command-bases/command-base";
|
|
24
|
-
|
|
25
|
-
export class Commands extends ObjectBase {
|
|
26
|
-
public static readonly trace:string[] = [];
|
|
27
|
-
|
|
28
|
-
private current: { [ key: string ]: CommandBase } = {};
|
|
29
|
-
private currentArgs: { [ key: string ]: ICommandArgsInterface } = {};
|
|
30
|
-
|
|
31
|
-
private trace:string[] = [];
|
|
32
|
-
|
|
33
|
-
private commands: { [ key: string ]: typeof CommandBase } = {};
|
|
34
|
-
|
|
35
|
-
private onBeforeHooks: IOnHookInterface = {};
|
|
36
|
-
private onBeforeUIHooks: IOnHookInterface = {};
|
|
37
|
-
|
|
38
|
-
private onAfterHooks: IOnHookInterface = {};
|
|
39
|
-
private onAfterOnceHooks: IOnHookInterface = {};
|
|
40
|
-
private onAfterUIHooks: IOnHookInterface = {};
|
|
41
|
-
private onAfterAffectHooks: IOnHookAffectInterface = {};
|
|
42
|
-
|
|
43
|
-
private readonly logger: ILogger;
|
|
44
|
-
|
|
45
|
-
public static getName() {
|
|
46
|
-
return "ZenFlux/Core/Managers/Commands";
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
private static async runCallbacks( callbacks: Array<Function>, args: ICommandArgsInterface = {}, options: ICommandOptionsInterface = {} ) {
|
|
50
|
-
const callbacksLength = callbacks?.length || 0;
|
|
51
|
-
|
|
52
|
-
for ( let i = 0; i < callbacksLength; ++i ) {
|
|
53
|
-
const callback = options.pop === true ? callbacks.pop() : callbacks[ i ];
|
|
54
|
-
|
|
55
|
-
if ( ! callback ) {
|
|
56
|
-
throw new Error( "Callback is not defined." );
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
await callback( args, options );
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
private static hookCommand( stack: any, hookCommand: string, context: TCommandCallbackType | string ) {
|
|
64
|
-
if ( ! stack[ hookCommand ] ) {
|
|
65
|
-
stack[ hookCommand ] = [];
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
stack[ hookCommand ].push( context );
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
public constructor() {
|
|
72
|
-
super();
|
|
73
|
-
|
|
74
|
-
const type = ( this.constructor as typeof Commands );
|
|
75
|
-
|
|
76
|
-
this.logger = new zCore.classes.Logger( type );
|
|
77
|
-
this.logger.startsEmpty( this.constructor );
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public async run( command: string | CommandBase, args: ICommandArgsInterface = {}, options: any = {} ) {
|
|
81
|
-
if ( typeof command === "string" ) {
|
|
82
|
-
command = this.getCommandInstance( command, args, options );
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
this.attachCurrent( command, args );
|
|
86
|
-
|
|
87
|
-
const result = await this.runInstance( command, args, options );
|
|
88
|
-
|
|
89
|
-
this.detachCurrent( command );
|
|
90
|
-
|
|
91
|
-
return result;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
public register( commands: { [ key: string ]: typeof CommandBase }, controller: ControllerBase ) {
|
|
95
|
-
const result: { [ key: string ]: typeof CommandBase } = {};
|
|
96
|
-
|
|
97
|
-
Object.values( commands ).forEach( ( command ) => {
|
|
98
|
-
const commandName = command.getName();
|
|
99
|
-
|
|
100
|
-
if ( this.commands[ commandName ] ) {
|
|
101
|
-
throw new CommandAlreadyRegistered( command );
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
command.setController( controller );
|
|
105
|
-
|
|
106
|
-
this.commands[ commandName ] = command;
|
|
107
|
-
|
|
108
|
-
result[ commandName ] = command;
|
|
109
|
-
} );
|
|
110
|
-
|
|
111
|
-
return result;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
public getAll() {
|
|
115
|
-
return this.commands;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
public getByName( name: string ) {
|
|
119
|
-
return this.commands[ name ];
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
public getLogger() {
|
|
123
|
-
return this.logger;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
public getCommandInstance( name: string, args: ICommandArgsInterface = {}, options = {} ): CommandBase {
|
|
127
|
-
const CommandClass = this.commands[ name ];
|
|
128
|
-
|
|
129
|
-
if ( ! CommandClass ) {
|
|
130
|
-
throw new CommandNotFound( name );
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// @ts-ignore
|
|
134
|
-
return new CommandClass( args, options );
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Used to set hooks that effects only data and not UI.
|
|
139
|
-
*/
|
|
140
|
-
public onBefore( hookCommand: string, callback: TCommandCallbackType ) {
|
|
141
|
-
Commands.hookCommand( this.onBeforeHooks, hookCommand, callback );
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Used to set hooks that effects only UI.
|
|
146
|
-
*/
|
|
147
|
-
public onBeforeUI( hookCommand: string, callback: TCommandCallbackType ) {
|
|
148
|
-
Commands.hookCommand( this.onBeforeUIHooks, hookCommand, callback );
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Used to set hooks that effects only data and not UI.
|
|
153
|
-
*/
|
|
154
|
-
public onAfter( hookCommand: string, callback: TCommandCallbackType ) {
|
|
155
|
-
Commands.hookCommand( this.onAfterHooks, hookCommand, callback );
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Used to set hooks that effects only UI.
|
|
160
|
-
*/
|
|
161
|
-
public onAfterUI( command: string, callback: TCommandCallbackType ) {
|
|
162
|
-
Commands.hookCommand( this.onAfterUIHooks, command, callback );
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Used to set hooks that effects only data and not UI.
|
|
167
|
-
*/
|
|
168
|
-
public onAfterOnce( command: string, callback: TCommandCallbackType ) {
|
|
169
|
-
Commands.hookCommand( this.onAfterOnceHooks, command, callback );
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Used to register a trigger that runs command after `hookCommand` run's.
|
|
174
|
-
*/
|
|
175
|
-
public onAfterAffect( hookCommand: string, affectCommand: string ) {
|
|
176
|
-
Commands.hookCommand( this.onAfterAffectHooks, hookCommand, affectCommand );
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
protected onBeforeRun( command: CommandBase, args: ICommandArgsInterface = {}, options = {} ) {
|
|
180
|
-
if ( this.onBeforeHooks[ command.getName() ] ) {
|
|
181
|
-
const callbacks = this.onBeforeHooks[ command.getName() ];
|
|
182
|
-
callbacks.forEach( ( callback ) => callback( args, options ) );
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if ( this.onBeforeUIHooks[ command.getName() ] ) {
|
|
186
|
-
const callbacks = this.onBeforeUIHooks[ command.getName() ];
|
|
187
|
-
callbacks.forEach( ( callback ) => callback( args, options ) );
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
protected async runInstance( command: CommandBase, args: ICommandArgsInterface = {}, options = {} ) {
|
|
192
|
-
let result: any = null;
|
|
193
|
-
|
|
194
|
-
this.logger.startsWith( this.runInstance, {
|
|
195
|
-
command: command.getName(),
|
|
196
|
-
options,
|
|
197
|
-
args,
|
|
198
|
-
} );
|
|
199
|
-
|
|
200
|
-
this.onBeforeRun( command, args, options );
|
|
201
|
-
|
|
202
|
-
result = await command.run();
|
|
203
|
-
|
|
204
|
-
await this.onAfterRun( command, args, options, result );
|
|
205
|
-
|
|
206
|
-
return result;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
protected async onAfterRun( command: CommandBase, args: ICommandArgsInterface, options: {}, result: any ) {
|
|
210
|
-
options = Object.assign( {}, options );
|
|
211
|
-
if ( this.onAfterAffectHooks[ command.getName() ] ) {
|
|
212
|
-
this.onAfterAffectHooks[ command.getName() ].forEach( ( command ) => {
|
|
213
|
-
args.result = result;
|
|
214
|
-
|
|
215
|
-
result = this.run( command.toString(), args, options );
|
|
216
|
-
} );
|
|
217
|
-
|
|
218
|
-
result = await result;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if ( this.onAfterHooks ) {
|
|
222
|
-
args.result = result;
|
|
223
|
-
|
|
224
|
-
await Commands.runCallbacks(
|
|
225
|
-
Object.assign( [], this.onAfterHooks[ command.getName() ] ),
|
|
226
|
-
args,
|
|
227
|
-
options
|
|
228
|
-
);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
if ( this.onAfterOnceHooks ) {
|
|
232
|
-
await Commands.runCallbacks(
|
|
233
|
-
this.onAfterOnceHooks[ command.getName() ],
|
|
234
|
-
args,
|
|
235
|
-
{
|
|
236
|
-
... options,
|
|
237
|
-
pop: true, // Delete after run.
|
|
238
|
-
}
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
if ( this.onAfterUIHooks ) {
|
|
243
|
-
Commands.runCallbacks(
|
|
244
|
-
Object.assign( [], this.onAfterUIHooks[ command.getName() ] ),
|
|
245
|
-
args,
|
|
246
|
-
options
|
|
247
|
-
);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
return result;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
protected attachCurrent( command: CommandBase, args:any = {} ) {
|
|
254
|
-
this.current[ command.getName() ] = command;
|
|
255
|
-
this.currentArgs[ command.getName() ] = args;
|
|
256
|
-
|
|
257
|
-
this.trace.push( command.getName() );
|
|
258
|
-
Commands.trace.push( command.getName() );
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
protected detachCurrent( command: CommandBase ) {
|
|
262
|
-
delete this.current[ command.getName() ];
|
|
263
|
-
delete this.currentArgs[ command.getName() ];
|
|
264
|
-
|
|
265
|
-
Commands.trace.pop();
|
|
266
|
-
this.trace.pop();
|
|
267
|
-
}
|
|
268
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
* @description Responsible for manging controllers, each controller is global singleton instance.
|
|
4
|
-
*/
|
|
5
|
-
import { ControllerAlreadyRegistered } from "../errors/controller-already-registered";
|
|
6
|
-
|
|
7
|
-
import { ObjectBase } from "../bases/object-base";
|
|
8
|
-
|
|
9
|
-
import type { ControllerBase } from "../bases/controller-base";
|
|
10
|
-
|
|
11
|
-
export class Controllers extends ObjectBase {
|
|
12
|
-
private controllers: { [ key: string ]: ControllerBase } = {};
|
|
13
|
-
|
|
14
|
-
public constructor() {
|
|
15
|
-
super();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public static getName() {
|
|
19
|
-
return "ZenFlux/Core/Managers/Controllers";
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public get( name: string ) {
|
|
23
|
-
return this.controllers[ name ];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public getAll() {
|
|
27
|
-
return this.controllers;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public register( controller: ControllerBase ) {
|
|
31
|
-
if ( this.controllers[ controller.getName() ] ) {
|
|
32
|
-
throw new ControllerAlreadyRegistered( controller );
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Register.
|
|
36
|
-
this.controllers[ controller.getName() ] = controller;
|
|
37
|
-
|
|
38
|
-
return controller;
|
|
39
|
-
}
|
|
40
|
-
}
|