Sha256: 94618e84e1e568d15ade4539427fbf4ce3f1668ce1dd66c2f8cc730f2dc8b3f1

Contents?: true

Size: 1.36 KB

Versions: 15

Compression:

Stored size: 1.36 KB

Contents

# backtick_javascript: true

module Browser
  module EventTarget
    # Add the block as a handler for the specified event name. Will use either
    # `addEventListener` or `addListener` if they exist.
    #
    # @param event_name [String] the name of the event
    # @return [Proc] the block to pass to `off` to remove this handler
    # @yieldparam event [Browser::Event] the event object
    def on event_name, &block
      wrapper = proc { |event| block.call Event.new(event) }

      if `#@native.addEventListener !== undefined`
        `#@native.addEventListener(event_name, wrapper)`
      elsif `#@native.addListener !== undefined`
        `#@native.addListener(event_name, wrapper)`
      else
        warn "[Browser] Not entirely sure how to add an event listener to #{self}"
      end

      wrapper
    end

    # Remove an event handler
    #
    # @param event_name [String] the name of the event
    # @block the handler to remove, as returned from `on`
    def off event_name, &block
      if `#@native.removeEventListener !== undefined`
        `#@native.removeEventListener(event_name, block)`
      elsif `#@native.removeListener !== undefined`
        `#@native.removeListener(event_name, block)`
      else
        warn "[Browser] Not entirely sure how to remove an event listener from #{self}"
      end

      nil
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
isomorfeus-preact-23.9.0.rc12 lib/browser_auto/browser/event_target.rb
isomorfeus-preact-23.9.0.rc11 lib/browser_auto/browser/event_target.rb
isomorfeus-preact-23.9.0.rc10 lib/browser_auto/browser/event_target.rb
isomorfeus-preact-23.9.0.rc9 lib/browser_auto/browser/event_target.rb
isomorfeus-preact-23.9.0.rc8 lib/browser_auto/browser/event_target.rb
isomorfeus-preact-23.9.0.rc7 lib/browser_auto/browser/event_target.rb
isomorfeus-preact-23.9.0.rc6 lib/browser_auto/browser/event_target.rb
isomorfeus-preact-23.9.0.rc5 lib/browser_auto/browser/event_target.rb
isomorfeus-preact-23.9.0.rc4 lib/browser_auto/browser/event_target.rb
isomorfeus-preact-23.9.0.rc3 lib/browser_auto/browser/event_target.rb
isomorfeus-preact-23.9.0.rc2 lib/browser_auto/browser/event_target.rb
isomorfeus-preact-23.9.0.rc1 lib/browser_auto/browser/event_target.rb
isomorfeus-preact-23.8.0.rc3 lib/browser/event_target.rb
isomorfeus-preact-23.8.0.rc2 lib/browser/event_target.rb
isomorfeus-preact-23.8.0.rc1 lib/browser/event_target.rb