Class: MijDiscord::Events::EventDispatcher

Inherits:
DispatcherBase show all
Defined in:
lib/mij-discord/events/basic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DispatcherBase

#add_callback, #callbacks, #remove_callback, #trigger

Constructor Details

#initialize(klass, bot) ⇒ EventDispatcher

Returns a new instance of EventDispatcher.



52
53
54
55
56
# File 'lib/mij-discord/events/basic.rb', line 52

def initialize(klass, bot)
  super(klass)

  @bot, @threads = bot, []
end

Instance Attribute Details

#threadsObject (readonly)

Returns the value of attribute threads.



50
51
52
# File 'lib/mij-discord/events/basic.rb', line 50

def threads
  @threads
end

Instance Method Details

#execute_callback(callback, event, _) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mij-discord/events/basic.rb', line 58

def execute_callback(callback, event, _)
  Thread.new do
    thread = Thread.current

    @threads << thread
    thread[:mij_discord] = "event-#{callback.key}"

    begin
      callback.block.call(event, callback.key)
    rescue LocalJumpError
      # Allow premature return from callback block
    rescue => exc
      @bot.handle_exception(:event, exc, event)

      MijDiscord::LOGGER.error('Events') { 'An error occurred in event callback' }
      MijDiscord::LOGGER.error('Events') { exc }
    ensure
      @threads.delete(thread)
    end
  end
end