-
-
Notifications
You must be signed in to change notification settings - Fork 81
Description
Problem
The current SyncResolver has several design issues that make device connections slower and the code harder to maintain:
-
Two-phase connection with event round-trip: Connecting from DISCONNECTED requires two separate event cycles (ResolveDisconnected → CONNECTING → ResolveConnecting → CONNECTED), adding unnecessary latency.
-
Implicit UNVERIFIED state: UNVERIFIED is a side-effect of tokenCache miss during resolveConnecting, not an explicit state transition.
-
Complex state transition paths: State transitions are scattered across resolveDisconnected, resolveConnecting, resolveConnection, and tryUseTokenCache with implicit dispatch logic.
-
Redundant event types: Four separate resolve events (ResolveDisconnected, ResolveConnecting, ResolveConnection, ForceResolveConnection) where two suffice.
Solution
Refactor the internal state machine while keeping the external protocol unchanged (VERSION=3, all HTTP endpoints, DB schema — fully compatible with old devices):
- Unify 4 resolve events into 2:
ResolveandForceResolve - Replace scattered resolve methods with a clear phase-based flow:
resolve → discoverAndConnect → authenticate → tryTokenCacheOrUnverified / verifyConnection - Complete the full discover → authenticate flow in a single resolve pass (no intermediate event round-trip)
- Avoid unnecessary DB writes when state doesn't change (e.g., UNVERIFIED polling)