Module: Cabin::Mixins::Timer

Included in:
Channel
Defined in:
lib/cabin/mixins/timer.rb

Instance Method Summary collapse

Instance Method Details

#time(data, &block) ⇒ Object

Start timing something. Returns an instance of Cabin::Timer bound to this Cabin::Channel. To stop the timer and immediately emit the result to this channel, invoke the Cabin::Timer#stop method.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cabin/mixins/timer.rb', line 9

def time(data, &block)
  # TODO(sissel): need to refactor string->hash shoving.
  data = dataify(data)

  timer = Cabin::Timer.new do |duration|
    data[:duration] = duration
    publish(data)
  end

  if block_given?
    block.call
    return timer.stop
  else
    return timer
  end
end