Class: Cinch::Callback Private

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/cinch/callback.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#Channel, #User

Constructor Details

#initialize(bot) ⇒ Callback

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Callback.



8
9
10
# File 'lib/cinch/callback.rb', line 8

def initialize(bot)
  @bot = bot
end

Instance Attribute Details

#botBot (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



7
8
9
# File 'lib/cinch/callback.rb', line 7

def bot
  @bot
end

Instance Method Details

#synchronize(*args) { ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Since Cinch uses threads, all handlers can be run simultaneously, even the same handler multiple times. This also means, that your code has to be thread-safe. Most of the time, this is not a problem, but if you are accessing stored data, you will most likely have to synchronize access to it. Instead of managing all mutexes yourself, Cinch provides a synchronize method, which takes a name and block.

Synchronize blocks with the same name share the same mutex, which means that only one of them will be executed at a time.

Examples:

configure do |c|
  
  @i = 0
end

on :channel, /^start counting!/ do
  synchronize(:my_counter) do
    10.times do
      val = @i
      # at this point, another thread might've incremented :i already.
      # this thread wouldn't know about it, though.
      @i = val + 1
    end
  end
end

Parameters:

  • name (String, Symbol)

    a name for the synchronize block.

Yields:



13
14
15
# File 'lib/cinch/callback.rb', line 13

def synchronize(*args, &block)
  @bot.synchronize(*args, &block)
end