Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
207f710
Defer presence namespace handshake until first presence operation
mstdokumaci Nov 21, 2026
8ab7e21
Add presence namespace handling and tests for same-namespace scope
mstdokumaci Nov 21, 2026
a96af13
Implement presence scope generation and handle superseded setups in C…
mstdokumaci Nov 21, 2026
9d1b165
Refactor presence handling in ConnectionManager and improve error man…
mstdokumaci Nov 21, 2026
e4e210c
Enhance presence handling with namespace switch logic and improve sub…
mstdokumaci Nov 21, 2026
241ce40
Implement ensurePresenceScope method and update PresenceImpl for name…
mstdokumaci Nov 21, 2026
6794596
Refactor presence scope management in ConnectionManager to remove con…
mstdokumaci Nov 21, 2026
aefab7c
Refactor presence handling in ConnectionManager and PresenceImpl to s…
mstdokumaci Nov 21, 2026
a3618be
Refactor presence namespace handling in ConnectionManager to ensure s…
mstdokumaci Nov 21, 2026
bf95e72
Add tests for presence namespace switching and handle request superse…
mstdokumaci Nov 22, 2026
464208d
Add tests for handling namespace switch rejection and ensure queued p…
mstdokumaci Nov 22, 2026
1649017
Enhance presence management by adding support for queued presence set…
mstdokumaci Nov 22, 2026
6018664
Add tests for concurrent presence namespace switches and ensure old s…
mstdokumaci Nov 22, 2026
7a08f6c
Enhance presence management by adding automatic retry for dispatch re…
mstdokumaci Nov 22, 2026
45d4b7c
Refactor presence management to ensure shared subscription cleanup an…
mstdokumaci Nov 22, 2026
1480b7f
Adjust retryable store action test to disable retry rate limits and e…
mstdokumaci Nov 22, 2026
3614da8
Enhance presence management and error handling for retryable actions,…
mstdokumaci Nov 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion examples/quorum/room.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,10 @@ describe("quorum voting", () => {
presenceNamespace: roomNamespace(roomB),
reconnect: false,
});
await expect(client.connect()).rejects.toThrow();
await client.connect();
await expect(client.presence.subscribe(() => {})).rejects.toMatchObject({
code: "NAMESPACE_UNAUTHORIZED",
});
client.disconnect();
});

Expand Down
78 changes: 77 additions & 1 deletion sdk/typescript/src/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ async function setupSchema(schema: SchemaDictionary): Promise<void> {
function createMockConnection(options?: {
response?: OkResponse;
error?: unknown;
errorFor?: (msg: OutboundRequest) => unknown;
ready?: boolean;
}) {
const schema = new SchemaDictionary();
Expand All @@ -42,7 +43,8 @@ function createMockConnection(options?: {
return {
dispatchNoRetry: (msg: OutboundRequest) => {
dispatched.push(msg);
if (options?.error) return Promise.reject(options.error);
const error = options?.errorFor?.(msg) ?? options?.error;
if (error) return Promise.reject(error);
return Promise.resolve(
options?.response ?? ({ type: "ok", id: 0 } as OkResponse),
);
Expand Down Expand Up @@ -350,4 +352,78 @@ describe("ActionsImpl.handle", () => {
action_ids: ["checkout"],
});
});
test("replayRegistrations partitions store and presence batches", async () => {
const conn = createMockConnection();
await setupSchema(conn.schemaDictionary);
const actions = new ActionsImpl(conn);
await actions.handle("checkout", () => ({}));
await actions.handle("player_move", () => {});
conn.dispatched.length = 0;
actions.replayRegistrations();
await nextTick();
expect(conn.dispatched).toHaveLength(2);
expect(conn.dispatched).toContainEqual({
type: "ActionRegister",
action_ids: ["checkout"],
});
expect(conn.dispatched).toContainEqual({
type: "ActionRegister",
action_ids: ["player_move"],
});
});
test("a presence registration failure does not block store replay", async () => {
let failPresence = false;
const presenceError = new ZyncBaseError("presence denied", {
code: ErrorCodes.NAMESPACE_UNAUTHORIZED,
category: "authorization",
retryable: false,
});
const errors: ZyncBaseError[] = [];
const conn = createMockConnection({
errorFor: (msg) =>
failPresence &&
msg.type === "ActionRegister" &&
msg.action_ids.includes("player_move")
? presenceError
: undefined,
});
await setupSchema(conn.schemaDictionary);
const actions = new ActionsImpl(conn, (err) => errors.push(err));
await actions.handle("checkout", () => ({}));
await actions.handle("player_move", () => {});
conn.dispatched.length = 0;
failPresence = true;
const replay = actions.replayRegistrations();
await Promise.allSettled([replay.store, replay.presence]);
expect(conn.dispatched).toHaveLength(2);
expect(conn.dispatched).toContainEqual({
type: "ActionRegister",
action_ids: ["checkout"],
});
expect(errors.some((err) => err.code === "NAMESPACE_UNAUTHORIZED")).toBe(
true,
);
});
test("a retryable registration failure stays retryable", async () => {
let fail = false;
const registrationError = new ZyncBaseError("server busy", {
code: ErrorCodes.INTERNAL_ERROR,
category: "server",
retryable: true,
});
const conn = createMockConnection({
errorFor: (msg) =>
fail && msg.type === "ActionRegister" ? registrationError : undefined,
});
await setupSchema(conn.schemaDictionary);
const actions = new ActionsImpl(conn);
await actions.handle("checkout", () => ({}));
fail = true;
const replay = actions.replayRegistrations();
const rejected = await replay.store.then(
() => null,
(err: { retryable?: boolean }) => err,
);
expect(rejected?.retryable).toBe(true);
});
});
41 changes: 33 additions & 8 deletions sdk/typescript/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import type {

/** The subset of ConnectionManager that ActionsImpl depends on. */
export interface ActionsConnection {
/** Send a request with automatic retry (e.g. registration). */
dispatch?(msg: OutboundRequest): Promise<OkResponse>;
/** Send a request without automatic retry (action calls are never retried). */
dispatchNoRetry(msg: OutboundRequest): Promise<OkResponse>;
/** Encode a one-way outbound message (ActionReply). */
Expand Down Expand Up @@ -92,30 +94,53 @@ export class ActionsImpl implements Actions {
}

/** Re-register all handlers after reconnect or a bound-scope namespace switch. */
replayRegistrations(): void {
if (this.handlers.size === 0 || !this.conn.isSchemaReady()) return;
replayRegistrations(): {
store: Promise<void>;
presence: Promise<void>;
} {
if (this.handlers.size === 0 || !this.conn.isSchemaReady()) {
return { store: Promise.resolve(), presence: Promise.resolve() };
}

const names: string[] = [];
const storeNames: string[] = [];
const presenceNames: string[] = [];
for (const name of this.handlers.keys()) {
try {
this.conn.schemaDictionary.getAction(name);
names.push(name);
const action = this.conn.schemaDictionary.getAction(name);
if (action.scope === "presence") {
presenceNames.push(name);
} else {
storeNames.push(name);
}
} catch {
// Schema changed and the action no longer exists; drop the handler.
this.handlers.delete(name);
}
}
if (names.length > 0) void this.register(names).catch(() => {});
return {
store:
storeNames.length > 0 ? this.register(storeNames) : Promise.resolve(),
presence:
presenceNames.length > 0
? this.register(presenceNames)
: Promise.resolve(),
};
}

private async register(names: string[]): Promise<void> {
try {
await this.conn.dispatchNoRetry({
const dispatch = this.conn.dispatch
? (msg: OutboundRequest) => this.conn.dispatch?.(msg)
: (msg: OutboundRequest) => this.conn.dispatchNoRetry(msg);
await dispatch({
Comment thread
mstdokumaci marked this conversation as resolved.
Resolved
type: "ActionRegister",
action_ids: names,
});
} catch (err) {
const error = mapActionError(err);
const error =
err instanceof ZyncBaseError && err.retryable
? err
: mapActionError(err);
this.emitError(error);
throw error;
}
Expand Down
Loading
Loading