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
type Timer
Timer
with to_handle(self :
type Timer
Timer
) ->
type Handle
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
type Timer
Timer
with of_handle(self :
type Handle
Handle
) ->
type Timer
Timer
= "%identity"
23 24///| 25extern "c" fn uv_timer_make() ->
type Timer
Timer
= "moonbit_uv_timer_make"
26 27///| 28extern "c" fn uv_timer_init(uv :
type Loop Bytes
Loop
, timer :
type Timer
Timer
) ->
Int
Int
= "moonbit_uv_timer_init"
29 30///| 31pub fn
type Timer
Timer
::
(self : Loop) -> Timer raise Errno
new
(
Loop
self
:
type Loop Bytes
Loop
) ->
type Timer
Timer
raise
type! Errno Status
Errno
{
32 let
Timer
timer
=
() -> Timer
uv_timer_make
()
33 let
Int
status
=
(uv : Loop, timer : Timer) -> Int
uv_timer_init
(
Loop
self
,
Timer
timer
)
34 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
35 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
36 } 37 return
Timer
timer
38} 39 40///| 41extern "c" fn uv_timer_start( 42 timer :
type Timer
Timer
,
43 cb : (
type Timer
Timer
) ->
Unit
Unit
,
44 // We shall never use Int/UInt for time, even in MoonBit. 45 timeout :
UInt64
UInt64
,
46 repeat :
UInt64
UInt64
47) ->
Int
Int
= "moonbit_uv_timer_start"
48 49///| 50pub fn
type Timer
Timer
::
(self : Timer, timeout~ : UInt64, repeat~ : UInt64, cb : (Timer) -> Unit) -> Unit raise Errno
start
(
51
Timer
self
:
type Timer
Timer
,
52
UInt64
timeout
~ :
UInt64
UInt64
,
53
UInt64
repeat
~ :
UInt64
UInt64
,
54
(Timer) -> Unit
cb
: (
type Timer
Timer
) ->
Unit
Unit
55) ->
Unit
Unit
raise
type! Errno Status
Errno
{
56 let
Int
status
=
(timer : Timer, cb : (Timer) -> Unit, timeout : UInt64, repeat : UInt64) -> Int
uv_timer_start
(
Timer
self
,
(Timer) -> Unit
cb
,
UInt64
timeout
,
UInt64
repeat
)
57 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
58 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
59 } 60} 61 62///| 63extern "c" fn uv_timer_stop(timer :
type Timer
Timer
) ->
Int
Int
= "moonbit_uv_timer_stop"
64 65///| 66pub fn
type Timer
Timer
::
(self : Timer) -> Unit raise Errno
stop
(
Timer
self
:
type Timer
Timer
) ->
Unit
Unit
raise
type! Errno Status
Errno
{
67 let
Int
status
=
(timer : Timer) -> Int
uv_timer_stop
(
Timer
self
)
68 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
69 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
70 } 71} 72 73///| 74extern "c" fn uv_timer_set_repeat(timer :
type Timer
Timer
, repeat :
UInt64
UInt64
) = "moonbit_uv_timer_set_repeat"
75 76///| 77pub fn
type Timer
Timer
::
(self : Timer, repeat : UInt64) -> Unit
set_repeat
(
Timer
self
:
type Timer
Timer
,
UInt64
repeat
:
UInt64
UInt64
) ->
Unit
Unit
{
78
(timer : Timer, repeat : UInt64) -> Unit
uv_timer_set_repeat
(
Timer
self
,
UInt64
repeat
)
79} 80 81///| 82extern "c" fn uv_timer_get_repeat(timer :
type Timer
Timer
) ->
UInt64
UInt64
= "moonbit_uv_timer_get_repeat"
83 84///| 85pub fn
type Timer
Timer
::
(self : Timer) -> UInt64
get_repeat
(
Timer
self
:
type Timer
Timer
) ->
UInt64
UInt64
{
86
(timer : Timer) -> UInt64
uv_timer_get_repeat
(
Timer
self
)
87} 88 89///| 90extern "c" fn uv_timer_get_due_in(timer :
type Timer
Timer
) ->
UInt64
UInt64
= "moonbit_uv_timer_get_due_in"
91 92///| 93pub fn
type Timer
Timer
::
(self : Timer) -> UInt64
get_due_in
(
Timer
self
:
type Timer
Timer
) ->
UInt64
UInt64
{
94
(timer : Timer) -> UInt64
uv_timer_get_due_in
(
Timer
self
)
95} 96