Skip to content

Commit

Permalink
Merge pull request #16 from beizhedenglong/master
Browse files Browse the repository at this point in the history
Add some time based functions
  • Loading branch information
jonlaing authored Dec 7, 2018
2 parents 8db8531 + 0f2b4b1 commit ac6f2a1
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
78 changes: 78 additions & 0 deletions __tests__/Function_test.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
open Jest;
open Expect;
open Function;

describe("debounce", () => {
describe("immediate is false", () => {
let num = ref(0);
let add = debounce(~immediate=false, () => num := num^ + 1, 300);
test("1: num is 0", () => {
add();
add();
expect(num^) |> toEqual(0);
});
testAsync("2: num is 1", finish =>
delay(() => finish(expect(num^) |> toEqual(1)), 300) |> ignore
);
testAsync("3: num is 1", finish => {
add();
delay(() => finish(expect(num^) |> toEqual(1)), 100) |> ignore;
});
testAsync("4: num is 2", finish =>
delay(() => finish(expect(num^) |> toEqual(2)), 200) |> ignore
);
});
describe("immediate is true", () => {
let num = ref(0);
let add = debounce(~immediate=true, () => num := num^ + 1, 300);
test("1: num is 1", () => {
add();
add();
expect(num^) |> toEqual(1);
});
testAsync("2: num is 1", finish =>
delay(() => finish(expect(num^) |> toEqual(1)), 300) |> ignore
);
testAsync("3: num is 2", finish => {
add();
delay(() => finish(expect(num^) |> toEqual(2)), 100) |> ignore;
});
testAsync("4: num is 2", (finish) => {
delay(() => finish(expect(num^) |> toEqual(2)), 200) |> ignore ;
});
});
});

describe("throttle", () => {
let num = ref(0);
let add =
throttle(
() => {
num := num^ + 1;
num;
},
300,
);
test("1: num is 1", () => {
add();
add();
expect(num^) |> toEqual(1);
});
testAsync("2: num is 2", finish =>
delay(() => finish(expect(num^) |> toEqual(2)), 350) |> ignore
);

testAsync("3: num is 4", finish => {
delay(
() => {
add();
add();
add();
add();
},
350,
)
|> ignore;
delay(() => finish(expect(num^) |> toEqual(4)), 700) |> ignore;
});
});
59 changes: 59 additions & 0 deletions src/Function.re
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,62 @@ module Infix = {
};

include Infix;

let delay = Js.Global.setTimeout;

let debounce = (~immediate=false, f, wait) => {
open Js.Global;
let timeout = ref(None);
let afterWait = f => timeout := delay(f, wait)->Some;
() =>
switch (immediate, timeout^) {
| (true, None) =>
f();
afterWait(() => timeout := None);
| (true, Some(timerId)) =>
clearTimeout(timerId);
afterWait(() => timeout := None);
| (false, None) =>
afterWait(() => {
f();
timeout := None;
})
| (false, Some(timerId)) =>
clearTimeout(timerId);
afterWait(() => {
f();
timeout := None;
});
};
};

let throttle = (f, wait) => {
open Js.Global;
let timeout = ref(None);
let previous = ref(0.0);
() =>
switch (timeout^) {
| None =>
f();
timeout := delay(() => timeout := None, wait)->Some;
previous := Js.Date.now();
| Some(timerId) =>
clearTimeout(timerId);
let now = Js.Date.now();
let remaining = wait - int_of_float(now -. previous^);
if (remaining <= 0 || remaining > wait) {
f();
previous := Js.Date.now();
} else {
timeout :=
delay(
() => {
f();
previous := Js.Date.now();
},
remaining,
)
->Some;
};
};
};

0 comments on commit ac6f2a1

Please sign in to comment.