@gitorial/sync
Version:
Universal sync library for real-time tutorial state synchronization between websites and VS Code extensions with built-in relay server orchestration
149 lines • 4.79 kB
TypeScript
import { TutorialSyncState, ConnectionStatus, SyncClientError } from './types';
import { SyncPhase, SyncPhaseChangeEvent } from './types/sync-phases';
import { SessionData } from '../server/types/session';
import { ControlRequestEvent, ControlOfferEvent } from './messaging/MessageDispatcher';
export interface RelayClientConfig {
sessionEndpoint: string;
baseUrl: string;
wsUrl: string;
connectionTimeout?: number;
autoReconnect?: boolean;
maxReconnectAttempts?: number;
reconnectDelay?: number;
}
export interface RelayClientEvents {
connected: () => void;
disconnected: () => void;
connectionStatusChanged: (status: ConnectionStatus) => void;
syncPhaseChanged: (event: SyncPhaseChangeEvent) => void;
controlRequested: (event: ControlRequestEvent) => void;
controlOffered: (event: ControlOfferEvent) => void;
tutorialStateUpdated: (state: TutorialSyncState) => void;
clientConnected: (clientId: string) => void;
clientDisconnected: (clientId: string) => void;
error: (error: SyncClientError) => void;
}
/**
* Refactored RelayClient with explicit sync lifecycle state machine
*
* Sync phases:
* - DISCONNECTED → CONNECTING → CONNECTED_IDLE
* - CONNECTED_IDLE → INITIALIZING_PULL → ACTIVE
* - CONNECTED_IDLE → INITIALIZING_PUSH → PASSIVE
* - ACTIVE ↔ PASSIVE (via control transfer)
* - Any phase → DISCONNECTED
*/
export declare class RelayClient {
private readonly clientId;
private readonly config;
private readonly eventEmitter;
private readonly connectionManager;
private readonly sessionManager;
private readonly messageDispatcher;
private readonly syncPhaseStateMachine;
constructor(config: RelayClientConfig);
on(event: string, listener: (...args: any[]) => void): this;
off(event: string, listener: (...args: any[]) => void): this;
emit(event: string, ...args: any[]): boolean;
once(event: string, listener: (...args: any[]) => void): this;
removeAllListeners(event?: string): this;
/**
* Create a new session and connect to it
*/
createSessionAndConnect(metadata?: any): Promise<SessionData>;
/**
* Connect to an existing session by ID
*/
connectToSession(sessionId: string): Promise<void>;
/**
* Get current session information
*/
getSessionInfo(): Promise<SessionData | null>;
/**
* Disconnect from current session
*/
disconnect(): void;
/**
* Get current sync phase
*/
getCurrentSyncPhase(): SyncPhase;
/**
* Check if client is in idle state (connected but no sync direction chosen)
*/
isConnectedIdle(): boolean;
/**
* Check if client is currently active (has control)
*/
isActive(): boolean;
/**
* Check if client is currently passive (listening to updates)
*/
isPassive(): boolean;
/**
* Check if client is in any connecting phase
*/
isConnecting(): boolean;
/**
* Choose to pull state from peer (become ACTIVE)
* Only available when CONNECTED_IDLE
*/
pullStateFromPeer(): Promise<void>;
/**
* Choose to push current state to peer (become PASSIVE)
* Only available when CONNECTED_IDLE
*/
pushStateToPeer(initialState?: TutorialSyncState): Promise<void>;
/**
* Send tutorial state update (only ACTIVE clients)
*/
sendTutorialState(state: TutorialSyncState): void;
/**
* Request current tutorial state from peer (ACTIVE clients)
*/
requestTutorialState(): void;
/**
* Get the last synchronized tutorial state
*/
getLastTutorialState(): TutorialSyncState | null;
/**
* Offer control transfer to peer (only ACTIVE clients)
*/
offerControlToPeer(): void;
/**
* Accept control transfer from peer
*/
acceptControlTransfer(): void;
/**
* Release control back to peer (ACTIVE becomes PASSIVE)
*/
releaseControl(): void;
/**
* Check if currently connected to a session
*/
isConnected(): boolean;
/**
* Get current connection status
*/
getConnectionStatus(): ConnectionStatus;
/**
* Get current session ID (null if not connected)
*/
getCurrentSessionId(): string | null;
/**
* Get the unique client ID
*/
getClientId(): string;
/**
* List all available sessions (if supported by server)
*/
listAvailableSessions(): Promise<SessionData[]>;
/**
* Delete the current session (if connected)
*/
deleteCurrentSession(): Promise<boolean>;
private transitionToPhase;
private enforceValidTransition;
private enforcePermission;
private setupEventHandlers;
}
//# sourceMappingURL=RefactoredRelayClient.d.ts.map