I have several functions that need to access a DB.
(我有一些需要访问数据库的功能。)
const dbProcess1 = (s1: string) => {
console.log(`dbProcess1 is called with a param : ${s1}`)
};
const dbProcess2 = (s2: string, n2: number) => {
console.log(`dbProcess2 is called with params : ${s2}, ${n2}`)
};
const dbProcess3 = (s1: string, s2: string, n2: number) => {
dbProcess1(s1);
dbProcess2(s2, n2);
};
I want to control a spinner before and after these process.
(我想在这些过程之前和之后控制微调器。)
The following decorator is my approach. (下面的装饰器是我的方法。)
const withSpinner = (process: (...params: any[]) => void) => {
return (...params: any[]) => {
console.log('display spinner');
process(...params);
console.log('stop spinner');
}
};
const dbProcess1WithSpinner = withSpinner(dbProcess1);
const dbProcess2WithSpinner = withSpinner(dbProcess2);
const dbProcess3WithSpinner = withSpinner(dbProcess3);
dbProcess1WithSpinner('p1');
dbProcess2WithSpinner('p2','42');
dbProcess3WithSpinner('p1', 'p2', '42');
However, this approach make the type hint useless:
(但是,这种方法使类型提示毫无用处:)
const dbProcess3: (s1: string, s2: string, n2: number) => void
becomes
(变成)
const dbProcess3WithSpinner: (...params: any[]) => void
Any suggestions to make it better?
(有什么建议可以使它更好吗?)
ask by dacapo1142 translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…