wonka
Version:
A fast push & pull stream library for Reason, loosely following the [callbag spec](https://github.com/callbag/callbag)
61 lines (57 loc) • 2.51 kB
JavaScript
;
var Block = require("bs-platform/lib/js/block.js");
var Curry = require("bs-platform/lib/js/curry.js");
var Caml_option = require("bs-platform/lib/js/caml_option.js");
function debounce(f) {
return (function (source) {
return (function (sink) {
var gotEndSignal = /* record */[/* contents */false];
var id = /* record */[/* contents */undefined];
var $$clearTimeout$1 = function (param) {
var match = id[0];
if (match !== undefined) {
id[0] = undefined;
clearTimeout(Caml_option.valFromOption(match));
return /* () */0;
} else {
return /* () */0;
}
};
return Curry._1(source, (function (signal) {
if (typeof signal === "number") {
gotEndSignal[0] = true;
var match = id[0];
if (match !== undefined) {
return /* () */0;
} else {
return sink(/* End */0);
}
} else if (signal.tag) {
$$clearTimeout$1(/* () */0);
id[0] = Caml_option.some(setTimeout((function (param) {
id[0] = undefined;
sink(signal);
if (gotEndSignal[0]) {
return sink(/* End */0);
} else {
return 0;
}
}), f(signal[0])));
return /* () */0;
} else {
var tb = signal[0];
return sink(/* Start */Block.__(0, [(function (signal) {
if (signal) {
$$clearTimeout$1(/* () */0);
return tb(/* Close */1);
} else {
return tb(signal);
}
})]));
}
}));
});
});
}
exports.debounce = debounce;
/* No side effect */