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

lunarmodules / copas / 3874175729

pending completion
3874175729

push

github

Thijs Schreijer
feat(cli) add a runtime script to run code in a copas environment

1284 of 1497 relevant lines covered (85.77%)

14626.21 hits per line

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

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

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

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

11

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

15

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

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

25

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

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

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

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

55
      copas.pause(self.delay)
13✔
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)
1✔
64
    assert(initial_delay == nil or initial_delay >= 0, "delay must be greater than or equal to 0")
8✔
65
    if self.co then
8✔
66
      return nil, "already armed"
1✔
67
    end
68

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

75

76

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

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

88
  self.cancelled = true
3✔
89
  copas.wakeup(self.co)       -- resume asap
3✔
90
  copas.removethread(self.co) -- will immediately drop the thread upon resuming
3✔
91
  self.co = nil
3✔
92
  return self
3✔
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)
4✔
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)
1✔
109
    assert(opts.delay or -1 >= 0, "delay must be greater than or equal to 0")
6✔
110
    assert(type(opts.callback) == "function", "expected callback to be a function")
6✔
111

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

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

128

129

130
return timer
1✔
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