Class: LongPolling
- Inherits:
-
Object
- Object
- LongPolling
- Defined in:
- backend/app/lib/longpolling.rb
Instance Method Summary (collapse)
-
- (Object) blocking_updates_since(seq)
-
- (LongPolling) initialize(ms_to_keep)
constructor
A new instance of LongPolling.
-
- (Object) record_update(values)
-
- (Object) reset!
-
- (Object) shutdown
-
- (Object) updates_since(seq)
Constructor Details
- (LongPolling) initialize(ms_to_keep)
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
- (Object) blocking_updates_since(seq)
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 |
- (Object) record_update(values)
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 |
- (Object) reset!
20 21 22 23 24 |
# File 'backend/app/lib/longpolling.rb', line 20 def reset! @lock.synchronize do @updates = [] end end |
- (Object) shutdown
14 15 16 17 18 |
# File 'backend/app/lib/longpolling.rb', line 14 def shutdown @lock.synchronize do @waiting_list.broadcast end end |
- (Object) updates_since(seq)
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 |