Add radix cwl graph visualization
[arvados-workbench2.git] / src / lib / cwl-svg / utils / perf.ts
diff --git a/src/lib/cwl-svg/utils/perf.ts b/src/lib/cwl-svg/utils/perf.ts
new file mode 100644 (file)
index 0000000..eba3599
--- /dev/null
@@ -0,0 +1,27 @@
+export class Perf {
+
+    static DEFAULT_THROTTLE = 1;
+
+    public static throttle(fn: Function, threshold = Perf.DEFAULT_THROTTLE, context?: any): Function {
+        let last: any, deferTimer: any;
+
+        return function () {
+            // @ts-ignore
+            const scope = context || this;
+
+            let now  = +new Date,
+                args = arguments;
+            if (last && now < last + threshold) {
+                clearTimeout(deferTimer);
+                deferTimer = setTimeout(function () {
+                    last = now;
+                    fn.apply(scope, args);
+                }, threshold);
+            } else {
+                last = now;
+                fn.apply(scope, args);
+            }
+        };
+    }
+
+}