Module: Lich::Main::DetachableSessionPoller

Defined in:
documented/main/detachable_session_poller.rb

Overview

Waits for a character name to become available (e.g. XMLData.name after a bare --sal connect), so the detachable session file can be created once one is known. Pulled out of the listener thread so its two exit conditions -- a usable name, or shutdown -- are unit-testable without threads or sleeps.

Constant Summary collapse

DEFAULT_INTERVAL =
0.2

Class Method Summary collapse

Class Method Details

.wait_for_name(name_source:, shutdown_requested:, interval: DEFAULT_INTERVAL) ⇒ String?

Polls until shutdown_requested says stop or name_source returns a non-blank String. No fixed timeout: a capped wait either drops a name that arrives late (silently, with no session file for the rest of the session) or has to guess how long is long enough. Shutdown is what already has to stop this thread either way, so it's the only cap.

Parameters:

  • name_source (#call) —

    returns the current candidate name (may be nil/blank)

  • shutdown_requested (#call) —

    returns true once teardown has begun

  • interval (Numeric) (defaults to: DEFAULT_INTERVAL) —

    seconds to sleep between checks

Returns:

  • (String, nil) —

    the first non-blank, stripped name seen, or nil if shutdown was requested first



26
27
28
29
30
31
32
33
34
35
36
37
# File 'documented/main/detachable_session_poller.rb', line 26

def self.wait_for_name(name_source:, shutdown_requested:, interval: DEFAULT_INTERVAL)
  # `until`, not Kernel#loop: loop silently rescues StopIteration and
  # returns whatever it was carrying, which would violate the
  # [String, nil] contract here if name_source ever raised one.
  until shutdown_requested.call
    candidate = name_source.call
    return candidate.strip if candidate.is_a?(String) && !candidate.strip.empty?

    sleep(interval)
  end
  nil
end