Skip to content

Commit

Permalink
feat: improve the execution efficiency of initialization and operation
Browse files Browse the repository at this point in the history
  • Loading branch information
ChoGathK committed Oct 12, 2022
1 parent de7cd34 commit 616d9e6
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 73 deletions.
12 changes: 6 additions & 6 deletions src/common/interface/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IConfigSubscriber } from './config';

export interface IClient<T = any, C = any> {
export interface IClientAdapter<T = any, C = any> {
/**
* Close the client.
*
Expand All @@ -20,15 +20,15 @@ export interface IClient<T = any, C = any> {
*
* @publicApi
*/
create: (config: C) => T | Promise<T>;
create: (config: C) => void | Promise<void>;
/**
* Redeploy the client instance.
*
* @param config C The configuration of client instance
*
* @publicApi
*/
redeploy: (config: C) => T | Promise<T>;
redeploy: (config: C) => void | Promise<void>;
}

export interface IClientMediator<T = any, C = any> extends IConfigSubscriber {
Expand All @@ -44,11 +44,11 @@ export interface IClientMediator<T = any, C = any> extends IConfigSubscriber {
* Deploy the client inside to mediator.
*
* @param key string The key of client.
* @param client IClient<T, C> The client.
* @param client IClientAdapter<T, C> The client.
*
* @publicApi
*/
deploy: (key: string, client: IClient<T, C>) => void | Promise<void>;
deploy: (key: string, client: IClientAdapter<T, C>) => void | Promise<void>;
/**
* Redeploy the client inside to mediator.
*
Expand All @@ -64,5 +64,5 @@ export interface IClientMediator<T = any, C = any> extends IConfigSubscriber {
*
* @publicApi
*/
getClient: (key: string) => IClient<T, C>;
getClient: (key: string) => IClientAdapter<T, C>;
}
18 changes: 9 additions & 9 deletions src/common/interface/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export interface IConfig<T = any> {
*/
search: <K extends keyof T>(key: K) => T[K]
/**
* Set the configuration with property key.
* Replace 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;
replace: (key: string, value: any) => void;
/**
* Merge the configuration.
*
Expand Down Expand Up @@ -110,7 +110,7 @@ export interface IConfigObserver extends Observer {
*
* @publicApi
*/
subscribe: (key: string, subscriber: IConfigSubscriber) => void | Promise<void>;
subscribe: (key: string, subscriber: IConfigSubscriber) => void;
/**
* Notify subscribers of configuration updates.
*
Expand All @@ -125,13 +125,13 @@ export interface IConfigObserver extends Observer {
*
* @publicApi
*/
polling: () => void | Promise<void>;
polling: () => void;
/**
* Close the polling.
*
* @publicApi
*/
unPolling: () => void | Promise<void>;
unPolling: () => void;
/**
* Remove the subscriber from client by the key of configuration..
*
Expand All @@ -150,7 +150,7 @@ export interface IConfigClient extends Observer {
*
* @publicApi
*/
load: <T = any>(loader: IConfigLoader) => T | Promise<T>;
init: <T = any>(loader: IConfigLoader) => T | Promise<T>;
/**
* Contrasting configuration.
*
Expand All @@ -166,7 +166,7 @@ export interface IConfigClient extends Observer {
*
* @publicApi
*/
subscribe: (subscriber: IConfigClientSubscriber) => void | Promise<void>;
subscribe: (subscriber: IConfigClientSubscriber) => void;
/**
* Notify subscribers of configuration updates.
*
Expand All @@ -180,13 +180,13 @@ export interface IConfigClient extends Observer {
*
* @publicApi
*/
polling: () => void | Promise<void>;
polling: () => void;
/**
* Close the polling.
*
* @publicApi
*/
unPolling: () => void | Promise<void>;
unPolling: () => void;
/**
* Remove the subscriber from client by the key of configuration..
*
Expand Down
9 changes: 4 additions & 5 deletions src/common/interface/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ export interface ConfigClientOptions {

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

export interface ArkOptions<T = any> extends ConfigHandlerOptions<T> {
imports: ModuleMetadata['imports'];
global: boolean;
imports?: ModuleMetadata['imports'];
global?: boolean;
}
10 changes: 5 additions & 5 deletions src/provider/config-manager.ts → src/provider/ark-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FactoryProvider } from '@nestjs/common';
import { This } from '@vodyani/class-decorator';
import { AsyncInjectable, AsyncProvider, AsyncProviderFactory } from '@vodyani/core';

import { ArkOptions } from '../common';
import { ConfigHandlerOptions } from '../common';
import { ConfigClientSubscriber } from '../struct';
import { ConfigArgumentHandler, ConfigClientHandler, DynamicDataSourceConfigObserverHandler } from '../struct/config-handler';

Expand All @@ -11,10 +11,10 @@ import { DynamicDataSourceConfigObserver } from './dynamic-data-source';

@AsyncInjectable
export class ArkManager extends AsyncProvider implements AsyncProviderFactory {
private options: ArkOptions;
private options: ConfigHandlerOptions;

@This
public create(options: ArkOptions): FactoryProvider {
public create(options: ConfigHandlerOptions): FactoryProvider {
const inject: any[] = [ConfigProvider];
const { enableDynamicDataSource } = options;

Expand All @@ -41,12 +41,12 @@ export class ArkManager extends AsyncProvider implements AsyncProviderFactory {

if (this.options.clients) {
const subscriber = new ConfigClientSubscriber(config);
const clientHandler = new ConfigClientHandler(subscriber);
const clientHandler = new ConfigClientHandler(config, subscriber);

handlers.push(clientHandler);
}

if (this.options.enableDynamicDataSourcePolling) {
if (this.options.enableDynamicDataSource) {
const observerHandler = new DynamicDataSourceConfigObserverHandler(observer);

handlers.push(observerHandler);
Expand Down
24 changes: 17 additions & 7 deletions src/provider/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Injectable } from '@nestjs/common';
import { isValidDict, toDeepMatch, toDeepMerge, toDeepRestore } from '@vodyani/utils';
import { This } from '@vodyani/class-decorator';
import { isValidDict, toDeepMatch, toDeepMerge, toDeepSave } from '@vodyani/utils';
import { cloneDeep } from 'lodash';

import { IConfig } from '../common';

Expand All @@ -9,25 +11,33 @@ export class ConfigProvider<T = any> implements IConfig<T> {

private readonlyConfig: T;

@This
public get<V = any>(key: string) {
return toDeepMatch<V>(this.readonlyConfig, key);
}

@This
public search<K extends keyof T>(key: K) {
return this.readonlyConfig[key];
}

public set(key: string, value: any) {
this.merge(toDeepRestore<T>(value, key));
@This
public replace(key: string, value: any) {
toDeepSave(this.writeConfig, value, key);
this.updateReadonlyConfig();
}

@This
public merge(config: T) {
if (isValidDict(config)) {
this.writeConfig = toDeepMerge(this.writeConfig, config);

this.readonlyConfig = this.writeConfig;

Object.freeze(this.readonlyConfig);
this.updateReadonlyConfig();
}
}

@This
private updateReadonlyConfig() {
this.readonlyConfig = cloneDeep(this.writeConfig);
Object.freeze(this.readonlyConfig);
}
}
22 changes: 10 additions & 12 deletions src/provider/dynamic-data-source.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Injectable } from '@nestjs/common';
import { This } from '@vodyani/class-decorator';
import { CircularHandler, isValid, circular } from '@vodyani/utils';
import { CircularHandler, circular } from '@vodyani/utils';

import { IClient, IClientMediator, IConfigObserver, IConfigSubscriber, toHash } from '../common';
import { IClientAdapter, IClientMediator, IConfigObserver, IConfigSubscriber, toHash } from '../common';

import { ConfigProvider } from './config';

@Injectable()
export class AsyncDynamicDataSourceProvider<T = any, C = any> implements IClientMediator<T, C> {
private readonly clients = new Map<string, IClient<T, C>>();
private readonly clients = new Map<string, IClientAdapter<T, C>>();

private readonly keys = new Set<string>();

Expand All @@ -27,7 +27,7 @@ export class AsyncDynamicDataSourceProvider<T = any, C = any> implements IClient
}

@This
public async deploy(key: string, client: IClient<T, C>) {
public async deploy(key: string, client: IClientAdapter<T, C>) {
const config = this.config.get<C>(key);

await client.create(config);
Expand All @@ -52,14 +52,14 @@ export class AsyncDynamicDataSourceProvider<T = any, C = any> implements IClient
}

@This
public update(key: string, value: any) {
this.redeploy(key, value);
public async update(key: string, value: any) {
await this.redeploy(key, value);
}
}

@Injectable()
export class DynamicDataSourceProvider<T = any, C = any> implements IClientMediator<T, C> {
private readonly clients = new Map<string, IClient<T, C>>();
private readonly clients = new Map<string, IClientAdapter<T, C>>();

private readonly keys = new Set<string>();

Expand All @@ -78,7 +78,7 @@ export class DynamicDataSourceProvider<T = any, C = any> implements IClientMedia
}

@This
public deploy(key: string, client: IClient<T, C>) {
public deploy(key: string, client: IClientAdapter<T, C>) {
const config = this.config.get<C>(key);

client.create(config);
Expand Down Expand Up @@ -152,10 +152,6 @@ export class DynamicDataSourceConfigObserver<T = any> implements IConfigObserver

@This
public polling() {
if (isValid(this.poller)) {
this.unPolling();
}

this.poller = circular(this.circularContrast, 1000);
}

Expand All @@ -176,6 +172,8 @@ export class DynamicDataSourceConfigObserver<T = any> implements IConfigObserver
this.keys.forEach((key) => {
const value = this.config.get(key);

console.log(value);

this.contrast(key, value);
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/provider/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './config-manager';
export * from './ark-manager';
export * from './config';
export * from './dynamic-data-source';
26 changes: 16 additions & 10 deletions src/struct/config-client.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import { isValidString } from '@vodyani/utils';

import { IConfigClient, IConfigLoader, IConfigClientSubscriber, toHash } from '../common';

export abstract class ConfigClient implements IConfigClient {
export abstract class AbstractConfigClient implements IConfigClient {
private subscriber: IConfigClientSubscriber;

private hash: string;

public contrast(value: any) {
const afterHash = toHash(value);
const beforeHash = this.hash;

if (afterHash !== beforeHash) {
this.notify(value);
this.hash = afterHash;
if (isValidString(this.hash)) {
const afterHash = toHash(value);
const beforeHash = this.hash;

if (afterHash !== beforeHash) {
this.notify(value);
this.hash = afterHash;
}
}
}

public load<T = any>(loader: IConfigLoader) {
return loader.execute<T>();
public init<T = any>(loader: IConfigLoader) {
const result = loader.execute<T>();
this.hash = toHash(result);
return result;
}

public subscribe(subscriber: IConfigClientSubscriber) {
Expand All @@ -40,4 +46,4 @@ export abstract class ConfigClient implements IConfigClient {
}
}

export class LocalConfigClient extends ConfigClient {}
export class LocalConfigClient extends AbstractConfigClient {}

0 comments on commit 616d9e6

Please sign in to comment.