# All files / src Relude_Debounce.bs.js

100% Statements 29/29
75% Branches 3/4
100% Functions 7/7
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50

    1x
    1x
    1x

    4x
    4x
    4x
    19x
    15x
    15x

    19x
    19x

    4x
    4x

    4x
    1x
    1x

    4x
    4x
    18x
    1x
    1x

    17x
    17x
    2x
    2x
    2x

    17x

    4x

    1x

     'use strict';

    var Curry = require("bs-platform/lib/js/curry.js");
    var Caml_option = require("bs-platform/lib/js/caml_option.js");
    var Relude_Option = require("./Relude_Option.bs.js");

    function debounce(delayMS, $staropt$star, f) {
      var leading = $staropt$star !== undefined ? $staropt$star : false;
      var timerId = /* record */[/* contents */undefined];
      var cancel = function (param) {
        Curry._2(Relude_Option.forEach, (function (timerId) {
                clearTimeout(timerId);
                return /* () */0;
              }), timerId[0]);
        timerId[0] = undefined;
        return /* () */0;
      };
      var isScheduled = function (param) {
        return Relude_Option.isSome(timerId[0]);
      };
      var flush = function (param) {
        cancel(/* () */0);
        return Curry._1(f, /* () */0);
      };
      var doLeading = /* record */[/* contents */leading];
      var debounced = function (param) {
        if (doLeading[0]) {
          doLeading[0] = false;
          return Curry._1(f, /* () */0);
        } else {
          cancel(/* () */0);
          timerId[0] = Caml_option.some(setTimeout((function (param) {
                      Curry._1(f, /* () */0);
                      timerId[0] = undefined;
                      return /* () */0;
                    }), delayMS));
          return /* () */0;
        }
      };
      return /* record */[
              /* cancel */cancel,
              /* flush */flush,
              /* isScheduled */isScheduled,
              /* f */debounced
            ];
    }

    exports.debounce = debounce;
    /* Relude_Option Not a pure module */