Module: EventWizard

Included in:
Nostr::Client
Defined in:
lib/event_wizard.rb

Instance Method Summary collapse

Instance Method Details

#clear(event) ⇒ Object



28
29
30
# File 'lib/event_wizard.rb', line 28

def clear(event)
  @listeners.delete(event)
end

#emit(event, *args) ⇒ Object



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

def emit(event, *args)
  @listeners[event].each { |callback| callback.call(*args) }
end

#initialize_event_emitterObject



2
3
4
# File 'lib/event_wizard.rb', line 2

def initialize_event_emitter
  @listeners = Hash.new { |hash, key| hash[key] = [] }
end

#off(event, callback) ⇒ Object



17
18
19
20
# File 'lib/event_wizard.rb', line 17

def off(event, callback)
  return unless @listeners[event]
  @listeners[event].delete(callback)
end

#on(event, &callback) ⇒ Object



6
7
8
9
10
11
# File 'lib/event_wizard.rb', line 6

def on(event, &callback)
  # Prevent adding the same callback multiple times
  unless @listeners[event].include?(callback)
    @listeners[event] << callback
  end
end

#replace(event, old_callback, new_callback) ⇒ Object



22
23
24
25
26
# File 'lib/event_wizard.rb', line 22

def replace(event, old_callback, new_callback)
  return unless @listeners[event]
  index = @listeners[event].index(old_callback)
  @listeners[event][index] = new_callback if index
end