Module: Playwright::EventEmitter

Included in:
Channel, ChannelOwner, EventEmitterProxy
Defined in:
lib/playwright/event_emitter.rb

Overview

A subset of Events/EventEmitter in Node.js

Instance Method Summary collapse

Instance Method Details

#emit(event, *args) ⇒ Object

Parameters:

  • event (String)


38
39
40
41
42
43
44
45
46
47
# File 'lib/playwright/event_emitter.rb', line 38

def emit(event, *args)
  handled = false
  if (callbacks = (@__event_emitter ||= {})[event.to_s])
    callbacks.dup.each do |callback|
      perform_event_emitter_callback(event, callback, args)
      handled = true
    end
  end
  handled
end

#off(event, callback) ⇒ Object

Parameters:

  • event (String)
  • callback (Proc)

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
# File 'lib/playwright/event_emitter.rb', line 79

def off(event, callback)
  raise ArgumentError.new('callback must not be nil') if callback.nil?

  cb = (@__event_emitter_callback ||= {})["#{event}/#{callback.object_id}"]
  if cb
    (@__event_emitter ||= {})[event.to_s]&.delete(cb)
  end
  self
end

#on(event, callback) ⇒ Object

Parameters:

  • event (String)
  • callback (Proc)

Raises:

  • (ArgumentError)


60
61
62
63
64
65
# File 'lib/playwright/event_emitter.rb', line 60

def on(event, callback)
  raise ArgumentError.new('callback must not be nil') if callback.nil?
  cb = (@__event_emitter_callback ||= {})["#{event}/#{callback.object_id}"] ||= EventEmitterCallback.new(callback)
  ((@__event_emitter ||= {})[event.to_s] ||= Set.new) << cb
  self
end

#once(event, callback) ⇒ Object

Parameters:

  • event (String)
  • callback (Proc)

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
# File 'lib/playwright/event_emitter.rb', line 69

def once(event, callback)
  raise ArgumentError.new('callback must not be nil') if callback.nil?

  cb = (@__event_emitter_callback ||= {})["#{event}/once/#{callback.object_id}"] ||= EventEmitterOnceCallback.new(callback)
  ((@__event_emitter ||= {})[event.to_s] ||= Set.new) << cb
  self
end