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-react-16.9.9 lib/browser/event_target.rb
isomorfeus-react-16.9.8 lib/browser/event_target.rb
isomorfeus-react-16.9.7 lib/browser/event_target.rb
isomorfeus-react-16.9.6 lib/browser/event_target.rb
isomorfeus-react-16.9.5 lib/browser/event_target.rb
isomorfeus-react-16.9.4 lib/browser/event_target.rb
isomorfeus-react-16.9.3 lib/browser/event_target.rb
isomorfeus-react-16.9.2 lib/browser/event_target.rb
isomorfeus-react-16.9.1 lib/browser/event_target.rb
isomorfeus-react-16.9.0 lib/browser/event_target.rb