class Redis::Client

def subscribe(*classes)

def subscribe(*classes)
  # Top-level `subscribe` MUST be called with a block,	
  # nested `subscribe` MUST NOT be called with a block	
  if !@pubsub && !block_given?	
    raise "Top-level subscribe requires a block"
  elsif @pubsub == true && block_given?	
    raise "Nested subscribe does not take a block"
  elsif @pubsub	
    # If we're already pubsub'ing, just subscribe us to some more classes
    call_command [:subscribe,*classes]
    return true
  end
  @pubsub = true
  call_command [:subscribe,*classes]
  sub = Subscription.new
  yield(sub)
  begin
    while true
      type, *reply = read_reply # type, [class,data]
      case type
      when 'subscribe','unsubscribe'
        sub.send(type) && sub.send(type).call(reply[0])
      when 'message'
        sub.send(type) && sub.send(type).call(reply[0],reply[1])
      end
      break if type == 'unsubscribe' && reply[1] == 0
    end
  rescue RuntimeError
    call_command [:unsubscribe]
    raise
  ensure
    @pubsub = false
  end
end