Skip to content

Commit

Permalink
feat: take previously irrational design apart & refactor it according…
Browse files Browse the repository at this point in the history
… to design pattern principles
  • Loading branch information
ChoGathK committed Oct 11, 2022
1 parent bc63d16 commit d9c14c1
Show file tree
Hide file tree
Showing 36 changed files with 711 additions and 1,403 deletions.
181 changes: 61 additions & 120 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
"@nestjs/common": "^8.4.7",
"@vodyani/class-decorator": "^8.2.3",
"@vodyani/core": "^8.8.1",
"@vodyani/utils": "^8.5.2",
"chokidar": "3.5.3",
"@vodyani/utils": "^8.6.1",
"js-yaml": "^4.1.0",
"lodash": "4.17.21",
"object-hash": "3.0.0"
},
Expand All @@ -75,6 +75,7 @@
"@commitlint/config-conventional": "16.2.4",
"@nestjs/testing": "8.4.7",
"@types/jest": "27.5.2",
"@types/js-yaml": "^4.0.5",
"@types/lodash": "^4.14.182",
"@types/node": "16.11.56",
"@types/object-hash": "2.2.1",
Expand Down
3 changes: 2 additions & 1 deletion src/common/declare.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as toHash from 'object-hash';
import * as Yaml from 'js-yaml';

export { toHash };
export { toHash, Yaml };
1 change: 0 additions & 1 deletion src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './interface';
export * from './declare';
export * from './type';
68 changes: 68 additions & 0 deletions src/common/interface/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { IConfigSubscriber } from './config';

export interface IClient<T = any, C = any> {
/**
* Close the client.
*
* @publicApi
*/
close: () => void | Promise<void>;
/**
* Get the client instance.
*
* @publicApi
*/
connect: () => T;
/**
* Create the client instance.
*
* @param config C The configuration of client instance
*
* @publicApi
*/
create: (config: C) => T | Promise<T>;
/**
* Redeploy the client instance.
*
* @param config C The configuration of client instance
*
* @publicApi
*/
redeploy: (config: C) => T | Promise<T>;
}

export interface IClientMediator<T = any, C = any> extends IConfigSubscriber {
/**
* Destroy the client from mediator.
*
* @param key string The key of client.
*
* @publicApi
*/
destroy: (key: string) => void | Promise<void>;
/**
* Deploy the client inside to mediator.
*
* @param key string The key of client.
* @param client IClient<T, C> The client.
*
* @publicApi
*/
deploy: (key: string, client: IClient<T, C>) => void | Promise<void>;
/**
* Redeploy the client inside to mediator.
*
* @param key string The key of client.
*
* @publicApi
*/
redeploy: (key: string, config: C) => void | Promise<void>;
/**
* Get the client from mediator.
*
* @param key string The key of client.
*
* @publicApi
*/
getClient: (key: string) => IClient<T, C>;
}
168 changes: 168 additions & 0 deletions src/common/interface/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { ConfigHandlerOptions } from './options';

export interface IConfig<T = any> {
/**
* Deep search the configuration with property key.
*
* @param key string The property of configuration.
*
* @publicApi
*/
get: <V = any>(key: string) => V;
/**
* Search the configuration with property key.
*
* @param key string The property of configuration.
*
* @publicApi
*/
search: <K extends keyof T>(key: K) => T[K]
/**
* Set the configuration with property key.
*
* @param key string The property of configuration.
* @param value any The configuration value.
*
* @publicApi
*/
set: (key: string, value: any) => void;
/**
* Merge the configuration.
*
* @param config any The configuration.
*
* @publicApi
*/
merge: (config: T) => void;
}

export interface IConfigSubscriber {
/**
* Updating Configuration.
*
* @param key string The property of configuration.
* @param value any The configuration value.
*
* @publicApi
*/
update: (key: string, value: any) => void | Promise<void>
}

export interface IConfigLoader {
/**
* Load configuration.
*
* @publicApi
*/
execute: <T = any>() => T;
}

export interface IConfigPoller {
/**
* Open the polling.
*
* @publicApi
*/
execute: () => void;
/**
* Close the polling.
*
* @publicApi
*/
close: () => void | Promise<void>;
}

export interface IConfigObserver {
/**
* Contrasting configuration.
*
* @param key string The property of configuration.
* @param value any The configuration value.
*
* @publicApi
*/
contrast?: (key: string, value: any) => void;
/**
* Register the subscriber inside client by the key of configuration.
*
* @param key string The key of configuration.
* @param value any The configuration value.
*
* @publicApi
*/
subscribe?: (key: string, subscriber: IConfigSubscriber) => void;
/**
* Notify subscribers of configuration updates.
*
* @param key string The key of configuration.
* @param value any The configuration value.
*
* @publicApi
*/
notify?: (key: string, value: any) => void;
/**
* Open the polling.
*
* @publicApi
*/
polling?: () => void | Promise<void>;
/**
* Close the polling.
*
* @publicApi
*/
unPolling?: () => void | Promise<void>;
/**
* Remove the subscriber from client by the key of configuration..
*
* @param key string The key of configuration.
*
* @publicApi
*/
unSubscribe?: (key: string) => void;
}

export interface IConfigClient extends IConfigObserver {
/**
* Load configuration.
*
* @param loader: IConfigLoader<T>
*
* @publicApi
*/
load: <T = any>(loader: IConfigLoader) => T | Promise<T>;
/**
* Register the subscriber inside client.
*
* @param key string The key of configuration.
* @param value any The configuration value.
*
* @publicApi
*/
subscribeAll?: (subscriber: IConfigSubscriber) => void;
/**
* Remove the subscriber from client.
*
* @publicApi
*/
unSubscribeAll?: () => void;
}

export interface IConfigHandler<T = any> {
/**
* Set next handler.
*
* @param handler IConfigHandler
*
* @publicApi
*/
setNext: (handler: IConfigHandler) => IConfigHandler;
/**
* Handler execute.
*
* @param options: ArkOptions
*
* @publicApi
*/
execute: (options: ConfigHandlerOptions<T>) => Promise<void>;
}
6 changes: 3 additions & 3 deletions src/common/interface/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './manager';
export * from './monitor';
export * from './proxy';
export * from './client';
export * from './config';
export * from './options';
88 changes: 0 additions & 88 deletions src/common/interface/manager.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/common/interface/monitor.ts

This file was deleted.

22 changes: 22 additions & 0 deletions src/common/interface/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ModuleMetadata } from '@nestjs/common';

import { IConfigClient, IConfigLoader } from './config';

export interface ConfigClientOptions {
client: IConfigClient;
loader: IConfigLoader;
enablePolling?: boolean;
enableSubscribeAll?: boolean;
}

export interface ConfigHandlerOptions<T = any> {
args: T;
clients: ConfigClientOptions[];
enableDynamicDataSource: boolean;
enableDynamicDataSourcePolling: boolean;
}

export interface ArkOptions<T = any> extends ConfigHandlerOptions<T> {
imports: ModuleMetadata['imports'];
global: boolean;
}

0 comments on commit d9c14c1

Please sign in to comment.