| 34 | } |
| 35 | |
| 36 | export function debounce<CB extends Function>(fn:CB, wait:number, leading?:boolean) { |
| 37 | let timeout, context, args; |
| 38 | |
| 39 | let doFn = function doDebounced() { |
| 40 | timeout = undefined; |
| 41 | fn.apply(context, args); |
| 42 | context = undefined; |
| 43 | args = undefined; |
| 44 | } |
| 45 | |
| 46 | let debounced:CB; |
| 47 | if(!leading) { |
| 48 | debounced = function(...argList) { |
| 49 | context = this; |
| 50 | args = argList; |
| 51 | if(timeout) { |
| 52 | window.clearTimeout(timeout); |
| 53 | } |
| 54 | timeout = window.setTimeout(doFn, wait); |
| 55 | } as any; |
| 56 | } else { |
| 57 | debounced = function(...argList) { |
| 58 | context = this; |
| 59 | args = argList; |
| 60 | if(!timeout) { |
| 61 | timeout = window.setTimeout(doFn, wait); |
| 62 | } |
| 63 | } as any; |
| 64 | } |
| 65 | |
| 66 | return debounced; |
| 67 | } |
| 68 | |
| 69 | export function unpad(str:string):string { |
| 70 | if(!str) return str; |