Class: Browser::EventSource
- Includes:
- Browser::Event::Target, Native::Wrapper
- Defined in:
- opal/browser/event_source.rb
Overview
An EventSource allows you to receive events from a server in real-time, similar to long-polling but not exactly.
Instance Attribute Summary collapse
-
#state ⇒ :connecting, ...
readonly
The state of the event source.
-
#url ⇒ String
readonly
The URL of the event source.
Class Method Summary collapse
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Check if the event source is alive.
-
#close ⇒ Object
Close the event source.
-
#initialize(path) { ... } ⇒ EventSource
constructor
Create an EventSource on the given path.
Methods included from Browser::Event::Target
#off, #on, #on!, #one, #trigger, #trigger!
Constructor Details
permalink #initialize(path) { ... } ⇒ EventSource
Create an Browser::EventSource on the given path.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'opal/browser/event_source.rb', line 25 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 |
Instance Attribute Details
permalink #state ⇒ :connecting, ... (readonly)
Returns the state of the event source.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'opal/browser/event_source.rb', line 45 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 |
Class Method Details
permalink .supported? ⇒ Boolean
9 10 11 |
# File 'opal/browser/event_source.rb', line 9 def self.supported? Browser.supports? :EventSource end |
Instance Method Details
permalink #alive? ⇒ Boolean
Check if the event source is alive.
61 62 63 |
# File 'opal/browser/event_source.rb', line 61 def alive? state == :open end |
permalink #close ⇒ Object
Close the event source.
66 67 68 |
# File 'opal/browser/event_source.rb', line 66 def close `#@native.close()` end |