Class: WifiWand::Platforms::Mac::SystemProfilerWifiDataProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/wifi_wand/platforms/mac/system_profiler_wifi_data_provider.rb

Constant Summary collapse

CACHE_CONTEXTS_KEY =
:wifi_wand_system_profiler_wifi_data_cache_contexts
SYSTEM_PROFILER_WIFI_ARGS =
%w[system_profiler -json SPAirPortDataType].freeze
SYSTEM_PROFILER_TIMEOUT_SECONDS =
15

Instance Method Summary collapse

Constructor Details

#initialize(owner:, command_runner:) ⇒ SystemProfilerWifiDataProvider

Returns a new instance of SystemProfilerWifiDataProvider.



15
16
17
18
19
20
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_provider.rb', line 15

def initialize(owner:, command_runner:)
  @owner = owner
  @command_runner = command_runner
  @cache_mutex = Mutex.new
  @cache_generation = 0
end

Instance Method Details

#active_cache_contextObject



52
53
54
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_provider.rb', line 52

def active_cache_context
  current_cache_contexts&.fetch(@owner, nil)
end

#data(timeout_in_secs: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_provider.rb', line 29

def data(timeout_in_secs: nil)
  context = active_cache_context
  generation = cache_generation
  return context[:data] if cached_data_current?(context, generation)

  parsed_data = parse_system_profiler_wifi_data(timeout_in_secs: timeout_in_secs)

  cache_data(context, generation, parsed_data)
  parsed_data
end

#invalidate_cacheObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_provider.rb', line 40

def invalidate_cache
  @cache_mutex.synchronize do
    @cache_generation += 1
  end

  context = active_cache_context
  return unless context

  context.delete(:data)
  context.delete(:generation)
end

#with_cache_scopeObject



22
23
24
25
26
27
# File 'lib/wifi_wand/platforms/mac/system_profiler_wifi_data_provider.rb', line 22

def with_cache_scope
  context = enter_cache_scope
  yield
ensure
  exit_cache_scope(context) if context
end