Skip to content

Commit b640511

Browse files
transport: replace started/ended booleans with TurnState enum
Replace the two boolean flags (started, ended) tracking server turn lifecycle with an explicit TurnState enum (INITIALIZED → STARTED → ENDED). This makes valid transitions self-documenting and prevents impossible states, consistent with how Ably's other SDKs model lifecycle and with the ClientTransportState enum added in c13618c. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 2dca022 commit b640511

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

src/core/transport/server-transport.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ interface RegisteredTurn {
5454
onError?: (error: Ably.ErrorInfo) => void;
5555
}
5656

57+
// ---------------------------------------------------------------------------
58+
// Internal state machine
59+
// ---------------------------------------------------------------------------
60+
61+
enum TurnState {
62+
INITIALIZED = 'initialized',
63+
STARTED = 'started',
64+
ENDED = 'ended',
65+
}
66+
5767
// ---------------------------------------------------------------------------
5868
// Implementation
5969
// ---------------------------------------------------------------------------
@@ -252,8 +262,7 @@ class DefaultServerTransport<TEvent, TMessage> implements ServerTransport<TEvent
252262
} = turnOpts;
253263

254264
const controller = new AbortController();
255-
let started = false;
256-
let ended = false;
265+
let state = TurnState.INITIALIZED;
257266

258267
// Spec: AIT-ST3a — register immediately so early cancels can fire the abort signal.
259268
const registration: RegisteredTurn = {
@@ -294,8 +303,8 @@ class DefaultServerTransport<TEvent, TMessage> implements ServerTransport<TEvent
294303
400,
295304
);
296305
}
297-
if (started) return;
298-
started = true;
306+
if (state !== TurnState.INITIALIZED) return;
307+
state = TurnState.STARTED;
299308

300309
try {
301310
await turnManager.startTurn(turnId, turnClientId, controller, {
@@ -321,7 +330,7 @@ class DefaultServerTransport<TEvent, TMessage> implements ServerTransport<TEvent
321330
addMessages: async (nodes: MessageNode<TMessage>[], opts?: AddMessageOptions): Promise<AddMessagesResult> => {
322331
logger?.trace('Turn.addMessages();', { turnId, count: nodes.length });
323332

324-
if (!started) {
333+
if (state === TurnState.INITIALIZED) {
325334
throw new Ably.ErrorInfo(
326335
`unable to add messages; start() must be called before addMessages() (turn ${turnId})`,
327336
ErrorCode.InvalidArgument,
@@ -364,7 +373,7 @@ class DefaultServerTransport<TEvent, TMessage> implements ServerTransport<TEvent
364373
addEvents: async (nodes: EventsNode<TEvent>[]): Promise<void> => {
365374
logger?.trace('Turn.addEvents();', { turnId, count: nodes.length });
366375

367-
if (!started) {
376+
if (state === TurnState.INITIALIZED) {
368377
throw new Ably.ErrorInfo(
369378
`unable to add events; start() must be called before addEvents() (turn ${turnId})`,
370379
ErrorCode.InvalidArgument,
@@ -406,7 +415,7 @@ class DefaultServerTransport<TEvent, TMessage> implements ServerTransport<TEvent
406415
): Promise<StreamResult> => {
407416
logger?.trace('Turn.streamResponse();', { turnId });
408417

409-
if (!started) {
418+
if (state === TurnState.INITIALIZED) {
410419
throw new Ably.ErrorInfo(
411420
`unable to stream response; start() must be called before streamResponse() (turn ${turnId})`,
412421
ErrorCode.InvalidArgument,
@@ -444,15 +453,15 @@ class DefaultServerTransport<TEvent, TMessage> implements ServerTransport<TEvent
444453
end: async (reason: TurnEndReason): Promise<void> => {
445454
logger?.trace('Turn.end();', { turnId, reason });
446455

447-
if (!started) {
456+
if (state === TurnState.INITIALIZED) {
448457
throw new Ably.ErrorInfo(
449458
`unable to end turn; start() must be called before end() (turn ${turnId})`,
450459
ErrorCode.InvalidArgument,
451460
400,
452461
);
453462
}
454-
if (ended) return;
455-
ended = true;
463+
if (state === TurnState.ENDED) return;
464+
state = TurnState.ENDED;
456465

457466
try {
458467
await turnManager.endTurn(turnId, reason);

0 commit comments

Comments
 (0)