-
-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 2019-11-25 - 写一个debounce的装饰器 (#69)
2019-11-25 - 写一个debounce的装饰器
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# 每日一题 - 2019-11-25 - 写一个debounce的装饰器 | ||
|
||
### 信息卡片 | ||
|
||
- 时间:2019-11-25 | ||
- tag:`编程题` | ||
|
||
### 问题描述 | ||
实现一个debounce装饰器 | ||
|
||
### 参考实现 | ||
|
||
##### 代码实现 | ||
```typescript | ||
/** | ||
* 装饰器的debounce | ||
* @param delay | ||
*/ | ||
export function debounce(delay: number): Function { | ||
return ( | ||
target: Function, | ||
propertyKey: string, | ||
propertyDesciptor: PropertyDescriptor | ||
) => { | ||
const method = propertyDesciptor.value; | ||
let timer = null; | ||
propertyDesciptor.value = (...args) => { | ||
if (timer) { | ||
clearTimeout(timer); | ||
timer = null; | ||
} | ||
timer = setTimeout(() => method(...args), delay); | ||
}; | ||
return propertyDesciptor; | ||
}; | ||
} | ||
``` | ||
|
||
##### 单元测试 | ||
|
||
``` typescript | ||
import { debounce } from './index'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
let a: any; | ||
let mockFunc: jest.Mock; | ||
beforeEach(() => { | ||
mockFunc = jest.fn(); | ||
class Test { | ||
@debounce(1000) | ||
sayHi() { | ||
mockFunc(); | ||
} | ||
} | ||
a = new Test(); | ||
}); | ||
|
||
describe('debounce:', () => { | ||
test('debounced function should be called after the delay time', () => { | ||
a.sayHi(); | ||
expect(mockFunc).toHaveBeenCalledTimes(0); | ||
jest.advanceTimersByTime(1000); | ||
expect(mockFunc).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('debounced function should not be called before the delay time', () => { | ||
a.sayHi(); | ||
expect(mockFunc).toHaveBeenCalledTimes(0); | ||
let count = 100; | ||
while (count--) { | ||
a.sayHi(); | ||
} | ||
expect(mockFunc).toHaveBeenCalledTimes(0); | ||
|
||
count = 100; | ||
while (count--) { | ||
jest.advanceTimersByTime(999); | ||
a.sayHi(); | ||
} | ||
expect(mockFunc).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
``` | ||
|
||
执行结果 | ||
![](assets/2019-11-25 - 写一个debounce的装饰器-unit-test.png) | ||
|
||
|
||
### 扩展 | ||
|
||
- 写一个throttle的装饰器 | ||
|
||
|
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.