-
Notifications
You must be signed in to change notification settings - Fork 0
/
repeatedly.gleam
66 lines (61 loc) · 2.25 KB
/
repeatedly.gleam
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
pub type Repeater(state)
/// Call a function every specified number of milliseconds, waiting the number
/// of milliseconds before the first call.
///
@external(erlang, "repeatedly_ffi", "call")
@external(javascript, "./repeatedly_ffi.mjs", "call")
pub fn call(
delay_ms: Int,
state: state,
function: fn(state, Int) -> a,
) -> Repeater(state)
/// Stop the repeater, preventing it from triggering again.
///
/// On Erlang if the repeater message queue is not empty then this message will
/// handled after all other messages.
///
/// On JavaScript there is no message queue so it will stop immediately, though
/// not interrupt the function callback if currently being executed.
///
@external(erlang, "repeatedly_ffi", "stop")
@external(javascript, "./repeatedly_ffi.mjs", "stop")
pub fn stop(repeater: Repeater(state)) -> Nil
/// Replace the function being called by a repeater.
///
/// On Erlang if the repeater message queue is not empty then this message will
/// handled after all other messages.
///
/// On JavaScript there is no message queue so it will stop immediately, though
/// not interrupt the function callback if currently being executed.
///
@external(erlang, "repeatedly_ffi", "replace")
@external(javascript, "./repeatedly_ffi.mjs", "replace")
pub fn set_function(
repeater: Repeater(state),
function: fn(state, Int) -> a,
) -> Nil
/// Set the repeater state.
///
/// On Erlang if the repeater message queue is not empty then this message will
/// handled after all other messages.
///
/// On JavaScript there is no message queue so it will stop immediately, though
/// not interrupt the function callback if currently being executed.
///
pub fn set_state(repeater: Repeater(state), state: state) -> Nil {
update_state(repeater, fn(_state) { state })
}
/// Update the repeater state.
///
/// On Erlang if the repeater message queue is not empty then this message will
/// handled after all other messages.
///
/// On JavaScript there is no message queue so it will stop immediately, though
/// not interrupt the function callback if currently being executed.
///
@external(erlang, "repeatedly_ffi", "update_state")
@external(javascript, "./repeatedly_ffi.mjs", "update_state")
pub fn update_state(
repeater: Repeater(state),
function: fn(state) -> state,
) -> Nil