正在尝试为我们的代码库中广泛使用的遥测库创建扩展类。
在下面的沙箱中模拟了我创建扩展的方式。 Logger
Crux是,我们有一个库tat,它为我们提供了一个单例记录器。
//centralized client governed outside or project - but inheritance/overriding is possible,
class singletonClient {
// this method's consumption is widespread, as a new param is introduced,lot of code changes required.
// public log(some: string) {
public log(some: string, extensionParams: any) {
// is there any cleaner way to pass the param from the context object from context class? (like decorator or something)
console.log(some, extensionParams);
}
}
//singleton client used throughout the project - govered outside our project
const _client = new singletonClient();
我的要求是向库中添加扩展,并且正在寻找最优雅/更干净的方法(可能是装饰器或闭包可能会有所帮助,但我对TS的了解非常有限)
我要上的课是
class context {
public extensionParams: any;
public cli: singletonClient;
constructor(name: string) {
this.extensionParams = { name: name };
this.cli = _client;
}
}
// const _context = new context("Product A");
const _context1 = new context("Product B");
const _context2 = new context("Product C");
//This change is difficult to make
_client.log("entry", _context1.extensionParams);
_client.log("entry", _context2.extensionParams);
//This is easier => coz only one line change in the start of the js file.
//_context1.cli.log("entry"); // but how to pass in the extension params without modifying the signatures