Module: Ibex::Runtime::Observation

Included in:
Parser
Defined in:
lib/ibex/runtime/observation.rb

Overview

Registration and ordered synchronous dispatch for parser observers.

Defined Under Namespace

Classes: Subscription

Instance Method Summary collapse

Instance Method Details

#observe(&observer) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ibex/runtime/observation.rb', line 30

def observe(&observer)
  __send__(:ensure_runtime_initialized!)
  raise ArgumentError, "observe requires a block" unless observer

  @runtime_observation_mutex.synchronize do
    ensure_observation_thread!
    @runtime_fast_path = false
    subscription = Subscription.__send__(:new)
    @runtime_observers ||= {} #: Hash[Subscription, Proc]
    @runtime_observers[subscription] = observer
    subscription
  end
end

#unobserve(subscription) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ibex/runtime/observation.rb', line 45

def unobserve(subscription)
  __send__(:ensure_runtime_initialized!)
  @runtime_observation_mutex.synchronize do
    ensure_observation_thread!
    observers = @runtime_observers
    next false unless observers

    removed = !observers.delete(subscription).nil?
    @runtime_observers = nil if observers.empty?
    removed
  end
end