1// Copyright 2025 International Digital Economy Academy
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15///|
16type Timer
17
18///|
19pub impl trait ToHandle {
to_handle(Self) -> Handle
of_handle(Handle) -> Self
close(Self, (Self) -> Unit) -> Unit
is_closing(Self) -> Bool
loop_(Self) -> Loop
}
ToHandle for Timer with to_handle(self : Timer) -> Handle = "%identity"
20
21///|
22pub impl trait ToHandle {
to_handle(Self) -> Handle
of_handle(Handle) -> Self
close(Self, (Self) -> Unit) -> Unit
is_closing(Self) -> Bool
loop_(Self) -> Loop
}
ToHandle for Timer with of_handle(self : Handle) -> Timer = "%identity"
23
24///|
25extern "c" fn uv_timer_make() -> Timer = "moonbit_uv_timer_make"
26
27///|
28extern "c" fn uv_timer_init(uv : Loop, timer : Timer) -> Int = "moonbit_uv_timer_init"
29
30///|
31pub fn Timer::(self : Loop) -> Timer raise Errno
new(self : Loop) -> Timer raise Errno {
32 let timer = uv_timer_make()
33 let status = (uv : Loop, timer : Timer) -> Int
uv_timer_init(self, timer)
34 if status (self_ : Int, other : Int) -> Bool
< 0 {
35 raise Errno::of_int(status)
36 }
37 return timer
38}
39
40///|
41extern "c" fn uv_timer_start(
42 timer : Timer,
43 cb : (Timer) -> Unit,
44 // We shall never use Int/UInt for time, even in MoonBit.
45 timeout : UInt64,
46 repeat : UInt64
47) -> Int = "moonbit_uv_timer_start"
48
49///|
50pub fn Timer::(self : Timer, timeout~ : UInt64, repeat~ : UInt64, cb : (Timer) -> Unit) -> Unit raise Errno
start(
51 self : Timer,
52 timeout~ : UInt64,
53 repeat~ : UInt64,
54 cb : (Timer) -> Unit
55) -> Unit raise Errno {
56 let status = (timer : Timer, cb : (Timer) -> Unit, timeout : UInt64, repeat : UInt64) -> Int
uv_timer_start(self, cb, timeout, repeat)
57 if status (self_ : Int, other : Int) -> Bool
< 0 {
58 raise Errno::of_int(status)
59 }
60}
61
62///|
63extern "c" fn uv_timer_stop(timer : Timer) -> Int = "moonbit_uv_timer_stop"
64
65///|
66pub fn Timer::(self : Timer) -> Unit raise Errno
stop(self : Timer) -> Unit raise Errno {
67 let status = uv_timer_stop(self)
68 if status (self_ : Int, other : Int) -> Bool
< 0 {
69 raise Errno::of_int(status)
70 }
71}
72
73///|
74extern "c" fn uv_timer_set_repeat(timer : Timer, repeat : UInt64) = "moonbit_uv_timer_set_repeat"
75
76///|
77pub fn Timer::(self : Timer, repeat : UInt64) -> Unit
set_repeat(self : Timer, repeat : UInt64) -> Unit {
78 (timer : Timer, repeat : UInt64) -> Unit
uv_timer_set_repeat(self, repeat)
79}
80
81///|
82extern "c" fn uv_timer_get_repeat(timer : Timer) -> UInt64 = "moonbit_uv_timer_get_repeat"
83
84///|
85pub fn Timer::get_repeat(self : Timer) -> UInt64 {
86 (timer : Timer) -> UInt64
uv_timer_get_repeat(self)
87}
88
89///|
90extern "c" fn uv_timer_get_due_in(timer : Timer) -> UInt64 = "moonbit_uv_timer_get_due_in"
91
92///|
93pub fn Timer::get_due_in(self : Timer) -> UInt64 {
94 (timer : Timer) -> UInt64
uv_timer_get_due_in(self)
95}
96