Skip to content

Commit

Permalink
feat: refactor the running structure and processing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ChoGathK committed Sep 7, 2022
1 parent 75e3a3c commit c738ff6
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 76 deletions.
46 changes: 23 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@
]
},
"dependencies": {
"@vodyani/class-decorator": "^8.2.1",
"@vodyani/core": "^8.6.6",
"@vodyani/utils": "^8.5.1",
"@vodyani/class-decorator": "^8.2.2",
"@vodyani/core": "^8.6.8",
"@vodyani/utils": "^8.5.2",
"chokidar": "3.5.3",
"lodash": "4.17.21",
"object-hash": "3.0.0"
Expand Down
2 changes: 1 addition & 1 deletion src/common/interface/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export interface WatchInfo {
key: string;
value: any;
hashToken: string;
callback: (config: any) => any;
callback: <T = any>(config: Partial<T>) => any;
}
5 changes: 2 additions & 3 deletions src/common/interface/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Client, AsyncClient } from '@vodyani/core';
import { Method, PromiseMethod } from '@vodyani/utils';

import { DeployClient, DeployAsyncClient } from '../type';

export interface IClientProxy<T = any, O = any> {
getClient: () => Client<T>;
deploy: DeployClient<T, O>;
redeploy: (option: O) => void;
close: Method<void>;
close: (...args: any[]) => any;
}

export interface IAsyncClientProxy<T = any, O = any> {
getClient: () => AsyncClient<T>;
deploy: DeployAsyncClient<T, O>;
redeploy: (option: O) => Promise<void>;
close: PromiseMethod<void>;
close: (...args: any[]) => Promise<any>;
}
23 changes: 10 additions & 13 deletions src/provider/config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Injectable } from '@vodyani/core';
import { cloneDeep, isObject } from 'lodash';
import { ArgumentValidator, CustomValidated, Required, This } from '@vodyani/class-decorator';
import { toDeepMerge, toDeepMatch, toDeepRestore, isValidObject, isKeyof, Dictionary } from '@vodyani/utils';
import { toDeepMerge, toDeepMatch, toDeepRestore, isValidObject, isKeyof } from '@vodyani/utils';

@Injectable()
export class ConfigProvider<T = any> {
/**
* The configuration details store.
*/
private store: Dictionary<T> = Object();
private store: Partial<T> = Object();

/**
* Deep query the configuration for the given key.
Expand All @@ -17,9 +17,7 @@ export class ConfigProvider<T = any> {
*/
@This
@ArgumentValidator()
public match(
@Required() key: string,
) {
public match(@Required() key: string) {
const result = toDeepMatch(this.store, key);
return isObject(result) ? cloneDeep(result) as any : result;
}
Expand All @@ -32,10 +30,8 @@ export class ConfigProvider<T = any> {
*/
@This
@ArgumentValidator()
public get<K extends keyof Dictionary<T>>(
@Required() key: K,
) {
if (isKeyof(this.store, key as string | number)) {
public get<K extends keyof Partial<T>>(@Required() key: K) {
if (isKeyof(this.store, key as string)) {
const result = this.store[key];
return isObject(result) ? cloneDeep(result) : result;
}
Expand All @@ -48,7 +44,7 @@ export class ConfigProvider<T = any> {
public set(
@Required() key: string,
@Required() value: any,
): void {
) {
const result = toDeepRestore(value, key);
this.merge(result);
}
Expand All @@ -57,9 +53,10 @@ export class ConfigProvider<T = any> {
*/
@This
@ArgumentValidator()
public merge(
@CustomValidated(isValidObject, 'value must be object !') value: object,
): void {
public merge<T = any>(
@CustomValidated(isValidObject, 'value must be object !') value: Partial<T>,
) {
this.store = toDeepMerge(this.store, cloneDeep(value));
}
}

6 changes: 4 additions & 2 deletions src/provider/dynamic-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ export class DynamicDataSourceProvider<T = any, O = any> {
) {}

@This
public getInstance(key: string): T {
@ArgumentValidator()
public getInstance(@Required() key: string): T {
return this.getClient(key)?.getInstance() || null;
}

@This
public getClient(key: string): Client<T> {
@ArgumentValidator()
public getClient(@Required() key: string): Client<T> {
return this.store.has(key) ? this.store.get(key).getClient() : null;
}

Expand Down
8 changes: 4 additions & 4 deletions src/provider/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readFileSync } from 'fs';
import { uniqueId } from 'lodash';
import { Injectable } from '@vodyani/core';
import { This } from '@vodyani/class-decorator';
import { isValid, PromiseMethod, toCycle } from '@vodyani/utils';
import { isValid, circular } from '@vodyani/utils';
import { FSWatcher, watch, WatchOptions } from 'chokidar';

import { toHash, WatchInfo } from '../common';
Expand Down Expand Up @@ -45,16 +45,16 @@ export class ConfigMonitor {

@This
public autoCycleSync(
callback: PromiseMethod<any>,
callback: (...args: any[]) => Promise<any>,
interval = 1000,
) {
const token = uniqueId('ConfigMonitor.autoCycleSync');

const worker = toCycle(
const worker = circular(
async () => {
this.autoMerge(await callback(), token);
},
{ interval },
interval,
);

this.cycleWorkers.set(token, worker);
Expand Down
7 changes: 2 additions & 5 deletions test/async-dynamic-data-source.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { toDelay } from '@vodyani/utils';
import { sleep } from '@vodyani/utils';
import { This } from '@vodyani/class-decorator';
import { describe, it, expect } from '@jest/globals';
import { AsyncClient, AsyncClientAdapter } from '@vodyani/core';
Expand Down Expand Up @@ -34,13 +33,11 @@ class ClientManager implements AsyncClientAdapter<Demo> {
private client: DemoClient;

@This
// @ts-ignore
public getClient() {
return this.client;
}

@This
// @ts-ignore
public async create(count: number, ...args: any[]) {
this.client = new DemoClient(count, args);
return this.client;
Expand Down Expand Up @@ -96,7 +93,7 @@ describe('AsyncDynamicDataSourceProvider', () => {

monitor.autoMerge({ AsyncDynamicDataSource: 2 }, 'async_merge');

await toDelay(200);
await sleep(200);

expect(provider.getInstance(key_01).getCount()).toBe(2);

Expand Down
25 changes: 8 additions & 17 deletions test/config-manager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { resolve } from 'path';

import { toDelay } from '@vodyani/utils';
import { sleep } from '@vodyani/utils';
import { This } from '@vodyani/class-decorator';
import { describe, it, expect } from '@jest/globals';
import { Test, TestingModule } from '@nestjs/testing';
Expand All @@ -12,23 +12,19 @@ import { ConfigProvider } from '../src/provider/config';
import { ArkManager, ConfigMonitor } from '../src/provider';

@Injectable()
// @ts-ignore
class RemoteClient1 implements RemoteConfigClient {
@This
// @ts-ignore
async init() {
return null;
return null as any;
}

@This
// @ts-ignore
async sync() {
return { name1: 'RemoteClient1' };
}

@This
// @ts-ignore
subscribe(cb) {
subscribe(__: string, cb: (value: any) => any) {
return cb({ name1: 'RemoteClient1' });
}
}
Expand All @@ -37,36 +33,30 @@ class RemoteClient1 implements RemoteConfigClient {
exports: [RemoteClient1],
providers: [RemoteClient1],
})
// @ts-ignore
class RemoteModule1 {}

@Injectable()
// @ts-ignore
class RemoteClient2 implements RemoteConfigClient {
@This
// @ts-ignore
async init() {
return null;
return null as any;
}

@This
// @ts-ignore
async sync() {
return { name2: 'RemoteClient2' };
}

@This
// @ts-ignore
subscribe(cb) {
return cb({ name2: 'RemoteClient2' });
subscribe(__: string, cb: (value: any) => any) {
return cb('RemoteClient2');
}
}

@Module({
exports: [RemoteClient2],
providers: [RemoteClient2],
})
// @ts-ignore
class RemoteModule2 {}

let config: ConfigProvider = Object();
Expand Down Expand Up @@ -98,14 +88,15 @@ describe('ArkModule', () => {
provider: RemoteClient2,
initArgs: [],
enableSubscribe: true,
subscribeKeys: ['name2'],
},
],
})],
}).compile();

config = moduleRef.get<ConfigProvider>(ArkManager.getToken());

await toDelay(1000);
await sleep(1000);

expect(config.get('name1')).toBe('RemoteClient1');
expect(config.get('name2')).toBe('RemoteClient2');
Expand Down

0 comments on commit c738ff6

Please sign in to comment.