Skip to content

Commit

Permalink
add new API
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaozg committed Oct 14, 2020
1 parent 7486cfa commit f047b2a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,19 @@ Get the timer repeat value.

**Returns:** `integer`

### `uv.timer_get_due_in(timer)`

> method form `timer:get_due_in()`
**Parameters:**
- `timer`: `uv_timer_t userdata`

Get the timer due value or 0 if it has expired. The time is relative to `uv.now()`.

**Returns:** `integer`

**Note**: New in libuv version 1.40.0.

## `uv_prepare_t` — Prepare handle

[`uv_prepare_t`]: #uv_prepare_t--prepare-handle
Expand Down
6 changes: 6 additions & 0 deletions src/luv.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ static const luaL_Reg luv_functions[] = {
{"timer_again", luv_timer_again},
{"timer_set_repeat", luv_timer_set_repeat},
{"timer_get_repeat", luv_timer_get_repeat},
#if LUV_UV_VERSION_GEQ(1, 40, 0)
{"timer_get_due_in", luv_timer_get_due_in},
#endif

// prepare.c
{"new_prepare", luv_new_prepare},
Expand Down Expand Up @@ -506,6 +509,9 @@ static const luaL_Reg luv_timer_methods[] = {
{"again", luv_timer_again},
{"set_repeat", luv_timer_set_repeat},
{"get_repeat", luv_timer_get_repeat},
#if LUV_UV_VERSION_GEQ(1, 40, 0)
{"get_due_in", luv_timer_get_due_in},
#endif
{NULL, NULL}
};

Expand Down
10 changes: 10 additions & 0 deletions src/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,13 @@ static int luv_timer_get_repeat(lua_State* L) {
lua_pushinteger(L, repeat);
return 1;
}

#if LUV_UV_VERSION_GEQ(1, 40, 0)
static int luv_timer_get_due_in(lua_State* L) {
uv_timer_t* handle = luv_check_timer(L, 1);
uint64_t val = uv_timer_get_due_in(handle);
lua_pushinteger(L, val);
return 1;
}
#endif

23 changes: 23 additions & 0 deletions tests/test-timer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,27 @@ return require('lib/tap')(function (test)
end, 4))
end)

test("timer init", function(print, p, expect, uv)
local timer = uv.new_timer()
assert(timer:get_repeat()==0)
assert(timer:get_due_in()>=0)
assert(timer:is_active()==false)
uv.close(timer)
end)

test("timer huge timeout", function(print, p, expect, uv)
local tiny_timer = uv.new_timer()
local huge_timer = uv.new_timer()

local function timer_cb()
uv.close(tiny_timer)
uv.close(huge_timer)
end

uv.timer_start(tiny_timer, 1, 0, expect(timer_cb))
uv.timer_start(huge_timer, 0xfffffffffff, 0, timer_cb)
assert(tiny_timer:get_due_in()==1)
assert(huge_timer:get_due_in()==0xfffffffffff)
end)

end)

0 comments on commit f047b2a

Please sign in to comment.