Class: Everywhere::Dock::State

Inherits:
Object
  • Object
show all
Defined in:
lib/everywhere/dock/state.rb

Overview

What the dock knows about each thing every dev is running. Pure data with an injected clock, so elapsed times are deterministic under test.

Defined Under Namespace

Classes: Target

Constant Summary collapse

ORDER =

Ordered so the dock reads left-to-right the way the session starts up.

%i[web desktop ios android].freeze
LABELS =
{
  web: "web",
  desktop: "desktop",
  ios: "iOS",
  android: "Android"
}.freeze
BUSY =

States that are "in flight" — they get a spinner and a running clock.

%i[booting building installing].freeze

Instance Method Summary collapse

Constructor Details

#initialize(keys, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }) ⇒ State

Returns a new instance of State.



25
26
27
28
29
30
31
32
# File 'lib/everywhere/dock/state.rb', line 25

def initialize(keys, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
  @clock = clock
  @lock = Mutex.new
  ordered = ORDER & keys
  @targets = ordered.to_h do |key|
    [key, Target.new(key: key, label: LABELS.fetch(key, key.to_s), state: :idle, since: clock.call)]
  end
end

Instance Method Details

#[](key) ⇒ Object



50
# File 'lib/everywhere/dock/state.rb', line 50

def [](key) = @lock.synchronize { @targets[key] }

#busy?Boolean

Returns:

  • (Boolean)


54
# File 'lib/everywhere/dock/state.rb', line 54

def busy? = @lock.synchronize { @targets.each_value.any?(&:busy?) }

#detail(key, text) ⇒ Object



46
47
48
# File 'lib/everywhere/dock/state.rb', line 46

def detail(key, text)
  @lock.synchronize { @targets[key]&.detail = text }
end

#elapsed(target) ⇒ Object



56
# File 'lib/everywhere/dock/state.rb', line 56

def elapsed(target) = @clock.call - target.since

#set(key, state, detail: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/everywhere/dock/state.rb', line 34

def set(key, state, detail: nil)
  @lock.synchronize do
    target = @targets[key] or return nil
    # Keep `since` when nothing actually changed, so a repeated
    # set(:ios, :building) from a retry doesn't reset the elapsed clock.
    target.since = @clock.call unless target.state == state
    target.state = state
    target.detail = detail
    target
  end
end

#targetsObject



52
# File 'lib/everywhere/dock/state.rb', line 52

def targets = @lock.synchronize { @targets.values.map(&:dup) }