Skip to content

Commit

Permalink
fix: remove invalid dependencies to improve the efficiency of local c…
Browse files Browse the repository at this point in the history
…lient reading
  • Loading branch information
ChoGathK committed Oct 14, 2022
1 parent 48f9843 commit 3f3063a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
31 changes: 19 additions & 12 deletions src/provider/dynamic-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,6 @@ export class DynamicDataSourceConfigObserver<T = any> implements IConfigObserver
private readonly config: ConfigProvider<T>,
) {}

@This
public contrast(key: string, value: any) {
const afterHash = toHash(value);
const beforeHash = this.hash.get(key);

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

@This
public subscribe(key: string, subscriber: IConfigSubscriber) {
const config = this.config.get(key);
Expand Down Expand Up @@ -162,7 +151,25 @@ export class DynamicDataSourceConfigObserver<T = any> implements IConfigObserver
private circularContrast() {
this.keys.forEach((key) => {
const value = this.config.get(key);
this.contrast(key, value);
const allowNotify = this.contrast(key, value);

if (allowNotify) {
this.notify(key, value);
}
});
}

@This
private contrast(key: string, value: any) {
const afterHash = toHash(value);
const beforeHash = this.hash.get(key);

if (afterHash !== beforeHash) {
this.hash.set(key, afterHash);
return true;
}

return false;
}

}
16 changes: 0 additions & 16 deletions src/struct/config-client.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
import { IConfigClient, IConfigClientSubscriber, IConfigLoader } from '@vodyani/core';
import { isValidString, toHash } from '@vodyani/utils';

export class LocalConfigClient implements IConfigClient {
private subscriber: IConfigClientSubscriber;

private hash: string;

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

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

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

Expand Down
22 changes: 22 additions & 0 deletions test/struct/config-client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { resolve } from 'path';

import { describe, it, expect } from '@jest/globals';
import { IConfigLoader } from '@vodyani/core';
import { toHash, isValidString } from '@vodyani/utils';

import { JSONConfigLoader, ConfigClientSubscriber, LocalConfigClient, ConfigProvider } from '../../src';

Expand All @@ -20,9 +22,29 @@ describe('LocalConfigClient', () => {

it('LocalConfigClient', async () => {
class DemoConfigClient extends LocalConfigClient {
private hash: string;

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

public polling(): void {
this.contrast({ poller: 'DemoConfigClient' });
}

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

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

const client = new DemoConfigClient();
Expand Down
22 changes: 22 additions & 0 deletions test/struct/config-handler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { resolve } from 'path';

import { describe, expect, it } from '@jest/globals';
import { IConfigLoader } from '@vodyani/core';
import { toHash, isValidString } from '@vodyani/utils';

import { ConfigClientSubscriber, ConfigHandlerOptions, ConfigProvider, JSONConfigLoader, LocalConfigClient } from '../../src';
import { ConfigArgumentHandler, ConfigClientHandler } from '../../src/struct/config-handler';
Expand All @@ -16,9 +18,29 @@ interface File {
}

class DemoConfigClient extends LocalConfigClient {
private hash: string;

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

public polling(): void {
this.contrast({ poller: 'DemoConfigClient' });
}

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

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

const options: ConfigHandlerOptions = {
Expand Down

0 comments on commit 3f3063a

Please sign in to comment.