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.



66
67
68
69
70
# File 'lib/mij-discord/events/basic.rb', line 66

def initialize(klass, bot)
  super(klass)

  @bot, @threads = bot, []
end

Instance Attribute Details

#threadsObject (readonly)

Returns the value of attribute threads.



64
65
66
# File 'lib/mij-discord/events/basic.rb', line 64

def threads
  @threads
end

Instance Method Details

#execute_callback(callback, event, _) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mij-discord/events/basic.rb', line 72

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