Sha256: 1b6c0143f96cce6096ce538f4e021b5ba58fbd8ad5b52a1f88611dfd37ddf393

Contents?: true

Size: 1.48 KB

Versions: 6

Compression:

Stored size: 1.48 KB

Contents

module Browser

# An {EventSource} allows you to receive events from a server in real-time,
# similar to long-polling but not exactly.
#
# @see https://developer.mozilla.org/en-US/docs/Web/API/EventSource
class EventSource
  def self.supported?
    Browser.supports? :EventSource
  end

  include Native::Wrapper
  include Event::Target

  target {|value|
    EventSource.new(value) if Native.is_a?(value, `window.EventSource`)
  }

  # Create an {EventSource} on the given path.
  #
  # @param path [String] the path to use as source
  # @yield if the block has no parameters it's instance_exec'd, otherwise it's
  #        called with self
  def initialize(path, &block)
    if native?(path)
      super(path)
    else
      super(`new window.EventSource(path)`)
    end

    if block.arity == 0
      instance_exec(&block)
    else
      block.call(self)
    end if block
  end

  # @!attribute [r] url
  # @return [String] the URL of the event source
  alias_native :url

  # @!attribute [r] state
  # @return [:connecting, :open, :closed] the state of the event source
  def state
    %x{
      switch (#@native.readyState) {
        case window.EventSource.CONNECTING:
          return "connecting";

        case window.EventSource.OPEN:
          return "open";

        case window.EventSource.CLOSED:
          return "closed";
      }
    }
  end

  # Check if the event source is alive.
  def alive?
    state == :open
  end

  # Close the event source.
  def close
    `#@native.close()`
  end
end

end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
opal-browser-0.3.4 opal/browser/event_source.rb
atome-opal-browser-0.3.9.5 opal/browser/event_source.rb
opal-browser-0.3.3 opal/browser/event_source.rb
opal-browser-0.3.2 opal/browser/event_source.rb
opal-browser-0.3.1 opal/browser/event_source.rb
opal-browser-0.3.0 opal/browser/event_source.rb