Sha256: 6c20a58a9118fdb275142e7392ea61fd2565ed8922d4fe25b385dc813ec7cf9e

Contents?: true

Size: 1.29 KB

Versions: 170

Compression:

Stored size: 1.29 KB

Contents

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

170 entries across 170 versions & 2 rubygems

Version Path
isomorfeus-preact-22.9.0.rc3 lib/browser/event_target.rb
isomorfeus-preact-22.9.0.rc2 lib/browser/event_target.rb
isomorfeus-preact-22.9.0.rc1 lib/browser/event_target.rb
isomorfeus-preact-10.9.0 lib/browser/event_target.rb
isomorfeus-preact-10.8.2 lib/browser/event_target.rb
isomorfeus-preact-10.8.1 lib/browser/event_target.rb
isomorfeus-preact-10.8.0 lib/browser/event_target.rb
isomorfeus-preact-10.7.3 lib/browser/event_target.rb
isomorfeus-preact-10.7.2 lib/browser/event_target.rb
isomorfeus-preact-10.7.1 lib/browser/event_target.rb
isomorfeus-react-16.13.12 lib/browser/event_target.rb
isomorfeus-preact-10.7.0 lib/browser/event_target.rb
isomorfeus-preact-10.6.62 lib/browser/event_target.rb
isomorfeus-preact-10.6.61 lib/browser/event_target.rb
isomorfeus-preact-10.6.60 lib/browser/event_target.rb
isomorfeus-preact-10.6.59 lib/browser/event_target.rb
isomorfeus-preact-10.6.58 lib/browser/event_target.rb
isomorfeus-preact-10.6.57 lib/browser/event_target.rb
isomorfeus-preact-10.6.56 lib/browser/event_target.rb
isomorfeus-preact-10.6.55 lib/browser/event_target.rb