module Cinch::Syncable

Provide blocking access to user/channel information.

Public Instance Methods

attr(attribute, data = false, unsync = false) click to toggle source

@param [Symbol] attribute @param [Boolean] data @param [Boolean] unsync @api private

# File lib/cinch/syncable.rb, line 65
def attr(attribute, data = false, unsync = false)
  unless unsync
    @when_requesting_synced_attribute&.call(attribute)
    wait_until_synced(attribute)
  end

  if data
    @data[attribute]
  else
    instance_variable_get("@#{attribute}")
  end
end
attribute_synced?(attribute) click to toggle source

@return [Boolean] @api private

# File lib/cinch/syncable.rb, line 44
def attribute_synced?(attribute)
  @synced_attributes.include?(attribute)
end
mark_as_synced(attribute) click to toggle source

@api private @return [void]

# File lib/cinch/syncable.rb, line 80
def mark_as_synced(attribute)
  @synced_attributes << attribute
end
sync(attribute, value, data = false) click to toggle source

@api private @return [void]

# File lib/cinch/syncable.rb, line 33
def sync(attribute, value, data = false)
  if data
    @data[attribute] = value
  else
    instance_variable_set("@#{attribute}", value)
  end
  @synced_attributes << attribute
end
unsync(attribute) click to toggle source

@return [void] @api private

# File lib/cinch/syncable.rb, line 50
def unsync(attribute)
  @synced_attributes.delete(attribute)
end
unsync_all() click to toggle source

@return [void] @api private @since 1.0.1

# File lib/cinch/syncable.rb, line 57
def unsync_all
  @synced_attributes.clear
end
wait_until_synced(attr) click to toggle source

Blocks until the object is synced.

@return [void] @api private

# File lib/cinch/syncable.rb, line 10
def wait_until_synced(attr)
  attr = attr.to_sym
  waited = 0
  loop do
    return if attribute_synced?(attr)

    waited += 1

    if waited % 100 == 0
      bot.loggers.warn "A synced attribute ('%s' for %s) has not been available for %d seconds, still waiting" % [attr, inspect, waited / 10]
      bot.loggers.warn caller.map { |s| "  #{s}" }

      if waited / 10 >= 30
        bot.loggers.warn "  Giving up..."
        raise Exceptions::SyncedAttributeNotAvailable, "'%s' for %s" % [attr, inspect]
      end
    end
    sleep 0.1
  end
end