Class: Smplkit::ContextRegistrationBuffer Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Thread-safe batch buffer for context registration.

Instance Method Summary collapse

Constructor Details

#initializeContextRegistrationBuffer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ContextRegistrationBuffer.



39
40
41
42
43
# File 'lib/smplkit/buffers.rb', line 39

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

Instance Method Details

#drainObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return and clear the current pending batch.



60
61
62
63
64
65
66
# File 'lib/smplkit/buffers.rb', line 60

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

#observe(contexts) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Queue any unseen contexts.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/smplkit/buffers.rb', line 46

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



68
69
70
# File 'lib/smplkit/buffers.rb', line 68

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