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.12.3 lib/browser/event_target.rb
isomorfeus-react-16.12.2 lib/browser/event_target.rb
isomorfeus-react-16.12.1 lib/browser/event_target.rb
isomorfeus-react-16.12.0 lib/browser/event_target.rb
isomorfeus-react-16.11.4 lib/browser/event_target.rb
isomorfeus-react-16.11.3 lib/browser/event_target.rb
isomorfeus-react-16.11.2 lib/browser/event_target.rb
isomorfeus-react-16.11.1 lib/browser/event_target.rb
isomorfeus-react-16.11.0 lib/browser/event_target.rb
isomorfeus-react-16.10.17 lib/browser/event_target.rb
isomorfeus-react-16.10.16 lib/browser/event_target.rb
isomorfeus-react-16.10.14 lib/browser/event_target.rb
isomorfeus-react-16.10.13 lib/browser/event_target.rb
isomorfeus-react-16.10.12 lib/browser/event_target.rb
isomorfeus-react-16.10.11 lib/browser/event_target.rb
isomorfeus-react-16.10.10 lib/browser/event_target.rb
isomorfeus-react-16.10.9 lib/browser/event_target.rb
isomorfeus-react-16.10.8 lib/browser/event_target.rb
isomorfeus-react-16.10.7 lib/browser/event_target.rb
isomorfeus-react-16.10.6 lib/browser/event_target.rb