Class: Async::Background::Web::EventHub

Inherits:
Object
  • Object
show all
Defined in:
lib/async/background/web/event_hub.rb

Constant Summary collapse

HEARTBEAT_FRAME =
":keepalive\n\n"
UNAVAILABLE_FRAME =
"event: unavailable\ndata: #{JSON.generate(error: 'unavailable')}\n\n".freeze

Instance Method Summary collapse

Constructor Details

#initialize(snapshot, serializer, metrics_reader: nil) ⇒ EventHub

Returns a new instance of EventHub.



12
13
14
15
16
17
18
19
20
# File 'lib/async/background/web/event_hub.rb', line 12

def initialize(snapshot, serializer, metrics_reader: nil)
  @snapshot = snapshot
  @serializer = serializer
  @metrics_reader = metrics_reader
  @mutex = Mutex.new
  @cached_version = nil
  @cached_frame = nil
  @closed = false
end

Instance Method Details

#closeObject



43
44
45
46
47
48
49
50
# File 'lib/async/background/web/event_hub.rb', line 43

def close
  @mutex.synchronize do
    @closed = true
    @cached_frame = nil
    @cached_version = nil
  end
  self
end

#closed?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/async/background/web/event_hub.rb', line 52

def closed?
  @mutex.synchronize { @closed }
end

#current_versionObject



22
23
24
25
# File 'lib/async/background/web/event_hub.rb', line 22

def current_version
  with_open {}
  @snapshot.data_version
end

#frame_for(version) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/async/background/web/event_hub.rb', line 27

def frame_for(version)
  with_open do
    next @cached_frame if @cached_version == version && @cached_frame

    refresh_frame_locked!
    @cached_frame
  end
end

#initial_frameObject



36
37
38
39
40
41
# File 'lib/async/background/web/event_hub.rb', line 36

def initial_frame
  with_open do
    refresh_frame_locked!
    [@cached_version, @cached_frame]
  end
end