• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SAP / ui5-webcomponents-react / 13287547337

12 Feb 2025 02:23PM CUT coverage: 86.925%. First build
13287547337

Pull #6929

github

web-flow
Merge 67c43dea0 into 65b203ac2
Pull Request #6929: chore(coverage): ignore `dist` folders

2254 of 2778 branches covered (81.14%)

3836 of 4413 relevant lines covered (86.92%)

57673.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

6.67
/packages/base/src/utils/throttle.ts
1
// Copied from https://github.com/jashkenas/underscore/blob/master/modules/throttle.js (24th January 2023)
2

3
const now = Date.now || (() => new Date().getTime());
245!
4

5
interface Cancelable {
6
  cancel(): void;
7
}
8

9
interface Options {
10
  leading?: boolean;
11
  trailing?: boolean;
12
}
13

14
/**
15
 * Returns a function, that, when invoked, will only be triggered at most once
16
 * during a given window of time. Normally, the throttled function will run
17
 * as much as it can, without ever going more than once per `wait` duration;
18
 * but if you'd like to disable the execution on the leading edge, pass
19
 * `{leading: false}`. To disable execution on the trailing edge, ditto.
20
 */
21
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
22
const throttle = <T extends Function>(
245✔
23
  func: T,
24
  wait: number,
25
  options: Options = { leading: true, trailing: true }
×
26
): T & Cancelable => {
27
  let timeout;
28
  let context;
29
  let args;
30
  let result;
31
  let previous = 0;
×
32
  if (!options) options = {};
×
33

34
  const later = () => {
×
35
    previous = options.leading === false ? 0 : now();
×
36
    timeout = null;
×
37
    result = func.apply(context, args);
×
38
    if (!timeout) context = args = null;
×
39
  };
40

41
  const throttled = function (...params: unknown[]) {
×
42
    const _now = now();
×
43
    if (!previous && options.leading === false) previous = _now;
×
44
    const remaining = wait - (_now - previous);
×
45
    // @ts-expect-error: copied - no need to infer types here
46
    // eslint-disable-next-line @typescript-eslint/no-this-alias
47
    context = this;
×
48
    args = params;
×
49
    if (remaining <= 0 || remaining > wait) {
×
50
      if (timeout) {
×
51
        clearTimeout(timeout);
×
52
        timeout = null;
×
53
      }
54
      previous = _now;
×
55
      result = func.apply(context, args);
×
56
      if (!timeout) context = args = null;
×
57
    } else if (!timeout && options.trailing !== false) {
×
58
      timeout = setTimeout(later, remaining);
×
59
    }
60
    return result;
×
61
  };
62

63
  throttled.cancel = () => {
×
64
    clearTimeout(timeout);
×
65
    previous = 0;
×
66
    timeout = context = args = null;
×
67
  };
68

69
  // @ts-expect-error: copied - no need to infer types here
70
  return throttled;
×
71
};
72

73
export { throttle };
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc