Class: Cinch::Callback Private

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

Overview

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.

Class used for encapsulating handlers to prevent them from overwriting instance variables in Bot

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#Channel, #Format, #Sanitize, #Target, #Timer, #Unformat, #User, #debug, #error, #exception, #fatal, #incoming, #info, #log, #outgoing, #rescue_exception, sanitize, #warn

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.



11
12
13
# File 'lib/cinch/callback.rb', line 11

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:



10
11
12
# File 'lib/cinch/callback.rb', line 10

def bot
  @bot
end

Instance Method Details

#synchronize(name) { ... }

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:



16
17
18
# File 'lib/cinch/callback.rb', line 16

def synchronize(name, &block)
  @bot.synchronize(name, &block)
end