- return @once
- end
- end
-
- # timer handler, manage multiple Action objects, calling them when required.
- # The timer must be ticked by whatever controls it, i.e. regular calls to
- # tick() at whatever granularity suits your application's needs.
- #
- # Alternatively you can call run(), and the timer will spawn a thread and
- # tick itself, intelligently shutting down the thread if there are no
- # pending actions.
- class Timer
- def initialize(granularity = 0.1)
- @granularity = granularity
- @timers = Hash.new
- @handle = 0
- @lasttime = 0
- @should_be_running = false
- @thread = false
- @next_action_time = 0
- end
-
- # period:: how often (seconds) to run the action
- # data:: optional data to pass to the action's proc
- # func:: associate a block with add() to perform the action
- #
- # add an action to the timer
- def add(period, data=nil, &func)
- @handle += 1
- @timers[@handle] = Action.new(period, data, &func)
- start_on_add
- return @handle