Class: LongPolling
- Inherits:
-
Object
- Object
- LongPolling
- Defined in:
- backend/app/lib/longpolling.rb
Instance Method Summary collapse
-
#blocking_updates_since(seq) ⇒ Object
-
#initialize(ms_to_keep) ⇒ LongPolling
constructor
A new instance of LongPolling.
-
#record_update(values) ⇒ Object
-
#reset! ⇒ Object
-
#shutdown ⇒ Object
-
#updates_since(seq) ⇒ Object
Constructor Details
#initialize(ms_to_keep) ⇒ LongPolling
Returns a new instance of LongPolling.
5 6 7 8 9 10 11 |
# File 'backend/app/lib/longpolling.rb', line 5 def initialize(ms_to_keep) @lock = Mutex.new @waiting_list = ConditionVariable.new @sequence = Time.now.to_i @updates = [] @ms_to_keep = ms_to_keep end |
Instance Method Details
#blocking_updates_since(seq) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'backend/app/lib/longpolling.rb', line 49 def blocking_updates_since(seq) @lock.synchronize do updates = updates_after(seq) if updates.empty? # Block until an update wakes us up (or until we time out) @waiting_list.wait(@lock, @ms_to_keep / 1000.0) updates_after(seq) else updates end end end |
#record_update(values) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'backend/app/lib/longpolling.rb', line 27 def record_update(values) @lock.synchronize do @sequence += 1 now = (Time.now.to_f * 1000).to_i @updates << values.merge(:sequence => @sequence, :timestamp => now) expire_older_than(now - @ms_to_keep) # Wake up any threads waiting for updates @waiting_list.broadcast end end |
#reset! ⇒ Object
20 21 22 23 24 |
# File 'backend/app/lib/longpolling.rb', line 20 def reset! @lock.synchronize do @updates = [] end end |
#shutdown ⇒ Object
14 15 16 17 18 |
# File 'backend/app/lib/longpolling.rb', line 14 def shutdown @lock.synchronize do @waiting_list.broadcast end end |
#updates_since(seq) ⇒ Object
42 43 44 45 46 |
# File 'backend/app/lib/longpolling.rb', line 42 def updates_since(seq) @lock.synchronize do updates_after(seq) end end |