sim*_*eon 2 typescript javascript-decorators
用例:我想知道一个函数在打字稿中执行需要多长时间。我想使用装饰器来达到这个目的。我希望装饰器应该返回时间,以便(我可以进一步使用它),而不仅仅是打印它。
例如:
export function createTimestamps(message: string) {
return function (target: any, name: string, descriptor: PropertyDescriptor) {
const method = descriptor.value;
descriptor.value = async function () {
const startTime = new Date(Date.now());
console.log(
`${message} started at: ${startTime.toLocaleString("en-GB")}`
);
await method.apply(this);
const endTime = new Date(Date.now());
console.log(
`${message} completed at: ${endTime.toLocaleString("en-GB")}`
);
console.log(
`${message} took ${
endTime.getTime() - startTime.getTime()
}ms to complete.`
);
};
};
}
Run Code Online (Sandbox Code Playgroud)
如果我使用上面的函数作为装饰器,那么我希望装饰器返回“endTime.getTime() - startTime.getTime()”,以便我可以进一步使用它。
@creaTimestamp
async newfunc():string{
return "typescript";
}
Run Code Online (Sandbox Code Playgroud)
现在,当我调用上面的函数时,await newfunc()。我可以获取执行时间值以及它返回的字符串吗?
另外,我有很多函数,我想避免在每个函数之上添加装饰器,因此在调用它们时,我想确保装饰器运行并返回计时。有人可以指出我这样的图书馆(如果存在)吗?
有人可以分享一些对上述场景的见解吗,我对装饰器很陌生。谢谢!
使用打字稿装饰器不可能返回附加数据。
主要问题是打字稿装饰器无法更改函数的返回类型。并且一个函数只能返回一个值。
那么,为什么这很重要?
例如:
class Foo {
async newfunc(): string {
return "typescript";
}
}
const lang = await new Foo().newFunc()
console.log(`${lang} is a cool language`)
Run Code Online (Sandbox Code Playgroud)
在此示例中lang是 a string,并且您的程序期望它是 a string。如果您随后将装饰器放在该函数上,并且您希望返回一个字符串以及计时信息,则必须返回类似以下内容的内容:
{ result: returnValueOfFunctionHere, elapsed: elapsedMsHere }
Run Code Online (Sandbox Code Playgroud)
但那不再是了string。现在您必须深入了解该result属性才能获取该字符串。这意味着您已经通过应用装饰器更改了函数的返回类型。目前这是不允许的。
这就是示例将信息记录到控制台而不是返回它的原因。
但正如 @Papooch 所建议的,您可以通过将经过的时间存储在元数据reflect-metadata中。您可以将其存储在正在测量的函数的唯一元数据键上。
reflect-metadata是一个对此类事情有用的包。请在此处阅读有关如何使用它的更多信息。
import 'reflect-metadata'
// Create a key to store the metadata under. This should be a symbol.
const lastCallElapsedKey = Symbol('lastCallElapsedKey')
function createTimestamps(message: string) {
return function (target: any, name: string, descriptor: PropertyDescriptor) {
const method = descriptor.value;
descriptor.value = async function () {
const startTime = new Date(Date.now());
const result = await method.apply(this);
const endTime = new Date(Date.now());
const elpased = endTime.getTime() - startTime.getTime()
// Write the elpased time to the new function's metadata.
Reflect.defineMetadata(lastCallElapsedKey, elpased, descriptor.value)
return result
};
};
}
// Get the elapsed time from the metadata.
function getElapsed(fn: (...args: unknown[]) => unknown): number | undefined {
return Reflect.getMetadata(lastCallElapsedKey, fn)
}
class Foo {
// Emulate an async call
@createTimestamps("test")
async newfunc(): Promise<string> {
return new Promise(resolve => {
setTimeout(() => resolve('typescript'), 250)
})
}
}
async function test() {
const foo = new Foo()
console.log(await foo.newfunc())
console.log(getElapsed(foo.newfunc)) // 250
}
test()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2539 次 |
| 最近记录: |