Class: Smplkit::ContextRegistrationBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/smplkit/buffers.rb

Overview

Thread-safe batch buffer for context registration.

Instance Method Summary collapse

Constructor Details

#initializeContextRegistrationBuffer

Returns a new instance of ContextRegistrationBuffer.



30
31
32
33
34
# File 'lib/smplkit/buffers.rb', line 30

def initialize
  @seen = {}
  @pending = []
  @lock = Mutex.new
end

Instance Method Details

#drainObject

Return and clear the current pending batch.



51
52
53
54
55
56
57
# File 'lib/smplkit/buffers.rb', line 51

def drain
  @lock.synchronize do
    batch = @pending
    @pending = []
    batch
  end
end

#observe(contexts) ⇒ Object

Queue any unseen contexts.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/smplkit/buffers.rb', line 37

def observe(contexts)
  @lock.synchronize do
    contexts.each do |ctx|
      cache_key = [ctx.type, ctx.key]
      next if @seen.key?(cache_key)

      @seen.shift if @seen.size >= CONTEXT_REGISTRATION_LRU_SIZE
      @seen[cache_key] = ctx.attributes
      @pending << { "type" => ctx.type, "key" => ctx.key, "attributes" => ctx.attributes.dup }
    end
  end
end

#pending_countObject



59
60
61
# File 'lib/smplkit/buffers.rb', line 59

def pending_count
  @lock.synchronize { @pending.length }
end