Skip to content

Commit

Permalink
feat: optimize the naming and interface structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ChoGathK committed Oct 12, 2022
1 parent 691f755 commit fb2b64c
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 100 deletions.
66 changes: 31 additions & 35 deletions src/common/interface/client.ts
@@ -1,72 +1,68 @@
export interface Client<T = any> {
import { IConfigSubscriber } from './config';

export interface IClientAdapter<T = any, C = any> {
/**
* Gets the client instance object.
* Close the client.
*
* @publicApi
*/
getInstance: () => T;
close: () => void | Promise<void>;
/**
* Close the client instance.
* Get the client instance.
*
* @publicApi
*/
close: (...args: any[]) => void;
}

export interface AsyncClient<T = any> {
connect: () => T;
/**
* Gets the client instance object.
* Create the client instance.
*
* @param config C The configuration of client instance
*
* @publicApi
*/
getInstance: () => T;
create: (config: C) => void | Promise<void>;
/**
* Close the client instance.
* Redeploy the client instance.
*
* @param config C The configuration of client instance
*
* @publicApi
*/
close: (...args: any[]) => Promise<void>;
redeploy: (config: C) => void | Promise<void>;
}

export interface ClientAdapter<T = any, O = any> {
export interface IClientMediator<T = any, C = any> extends IConfigSubscriber {
/**
* Creating the client.
* Destroy the client from mediator.
*
* @publicApi
*/
create: (options: O) => Client<T>;
/**
* Gets the client.
* @param key string The key of client.
*
* @publicApi
*/
getClient: (key: string) => Client<T>;
destroy: (key: string) => void | Promise<void>;
/**
* Gets the client instance object.
* Deploy the client inside to mediator.
*
* @publicApi
*/
getInstance: (key: string) => T;

}

export interface AsyncClientAdapter<T = any, O = any> {
/**
* Creating the client.
* @param key string The key of client.
* @param client IClientAdapter<T, C> The client.
*
* @publicApi
*/
create: (options: O) => Promise<AsyncClient<T>>;
deploy: (key: string, client: IClientAdapter<T, C>) => void | Promise<void>;
/**
* Gets the client.
* Redeploy the client inside to mediator.
*
* @param key string The key of client.
*
* @publicApi
*/
getClient: (key: string) => AsyncClient<T>;
redeploy: (key: string, config: C) => void | Promise<void>;
/**
* Gets the client instance object.
* Get the client from mediator.
*
* @param key string The key of client.
*
* @publicApi
*/
getInstance: (key: string) => T;
getClient: (key: string) => IClientAdapter<T, C>;
}
194 changes: 194 additions & 0 deletions src/common/interface/config.ts
@@ -0,0 +1,194 @@
interface Observer {
contrast: (...args: any[]) => void;
subscribe: (...args: any[]) => void | Promise<void>;
notify: (...args: any[]) => void;
polling: (...args: any[]) => void | Promise<void>;
unPolling: (...args: any[]) => void | Promise<void>;
unSubscribe: (...args: any[]) => void;
}

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]
/**
* Replace the configuration with property key.
*
* @param key string The property of configuration.
* @param value any The configuration value.
*
* @publicApi
*/
replace: (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 IConfigClientSubscriber {
/**
* Updating Configuration.
*
* @param value any The configuration value.
*
* @publicApi
*/
update: (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 extends Observer {
/**
* 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 subscriber IConfigSubscriber The configuration subscriber..
*
* @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;
/**
* Close the polling.
*
* @publicApi
*/
unPolling: () => 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 Observer {
/**
* Load configuration.
*
* @param loader: IConfigLoader<T>
*
* @publicApi
*/
init: <T = any>(loader: IConfigLoader) => T | Promise<T>;
/**
* Contrasting configuration.
*
* @param value any The configuration value.
*
* @publicApi
*/
contrast: (value: any) => void;
/**
* Register the subscriber inside client by the key of configuration.
*
* @param subscriber IConfigClientSubscriber The configuration client subscriber.
*
* @publicApi
*/
subscribe: (subscriber: IConfigClientSubscriber) => void;
/**
* Notify subscribers of configuration updates.
*
* @param value any The configuration value.
*
* @publicApi
*/
notify: (value: any) => void;
/**
* Open the polling.
*
* @publicApi
*/
polling: () => void;
/**
* Close the polling.
*
* @publicApi
*/
unPolling: () => void;
/**
* Remove the subscriber from client by the key of configuration..
*
* @publicApi
*/
unSubscribe: () => void;
}
4 changes: 2 additions & 2 deletions src/common/interface/index.ts
@@ -1,3 +1,3 @@
export * from './async-provider';
export * from './client';
export * from './remote-config';
export * from './config';
export * from './provider';
@@ -1,6 +1,6 @@
import { FactoryProvider } from '@nestjs/common';

export interface AsyncProviderFactory {
export interface IAsyncProviderFactory {
/**
* Create a factory provider by specifying the creation parameters externally.
*
Expand Down
37 changes: 0 additions & 37 deletions src/common/interface/remote-config.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/decorator/async-inject.ts
@@ -1,6 +1,6 @@
import { Inject, Type } from '@nestjs/common';

import { AsyncProvider, StaticStore } from '../struct';
import { AsyncProviderFactory, FactoryProviderStore } from '../struct';

/**
* Use this decorator to handle dependency management for asynchronous provider factory classes.
Expand All @@ -9,8 +9,8 @@ import { AsyncProvider, StaticStore } from '../struct';
*
* @publicApi
*/
export function AsyncInjectable(target: Type<AsyncProvider>) {
StaticStore.set(target.name, Symbol(target.name));
export function AsyncInjectable(target: Type<AsyncProviderFactory>) {
FactoryProviderStore.set(target.name, Symbol(target.name));
}
/**
* Use this decorator to inject the asynchronous provider into the class instantiation process.
Expand All @@ -19,6 +19,6 @@ export function AsyncInjectable(target: Type<AsyncProvider>) {
*
* @publicApi
*/
export function AsyncInject(target: Type<AsyncProvider>) {
export function AsyncInject(target: Type<AsyncProviderFactory>) {
return Inject((target as any).getToken());
}
2 changes: 1 addition & 1 deletion src/index.ts
@@ -1,3 +1,3 @@
export { AsyncProvider } from './struct';
export { AsyncProviderFactory } from './struct';
export * from './decorator';
export * from './common';
14 changes: 0 additions & 14 deletions src/struct/async-provider-factory.ts

This file was deleted.

0 comments on commit fb2b64c

Please sign in to comment.