• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lunarmodules / copas / 10159170494

30 Jul 2024 08:47AM UTC coverage: 84.343%. Remained the same
10159170494

push

github

web-flow
Merge c401387df into 20f1cf71d

1309 of 1552 relevant lines covered (84.34%)

51445.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.33
/src/copas/timer.lua
1
local copas = require("copas")
15✔
2

3
local xpcall = xpcall
15✔
4
local coroutine_running = coroutine.running
15✔
5

6
if _VERSION=="Lua 5.1" and not jit then     -- obsolete: only for Lua 5.1 compatibility
15✔
7
  xpcall = require("coxpcall").xpcall
3✔
8
  coroutine_running = require("coxpcall").running
3✔
9
end
10

11

12
local timer = {}
15✔
13
timer.__index = timer
15✔
14

15

16
local new_name do
15✔
17
  local count = 0
15✔
18

19
  function new_name()
12✔
20
    count = count + 1
35✔
21
    return "copas_timer_" .. count
35✔
22
  end
23
end
24

25

26
do
27
  local function expire_func(self, initial_delay)
28
    if self.errorhandler then
45✔
29
      copas.seterrorhandler(self.errorhandler)
5✔
30
    end
31
    copas.pause(initial_delay)
45✔
32
    while true do
33
      if not self.cancelled then
95✔
34
        if not self.recurring then
95✔
35
          -- non-recurring timer
36
          self.cancelled = true
20✔
37
          self.co = nil
20✔
38

39
          self:callback(self.params)
20✔
40
          return
20✔
41

42
        else
43
          -- recurring timer
44
          self:callback(self.params)
75✔
45
        end
46
      end
47

48
      if self.cancelled then
75✔
49
        -- clean up and exit the thread
50
        self.co = nil
15✔
51
        self.cancelled = true
15✔
52
        return
15✔
53
      end
54

55
      copas.pause(self.delay)
71✔
56
    end
57
  end
58

59

60
  --- Arms the timer object.
61
  -- @param initial_delay (optional) the first delay to use, if not provided uses the timer delay
62
  -- @return timer object, nil+error, or throws an error on bad input
63
  function timer:arm(initial_delay)
15✔
64
    assert(initial_delay == nil or initial_delay >= 0, "delay must be greater than or equal to 0")
50✔
65
    if self.co then
50✔
66
      return nil, "already armed"
5✔
67
    end
68

69
    self.cancelled = false
45✔
70
    self.co = copas.addnamedthread(self.name, expire_func, self, initial_delay or self.delay)
54✔
71
    return self
45✔
72
  end
73
end
74

75

76

77
--- Cancels a running timer.
78
-- @return timer object, or nil+error
79
function timer:cancel()
15✔
80
  if not self.co then
35✔
81
    return nil, "not armed"
10✔
82
  end
83

84
  if self.cancelled then
25✔
85
    return nil, "already cancelled"
×
86
  end
87

88
  self.cancelled = true
25✔
89
  copas.wakeup(self.co)       -- resume asap
25✔
90
  copas.removethread(self.co) -- will immediately drop the thread upon resuming
25✔
91
  self.co = nil
25✔
92
  return self
25✔
93
end
94

95

96
do
97
  -- xpcall error handler that forwards to the copas errorhandler
98
  local ehandler = function(err_obj)
99
    return copas.geterrorhandler()(err_obj, coroutine_running(), nil)
12✔
100
  end
101

102

103
  --- Creates a new timer object.
104
  -- Note: the callback signature is: `function(timer_obj, params)`.
105
  -- @param opts (table) `opts.delay` timer delay in seconds, `opts.callback` function to execute, `opts.recurring` boolean
106
  -- `opts.params` (optional) this value will be passed to the timer callback, `opts.initial_delay` (optional) the first delay to use, defaults to `delay`.
107
  -- @return timer object, or throws an error on bad input
108
  function timer.new(opts)
15✔
109
    assert(opts.delay or -1 >= 0, "delay must be greater than or equal to 0")
40✔
110
    assert(type(opts.callback) == "function", "expected callback to be a function")
40✔
111

112
    local callback = function(timer_obj, params)
113
      xpcall(opts.callback, ehandler, timer_obj, params)
95✔
114
    end
115

116
    return setmetatable({
104✔
117
      name = opts.name or new_name(),
47✔
118
      delay = opts.delay,
40✔
119
      callback = callback,
40✔
120
      recurring = not not opts.recurring,
40✔
121
      params = opts.params,
40✔
122
      cancelled = false,
32✔
123
      errorhandler = opts.errorhandler,
40✔
124
    }, timer):arm(opts.initial_delay)
80✔
125
  end
126
end
127

128

129

130
return timer
15✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc