Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / lib / cwl-svg / utils / perf.ts
1 export class Perf {
2
3     static DEFAULT_THROTTLE = 1;
4
5     public static throttle(fn: Function, threshold = Perf.DEFAULT_THROTTLE, context?: any): Function {
6         let last: any, deferTimer: any;
7
8         return function () {
9             // @ts-ignore
10             const scope = context || this;
11
12             let now  = +new Date,
13                 args = arguments;
14             if (last && now < last + threshold) {
15                 clearTimeout(deferTimer);
16                 deferTimer = setTimeout(function () {
17                     last = now;
18                     fn.apply(scope, args);
19                 }, threshold);
20             } else {
21                 last = now;
22                 fn.apply(scope, args);
23             }
24         };
25     }
26
27 }