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.23 lib/browser/event_target.rb
isomorfeus-react-16.12.22 lib/browser/event_target.rb
isomorfeus-react-16.12.21 lib/browser/event_target.rb
isomorfeus-react-16.12.20 lib/browser/event_target.rb
isomorfeus-react-16.12.19 lib/browser/event_target.rb
isomorfeus-react-16.12.18 lib/browser/event_target.rb
isomorfeus-react-16.12.17 lib/browser/event_target.rb
isomorfeus-react-16.12.16 lib/browser/event_target.rb
isomorfeus-react-16.12.15 lib/browser/event_target.rb
isomorfeus-react-16.12.14 lib/browser/event_target.rb
isomorfeus-react-16.12.13 lib/browser/event_target.rb
isomorfeus-react-16.12.12 lib/browser/event_target.rb
isomorfeus-react-16.12.11 lib/browser/event_target.rb
isomorfeus-react-16.12.10 lib/browser/event_target.rb
isomorfeus-react-16.12.9 lib/browser/event_target.rb
isomorfeus-react-16.12.8 lib/browser/event_target.rb
isomorfeus-react-16.12.7 lib/browser/event_target.rb
isomorfeus-react-16.12.6 lib/browser/event_target.rb
isomorfeus-react-16.12.5 lib/browser/event_target.rb
isomorfeus-react-16.12.4 lib/browser/event_target.rb