Skip to content

Commit

Permalink
fix: remote config client subscribe argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ChoGathK committed Sep 19, 2022
1 parent 18c89f9 commit f8761d1
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 113 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
},
"dependencies": {
"@vodyani/class-decorator": "^8.2.2",
"@vodyani/core": "^8.6.9",
"@vodyani/core": "^8.7.1",
"@vodyani/utils": "^8.5.2",
"chokidar": "3.5.3",
"lodash": "4.17.21",
Expand Down
4 changes: 0 additions & 4 deletions src/common/interface/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ export interface RemoteConfigOptions {
* The boolean of enable remote config center cycle sync.
*/
enableCycleSync?: boolean;
/**
* The key information in the remote config center for subscription methods.
*/
subscribeInfo?: RemoteConfigSubscribeInfo[];
/**
* Periodic synchronization interval. The default value is `1000` milliseconds.
*/
Expand Down
19 changes: 9 additions & 10 deletions src/provider/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cloneDeep } from 'lodash';
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 } from '@vodyani/utils';
import { toDeepMerge, toDeepMatch, toDeepRestore, isValidObject, isKeyof, isValidDict } from '@vodyani/utils';

@Injectable()
export class ConfigProvider<T = any> {
Expand All @@ -17,9 +17,9 @@ export class ConfigProvider<T = any> {
*/
@This
@ArgumentValidator()
public match(@Required() key: string) {
public match<V = any>(@Required() key: string): Partial<V> {
const result = toDeepMatch(this.store, key);
return isObject(result) ? cloneDeep(result) as any : result;
return isValidDict(result) ? cloneDeep(result) : result;
}
/**
* Get the configuration for the given key.
Expand All @@ -33,17 +33,17 @@ export class ConfigProvider<T = any> {
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;
return isValidDict(result) ? cloneDeep(result) : result;
}
}
/**
* set the configuration for the given key.
*/
@This
@ArgumentValidator()
public set(
public set<V = any>(
@Required() key: string,
@Required() value: any,
@Required() value: Partial<V>,
) {
const result = toDeepRestore(value, key);
this.merge(result);
Expand All @@ -53,10 +53,9 @@ export class ConfigProvider<T = any> {
*/
@This
@ArgumentValidator()
public merge<T = any>(
@CustomValidated(isValidObject, 'value must be object !') value: Partial<T>,
public merge<V = any>(
@CustomValidated(isValidObject, 'value must be object !') value: Partial<V>,
) {
this.store = toDeepMerge(this.store, cloneDeep(value));
}
}

22 changes: 11 additions & 11 deletions src/provider/dynamic-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ConfigProvider } from './config';

@Injectable()
export class DynamicDataSourceProvider<T = any, O = any> {
private readonly store: Map<string, IClientProxy<T>> = new Map();
private readonly store: Map<string, IClientProxy<T, Partial<O>>> = new Map();

constructor(
@AsyncInject(ArkManager)
Expand All @@ -34,18 +34,18 @@ export class DynamicDataSourceProvider<T = any, O = any> {
@This
@ArgumentValidator()
public deploy(
@Required() callback: CreateClient<T, O>,
@Required() create: CreateClient<T, Partial<O>>,
@Required() configKey: string,
...args: any[]
) {
const currentArgs = toConvert(args, { default: [] });
const options = this.config.match(configKey);
const proxy = new ClientProxy<T, O>();
const proxy = new ClientProxy<T, Partial<O>>();

proxy.deploy(callback, options, ...currentArgs);
proxy.deploy(create, options, ...currentArgs);

this.store.set(configKey, proxy);
this.monitor.watchConfig(proxy.redeploy, configKey);
this.monitor.setCheck(proxy.redeploy, configKey);
}

@This
Expand All @@ -59,7 +59,7 @@ export class DynamicDataSourceProvider<T = any, O = any> {

@Injectable()
export class AsyncDynamicDataSourceProvider<T = any, O = any> {
private readonly store = new Map<string, IAsyncClientProxy<T, O>>();
private readonly store = new Map<string, IAsyncClientProxy<T, Partial<O>>>();

constructor(
@AsyncInject(ArkManager)
Expand All @@ -80,22 +80,22 @@ export class AsyncDynamicDataSourceProvider<T = any, O = any> {
@This
@ArgumentValidator()
public async deploy(
@Required() callback: CreateAsyncClient<T, O>,
@Required() create: CreateAsyncClient<T, Partial<O>>,
@Required() configKey: string,
...args: any[]
) {
const currentArgs = toConvert(args, { default: [] });
const option = this.config.match(configKey);
const proxy = new AsyncClientProxy<T, O>();
const proxy = new AsyncClientProxy<T, Partial<O>>();

await proxy.deploy(callback, option, ...currentArgs);
await proxy.deploy(create, option, ...currentArgs);

this.store.set(configKey, proxy);
this.monitor.watchConfig(proxy.redeploy, configKey);
this.monitor.setCheck(proxy.redeploy, configKey);
}

@This
public async clear(key: string) {
public async close(key: string) {
if (this.store.has(key)) {
await this.store.get(key).close();
this.store.delete(key);
Expand Down
10 changes: 3 additions & 7 deletions src/provider/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFileSync } from 'fs';

import { Injectable } from '@vodyani/core';
import { isValidObject } from '@vodyani/utils';
import { ArgumentValidator, CustomValidated, Required, This } from '@vodyani/class-decorator';
import { ArgumentValidator, Required, This } from '@vodyani/class-decorator';

import { ConfigProvider } from './config';

Expand All @@ -14,17 +14,13 @@ export class ConfigHandler {

@This
@ArgumentValidator()
public init(
@CustomValidated(isValidObject, 'params must be object !') params: Record<string, any>,
): void {
public init<V = any>(params: Partial<V>): void {
this.config.merge(params);
}

@This
@ArgumentValidator()
public deploy(
@Required() path: string,
) {
public deploy(@Required() path: string) {
try {
const config = JSON.parse(readFileSync(path, 'utf8'));

Expand Down
22 changes: 9 additions & 13 deletions src/provider/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ export class ArkManager extends AsyncProvider implements AsyncProviderFactory {
const { local, remote } = this.options;

if (!local) {
throw new Error('ArkManager: local is a required parameter!');
throw new Error('local is a required parameter!');
}

const { env, params, path, enableWatch, watchOptions } = local;

if (!path) {
throw new Error('ArkManager: local.path is a required parameter!');
throw new Error('local.path is a required parameter!');
}

configHandler.init({ env: env.current, ...params });
Expand All @@ -65,8 +65,8 @@ export class ArkManager extends AsyncProvider implements AsyncProviderFactory {
configHandler.deploy(currentPath);

if (enableWatch) {
configMonitor.watchFile(defaultPath, watchOptions);
configMonitor.watchFile(currentPath, watchOptions);
configMonitor.setFileCheck(defaultPath, watchOptions);
configMonitor.setFileCheck(currentPath, watchOptions);
}

if (remote) {
Expand All @@ -85,11 +85,11 @@ export class ArkManager extends AsyncProvider implements AsyncProviderFactory {
const currentPath = `${path}/${env.current}.json`;

if (!existsSync(defaultPath)) {
throw new Error(`ArkManager.deployLocalPath: The file at ${defaultPath} does not exist!`);
throw new Error(`The file at ${defaultPath} does not exist!`);
}

if (!existsSync(currentPath)) {
throw new Error(`ArkManager.deployLocalPath: The file at ${currentPath} does not exist!`);
throw new Error(`The file at ${currentPath} does not exist!`);
}

return { currentPath, defaultPath };
Expand Down Expand Up @@ -123,16 +123,12 @@ export class ArkManager extends AsyncProvider implements AsyncProviderFactory {
options: RemoteConfigOptions[],
) {
await Promise.all(options.map(
async ({ enableCycleSync, enableSubscribe, subscribeInfo, cycleSyncInterval }, index) => {
async ({ enableCycleSync, enableSubscribe, cycleSyncInterval }, index) => {
const client = clients[index];

if (client) {
if (enableSubscribe && isValidArray(subscribeInfo)) {
await Promise.all(
subscribeInfo.map(
({ key, args = [] }) => monitor.autoSubscribe(client.subscribe, key, ...args),
),
);
if (enableSubscribe) {
monitor.autoSubscribe(client.subscribe);
}

if (enableCycleSync) {
Expand Down

0 comments on commit f8761d1

Please sign in to comment.