- # period:: how often (seconds) to run the action
- # data:: optional data to pass to the proc
- # once:: optional, if true, this action will be run once then removed
- # func:: associate a block to be called to perform the action
- #
- # create a new action
- def initialize(period, data=nil, once=false, &func)
- @blocked = false
- @period = period
- @in = period
- @func = func
- @data = data
- @once = once
- @last_tick = Time.new
+ def initialize(options = {}, &block)
+ opts = {
+ :period => 1,
+ :blocked => false,
+ :args => [],
+ :repeat => false
+ }.merge(options)
+
+ @block = nil
+ debug("adding timer #{self} :period => #{opts[:period]}, :repeat => #{opts[:repeat].inspect}")
+ self.configure(opts, &block)
+ debug("added #{self}")
+ end
+
+ # Provides for on-the-fly reconfiguration of the Actions
+ # Accept the same arguments as the constructor
+ def configure(opts = {}, &block)
+ @period = opts[:period] if opts.include? :period
+ @blocked = opts[:blocked] if opts.include? :blocked
+ @repeat = opts[:repeat] if opts.include? :repeat
+
+ if block_given?
+ @block = block
+ elsif opts[:code]
+ @block = opts[:code]
+ end
+
+ raise 'huh?? blockless action?' unless @block
+ if opts.include? :args
+ @args = Array === opts[:args] ? opts[:args] : [opts[:args]]
+ end
+
+ if opts[:start] and (Time === opts[:start])
+ self.next = opts[:start]
+ else
+ self.next = Time.now + (opts[:start] || @period)
+ end