Skip to content

Commit

Permalink
feat: merge processing for refactoring configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
ChoGathK committed Aug 17, 2022
1 parent da8b9cc commit 7a90ab3
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions src/provider/monitor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { existsSync, readFileSync } from 'fs';

import { toCycle } from '@vodyani/utils';
import { uniqueId } from 'lodash';
import { Injectable } from '@nestjs/common';
import { This } from '@vodyani/class-decorator';
import { isArray, isObject, uniqueId } from 'lodash';
import { isValid, toCycle } from '@vodyani/utils';
import { FSWatcher, watch, WatchOptions } from 'chokidar';

import { Method, toHash, WatchInfo } from '../common';
Expand All @@ -26,13 +26,10 @@ export class ConfigMonitor {

@This
public watchConfig(callback: Method, key: string) {
let value = this.config.get(key);
const value = this.config.get(key);
const hashToken = toHash(value);

if (value && (isArray(value) || isObject(value))) {
value = toHash(value);
}

this.configWatchers.set(key, { key, value, callback });
this.configWatchers.set(key, { key, value, hashToken, callback });
}

@This
Expand All @@ -47,12 +44,10 @@ export class ConfigMonitor {

watcher.on('change', (path) => {
try {
this.autoMerge(
'ConfigMonitor.watchFile',
JSON.parse(readFileSync(path, 'utf-8')),
);
const data = readFileSync(path, 'utf-8');
this.autoMerge(JSON.parse(data), 'ConfigMonitor.watchFile');
} catch (error) {
console.error(error);
//
}
});
}
Expand All @@ -66,7 +61,7 @@ export class ConfigMonitor {

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

this.cycleWorkers.set(remoteClientUniqueId, worker);
Expand All @@ -77,40 +72,35 @@ export class ConfigMonitor {
callback: (callback: (details: Record<string, any>) => any) => Promise<void>,
) {
const remoteClientUniqueId = uniqueId('ConfigMonitor.autoSubscribe');
await callback(async (config) => this.autoMerge(remoteClientUniqueId, config));
await callback(async (config) => this.autoMerge(config, remoteClientUniqueId));
}

@This
public autoMerge(source: string, value: any) {
if (!value) {
public autoMerge(value: any, token: string) {
if (!isValid(value)) {
return;
}

const record = this.configMergeWatchers.get(token);
const current = toHash(value);
const record = this.configMergeWatchers.get(source);

if (record !== current) {
this.config.merge(value);
this.configMergeWatchers.set(source, current);
this.configMergeWatchers.set(token, current);
this.check();
}
}

@This
public check() {
this.configWatchers.forEach(async (details) => {
let currentConfig = null;
const { key, value, callback } = details;

currentConfig = this.config.get(key);

if (currentConfig && (isArray(currentConfig) || isObject(currentConfig))) {
currentConfig = toHash(currentConfig);
}
const { key, callback, hashToken } = details;
const currentConfig = this.config.get(key);
const currentHashToken = toHash(currentConfig);

if (value !== currentConfig) {
if (hashToken !== currentHashToken) {
callback(currentConfig);
details.value = currentConfig;
details.hashToken = currentHashToken;
}

this.configWatchers.set(key, details);
Expand Down

0 comments on commit 7a90ab3

Please sign in to comment.