-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor the observer structure
- Loading branch information
Showing
9 changed files
with
129 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,43 @@ | ||
import { IConfigClient, IConfigLoader } from '../common'; | ||
import { IConfigClient, IConfigLoader, IConfigClientSubscriber, toHash } from '../common'; | ||
|
||
export abstract class ConfigClient 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; | ||
} | ||
} | ||
|
||
export class LocalConfigClient implements IConfigClient { | ||
public load<T = any>(loader: IConfigLoader) { | ||
return loader.execute<T>(); | ||
} | ||
|
||
public subscribe(subscriber: IConfigClientSubscriber) { | ||
this.subscriber = subscriber; | ||
} | ||
|
||
public notify(value: any) { | ||
this.subscriber.update(value); | ||
} | ||
|
||
public polling() { | ||
// do somethings ... | ||
} | ||
|
||
public unPolling() { | ||
// do somethings ... | ||
} | ||
|
||
public unSubscribe() { | ||
this.subscriber = null; | ||
} | ||
} | ||
|
||
export class LocalConfigClient extends ConfigClient {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { IConfig, IConfigSubscriber } from '../common'; | ||
import { IConfig, IConfigClientSubscriber } from '../common'; | ||
|
||
export class ConfigSubscriber implements IConfigSubscriber { | ||
export class ConfigClientSubscriber implements IConfigClientSubscriber { | ||
constructor( | ||
private readonly config: IConfig, | ||
) {} | ||
|
||
public update(key: string, value: any) { | ||
this.config.set(key, value); | ||
public update(value: any) { | ||
this.config.merge(value); | ||
} | ||
} |