Skip to content

Refactor check_session_values to fully async callback-based pattern #16

@coderabbitai

Description

@coderabbitai

Context

Currently, check_session_values in Scripts/s4ap/persistance/ap_session_data_store.py uses a hybrid approach that supports both synchronous (legacy) and asynchronous (callback-based) calling patterns. This was intentionally kept as a transitional state while the testing environment is essentially production.

Related PR: #12
Discussion: #12 (comment)


Current State (Hybrid Approach)

The method signature currently is:

def check_session_values(
    self, 
    host_name: str, 
    port: int, 
    seed_name: str, 
    player: str, 
    slot: int,
    on_complete: Optional[Callable[[bool], None]] = None
) -> bool:

Behavior:

  • When on_complete=None: Returns a boolean synchronously (legacy behavior, but unsafe with dialogs)
  • When on_complete is provided: Invokes the callback with the result (correct async pattern)

Why this exists:

  • Testing environment is essentially production
  • Need stable behavior for current beta release
  • Full refactoring deferred until after beta stabilizes

Desired End State (Fully Async)

1. Update check_session_values signature

Make on_complete required and remove the boolean return:

def check_session_values(
    self, 
    host_name: str, 
    port: int, 
    seed_name: str, 
    player: str, 
    slot: int,
    on_complete: Callable[[bool], None]  # No longer optional
) -> None:  # No longer returns bool

2. Ensure all dialog paths invoke the callback

All branches must call on_complete(True) or on_complete(False):

  • _ok_chosen: Call on_complete(True)
  • _cancel_chosen: Call on_complete(False)
  • No dialog (None guard): Call on_complete(False) immediately
  • Settings match: Call on_complete(True) immediately

3. Refactor caller in auto_parse_json.py

The caller currently uses:

if data_store.check_session_values(host_name, port, seed_name, player, slot):
    # continue processing

This needs to be refactored to:

def _on_session_check_complete(success: bool):
    if not success:
        cancel = True
        return
    # Continue with subsequent processing...

data_store.check_session_values(
    host_name, port, seed_name, player, slot,
    on_complete=_on_session_check_complete
)

Migration Checklist

  • Remove Optional from on_complete parameter type
  • Change return type from bool to None
  • Remove all return True/return False statements in the function body
  • Ensure all code paths invoke on_complete(bool)
  • Refactor caller in auto_parse_json.py to use callback pattern
  • Remove legacy synchronous code paths and deprecation warnings
  • Test thoroughly in non-production environment

Why This Matters

The Sims 4 runtime does not support safe blocking on UI dialog interactions. Using blocking synchronization primitives (like threading.Event, Future.result(), etc.) will freeze the game loop. The callback-based pattern is the only safe way to handle async dialog results.


Timeline

To be addressed after the current beta release is stable and a proper testing environment is available.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions