summaryrefslogtreecommitdiffhomepage
path: root/src/core/protocol
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/protocol')
-rw-r--r--src/core/protocol/index.ts12
-rw-r--r--src/core/protocol/reducer.test.ts424
-rw-r--r--src/core/protocol/reducer.ts154
-rw-r--r--src/core/protocol/types.ts28
4 files changed, 309 insertions, 309 deletions
diff --git a/src/core/protocol/index.ts b/src/core/protocol/index.ts
index e7fd161..2c1c290 100644
--- a/src/core/protocol/index.ts
+++ b/src/core/protocol/index.ts
@@ -1,9 +1,9 @@
export {
- applyServerMessage,
- getSurfaceSpec,
- initialState,
- invoke,
- subscribe,
- unsubscribe,
+ applyServerMessage,
+ getSurfaceSpec,
+ initialState,
+ invoke,
+ subscribe,
+ unsubscribe,
} from "./reducer";
export type { ProtocolResult, ProtocolState, Subscription } from "./types";
diff --git a/src/core/protocol/reducer.test.ts b/src/core/protocol/reducer.test.ts
index c8e517a..d42dce7 100644
--- a/src/core/protocol/reducer.test.ts
+++ b/src/core/protocol/reducer.test.ts
@@ -1,245 +1,245 @@
import { describe, expect, it } from "vitest";
import {
- applyServerMessage,
- getSurfaceSpec,
- initialState,
- invoke,
- subscribe,
- unsubscribe,
+ applyServerMessage,
+ getSurfaceSpec,
+ initialState,
+ invoke,
+ subscribe,
+ unsubscribe,
} from "./reducer";
const makeSpec = (id: string, title = id) => ({
- id,
- region: "test",
- title,
- fields: [],
+ id,
+ region: "test",
+ title,
+ fields: [],
});
describe("initialState", () => {
- it("returns empty catalog, no subscriptions, no error", () => {
- const s = initialState();
- expect(s.catalog).toEqual([]);
- expect(s.subscriptions.size).toBe(0);
- expect(s.lastError).toBeNull();
- });
+ it("returns empty catalog, no subscriptions, no error", () => {
+ const s = initialState();
+ expect(s.catalog).toEqual([]);
+ expect(s.subscriptions.size).toBe(0);
+ expect(s.lastError).toBeNull();
+ });
});
describe("applyServerMessage — catalog", () => {
- it("replaces the catalog", () => {
- const s = initialState();
- const catalog = [
- { id: "a", region: "r", title: "A" },
- { id: "b", region: "r", title: "B" },
- ];
- const next = applyServerMessage(s, { type: "catalog", catalog });
- expect(next.catalog).toEqual(catalog);
- });
+ it("replaces the catalog", () => {
+ const s = initialState();
+ const catalog = [
+ { id: "a", region: "r", title: "A" },
+ { id: "b", region: "r", title: "B" },
+ ];
+ const next = applyServerMessage(s, { type: "catalog", catalog });
+ expect(next.catalog).toEqual(catalog);
+ });
});
describe("applyServerMessage — surface", () => {
- it("sets the spec for a subscribed surface", () => {
- let s = initialState();
- s = subscribe(s, "s1").state;
- const spec = makeSpec("s1", "Surface 1");
- const next = applyServerMessage(s, { type: "surface", spec });
- expect(getSurfaceSpec(next, "s1")).toEqual(spec);
- });
-
- it("ignores a surface message for a non-subscribed surface", () => {
- const s = initialState();
- const spec = makeSpec("unknown");
- const next = applyServerMessage(s, { type: "surface", spec });
- expect(next.subscriptions.has("unknown")).toBe(false);
- });
+ it("sets the spec for a subscribed surface", () => {
+ let s = initialState();
+ s = subscribe(s, "s1").state;
+ const spec = makeSpec("s1", "Surface 1");
+ const next = applyServerMessage(s, { type: "surface", spec });
+ expect(getSurfaceSpec(next, "s1")).toEqual(spec);
+ });
+
+ it("ignores a surface message for a non-subscribed surface", () => {
+ const s = initialState();
+ const spec = makeSpec("unknown");
+ const next = applyServerMessage(s, { type: "surface", spec });
+ expect(next.subscriptions.has("unknown")).toBe(false);
+ });
});
describe("applyServerMessage — update", () => {
- it("replaces spec for a subscribed surface", () => {
- let s = initialState();
- s = subscribe(s, "s1").state;
- s = applyServerMessage(s, { type: "surface", spec: makeSpec("s1", "V1") });
- const next = applyServerMessage(s, {
- type: "update",
- update: { surfaceId: "s1", spec: makeSpec("s1", "V2") },
- });
- expect(getSurfaceSpec(next, "s1")?.title).toBe("V2");
- });
-
- it("ignores an update for a non-subscribed surface", () => {
- const s = initialState();
- const next = applyServerMessage(s, {
- type: "update",
- update: { surfaceId: "nope", spec: makeSpec("nope") },
- });
- expect(next.subscriptions.has("nope")).toBe(false);
- });
+ it("replaces spec for a subscribed surface", () => {
+ let s = initialState();
+ s = subscribe(s, "s1").state;
+ s = applyServerMessage(s, { type: "surface", spec: makeSpec("s1", "V1") });
+ const next = applyServerMessage(s, {
+ type: "update",
+ update: { surfaceId: "s1", spec: makeSpec("s1", "V2") },
+ });
+ expect(getSurfaceSpec(next, "s1")?.title).toBe("V2");
+ });
+
+ it("ignores an update for a non-subscribed surface", () => {
+ const s = initialState();
+ const next = applyServerMessage(s, {
+ type: "update",
+ update: { surfaceId: "nope", spec: makeSpec("nope") },
+ });
+ expect(next.subscriptions.has("nope")).toBe(false);
+ });
});
describe("applyServerMessage — error", () => {
- it("records the error without throwing", () => {
- const s = initialState();
- const err = { type: "error" as const, surfaceId: "s1", message: "boom" };
- const next = applyServerMessage(s, err);
- expect(next.lastError).toEqual(err);
- });
-
- it("records error without surfaceId", () => {
- const s = initialState();
- const err = { type: "error" as const, message: "global boom" };
- const next = applyServerMessage(s, err);
- expect(next.lastError).toEqual(err);
- });
+ it("records the error without throwing", () => {
+ const s = initialState();
+ const err = { type: "error" as const, surfaceId: "s1", message: "boom" };
+ const next = applyServerMessage(s, err);
+ expect(next.lastError).toEqual(err);
+ });
+
+ it("records error without surfaceId", () => {
+ const s = initialState();
+ const err = { type: "error" as const, message: "global boom" };
+ const next = applyServerMessage(s, err);
+ expect(next.lastError).toEqual(err);
+ });
});
describe("subscribe", () => {
- it("emits exactly one subscribe message (global, no conversationId)", () => {
- const s = initialState();
- const result = subscribe(s, "s1");
- expect(result.outgoing).toEqual([{ type: "subscribe", surfaceId: "s1" }]);
- expect(result.outgoing).toHaveLength(1);
- });
-
- it("adds the surface to subscriptions with null spec", () => {
- const s = initialState();
- const result = subscribe(s, "s1");
- expect(result.state.subscriptions.get("s1")).toEqual({
- conversationId: undefined,
- spec: null,
- });
- expect(getSurfaceSpec(result.state, "s1")).toBeNull();
- });
-
- it("is idempotent — second subscribe with the same scope is a no-op", () => {
- let s = initialState();
- s = subscribe(s, "s1").state;
- const result = subscribe(s, "s1");
- expect(result.outgoing).toEqual([]);
- expect(result.state).toBe(s);
- });
+ it("emits exactly one subscribe message (global, no conversationId)", () => {
+ const s = initialState();
+ const result = subscribe(s, "s1");
+ expect(result.outgoing).toEqual([{ type: "subscribe", surfaceId: "s1" }]);
+ expect(result.outgoing).toHaveLength(1);
+ });
+
+ it("adds the surface to subscriptions with null spec", () => {
+ const s = initialState();
+ const result = subscribe(s, "s1");
+ expect(result.state.subscriptions.get("s1")).toEqual({
+ conversationId: undefined,
+ spec: null,
+ });
+ expect(getSurfaceSpec(result.state, "s1")).toBeNull();
+ });
+
+ it("is idempotent — second subscribe with the same scope is a no-op", () => {
+ let s = initialState();
+ s = subscribe(s, "s1").state;
+ const result = subscribe(s, "s1");
+ expect(result.outgoing).toEqual([]);
+ expect(result.state).toBe(s);
+ });
});
describe("subscribe — conversation-scoped", () => {
- it("includes conversationId in the subscribe message", () => {
- const s = initialState();
- const result = subscribe(s, "cache-warming", "conv-A");
- expect(result.outgoing).toEqual([
- { type: "subscribe", surfaceId: "cache-warming", conversationId: "conv-A" },
- ]);
- expect(result.state.subscriptions.get("cache-warming")?.conversationId).toBe("conv-A");
- });
-
- it("re-scopes on conversation switch: unsubscribe old pair then subscribe new", () => {
- let s = initialState();
- s = subscribe(s, "cw", "conv-A").state;
- s = applyServerMessage(s, {
- type: "surface",
- spec: makeSpec("cw", "A-spec"),
- conversationId: "conv-A",
- });
- const result = subscribe(s, "cw", "conv-B");
- expect(result.outgoing).toEqual([
- { type: "unsubscribe", surfaceId: "cw", conversationId: "conv-A" },
- { type: "subscribe", surfaceId: "cw", conversationId: "conv-B" },
- ]);
- // previous spec retained until the new one arrives (no flicker)
- expect(getSurfaceSpec(result.state, "cw")?.title).toBe("A-spec");
- expect(result.state.subscriptions.get("cw")?.conversationId).toBe("conv-B");
- });
-
- it("drops a stale update echoing the previous conversationId", () => {
- let s = initialState();
- s = subscribe(s, "cw", "conv-A").state;
- s = subscribe(s, "cw", "conv-B").state; // re-scoped to B
- const next = applyServerMessage(s, {
- type: "update",
- update: { surfaceId: "cw", spec: makeSpec("cw", "STALE-A"), conversationId: "conv-A" },
- });
- expect(getSurfaceSpec(next, "cw")).toBeNull(); // stale ignored, no spec yet for B
- });
-
- it("accepts an update echoing the current conversationId", () => {
- let s = initialState();
- s = subscribe(s, "cw", "conv-B").state;
- const next = applyServerMessage(s, {
- type: "update",
- update: { surfaceId: "cw", spec: makeSpec("cw", "B-spec"), conversationId: "conv-B" },
- });
- expect(getSurfaceSpec(next, "cw")?.title).toBe("B-spec");
- });
-
- it("accepts a global (no-echo) surface message even when subscribed with a conversationId", () => {
- // loaded-extensions is global: server ignores our conversationId and echoes none.
- let s = initialState();
- s = subscribe(s, "loaded-extensions", "conv-A").state;
- const next = applyServerMessage(s, {
- type: "surface",
- spec: makeSpec("loaded-extensions", "Ext"),
- });
- expect(getSurfaceSpec(next, "loaded-extensions")?.title).toBe("Ext");
- });
+ it("includes conversationId in the subscribe message", () => {
+ const s = initialState();
+ const result = subscribe(s, "cache-warming", "conv-A");
+ expect(result.outgoing).toEqual([
+ { type: "subscribe", surfaceId: "cache-warming", conversationId: "conv-A" },
+ ]);
+ expect(result.state.subscriptions.get("cache-warming")?.conversationId).toBe("conv-A");
+ });
+
+ it("re-scopes on conversation switch: unsubscribe old pair then subscribe new", () => {
+ let s = initialState();
+ s = subscribe(s, "cw", "conv-A").state;
+ s = applyServerMessage(s, {
+ type: "surface",
+ spec: makeSpec("cw", "A-spec"),
+ conversationId: "conv-A",
+ });
+ const result = subscribe(s, "cw", "conv-B");
+ expect(result.outgoing).toEqual([
+ { type: "unsubscribe", surfaceId: "cw", conversationId: "conv-A" },
+ { type: "subscribe", surfaceId: "cw", conversationId: "conv-B" },
+ ]);
+ // previous spec retained until the new one arrives (no flicker)
+ expect(getSurfaceSpec(result.state, "cw")?.title).toBe("A-spec");
+ expect(result.state.subscriptions.get("cw")?.conversationId).toBe("conv-B");
+ });
+
+ it("drops a stale update echoing the previous conversationId", () => {
+ let s = initialState();
+ s = subscribe(s, "cw", "conv-A").state;
+ s = subscribe(s, "cw", "conv-B").state; // re-scoped to B
+ const next = applyServerMessage(s, {
+ type: "update",
+ update: { surfaceId: "cw", spec: makeSpec("cw", "STALE-A"), conversationId: "conv-A" },
+ });
+ expect(getSurfaceSpec(next, "cw")).toBeNull(); // stale ignored, no spec yet for B
+ });
+
+ it("accepts an update echoing the current conversationId", () => {
+ let s = initialState();
+ s = subscribe(s, "cw", "conv-B").state;
+ const next = applyServerMessage(s, {
+ type: "update",
+ update: { surfaceId: "cw", spec: makeSpec("cw", "B-spec"), conversationId: "conv-B" },
+ });
+ expect(getSurfaceSpec(next, "cw")?.title).toBe("B-spec");
+ });
+
+ it("accepts a global (no-echo) surface message even when subscribed with a conversationId", () => {
+ // loaded-extensions is global: server ignores our conversationId and echoes none.
+ let s = initialState();
+ s = subscribe(s, "loaded-extensions", "conv-A").state;
+ const next = applyServerMessage(s, {
+ type: "surface",
+ spec: makeSpec("loaded-extensions", "Ext"),
+ });
+ expect(getSurfaceSpec(next, "loaded-extensions")?.title).toBe("Ext");
+ });
});
describe("unsubscribe", () => {
- it("emits unsubscribe and drops the spec", () => {
- let s = initialState();
- s = subscribe(s, "s1").state;
- s = applyServerMessage(s, { type: "surface", spec: makeSpec("s1") });
- const result = unsubscribe(s, "s1");
- expect(result.outgoing).toEqual([{ type: "unsubscribe", surfaceId: "s1" }]);
- expect(result.state.subscriptions.has("s1")).toBe(false);
- });
-
- it("includes conversationId for a scoped subscription", () => {
- let s = initialState();
- s = subscribe(s, "cw", "conv-A").state;
- const result = unsubscribe(s, "cw");
- expect(result.outgoing).toEqual([
- { type: "unsubscribe", surfaceId: "cw", conversationId: "conv-A" },
- ]);
- });
-
- it("is a no-op if not subscribed", () => {
- const s = initialState();
- const result = unsubscribe(s, "nope");
- expect(result.outgoing).toEqual([]);
- expect(result.state).toBe(s);
- });
+ it("emits unsubscribe and drops the spec", () => {
+ let s = initialState();
+ s = subscribe(s, "s1").state;
+ s = applyServerMessage(s, { type: "surface", spec: makeSpec("s1") });
+ const result = unsubscribe(s, "s1");
+ expect(result.outgoing).toEqual([{ type: "unsubscribe", surfaceId: "s1" }]);
+ expect(result.state.subscriptions.has("s1")).toBe(false);
+ });
+
+ it("includes conversationId for a scoped subscription", () => {
+ let s = initialState();
+ s = subscribe(s, "cw", "conv-A").state;
+ const result = unsubscribe(s, "cw");
+ expect(result.outgoing).toEqual([
+ { type: "unsubscribe", surfaceId: "cw", conversationId: "conv-A" },
+ ]);
+ });
+
+ it("is a no-op if not subscribed", () => {
+ const s = initialState();
+ const result = unsubscribe(s, "nope");
+ expect(result.outgoing).toEqual([]);
+ expect(result.state).toBe(s);
+ });
});
describe("invoke", () => {
- it("emits the correct InvokeMessage", () => {
- const s = initialState();
- const result = invoke(s, "s1", "toggle", true);
- expect(result.outgoing).toEqual([
- { type: "invoke", surfaceId: "s1", actionId: "toggle", payload: true },
- ]);
- });
-
- it("omits payload when not provided", () => {
- const s = initialState();
- const result = invoke(s, "s1", "click");
- expect(result.outgoing).toEqual([
- { type: "invoke", surfaceId: "s1", actionId: "click", payload: undefined },
- ]);
- });
-
- it("includes conversationId when provided", () => {
- const s = initialState();
- const result = invoke(s, "cw", "cache-warming/set-interval", 120, "conv-A");
- expect(result.outgoing).toEqual([
- {
- type: "invoke",
- surfaceId: "cw",
- actionId: "cache-warming/set-interval",
- payload: 120,
- conversationId: "conv-A",
- },
- ]);
- });
-
- it("does not mutate state", () => {
- const s = initialState();
- const result = invoke(s, "s1", "a1");
- expect(result.state).toBe(s);
- });
+ it("emits the correct InvokeMessage", () => {
+ const s = initialState();
+ const result = invoke(s, "s1", "toggle", true);
+ expect(result.outgoing).toEqual([
+ { type: "invoke", surfaceId: "s1", actionId: "toggle", payload: true },
+ ]);
+ });
+
+ it("omits payload when not provided", () => {
+ const s = initialState();
+ const result = invoke(s, "s1", "click");
+ expect(result.outgoing).toEqual([
+ { type: "invoke", surfaceId: "s1", actionId: "click", payload: undefined },
+ ]);
+ });
+
+ it("includes conversationId when provided", () => {
+ const s = initialState();
+ const result = invoke(s, "cw", "cache-warming/set-interval", 120, "conv-A");
+ expect(result.outgoing).toEqual([
+ {
+ type: "invoke",
+ surfaceId: "cw",
+ actionId: "cache-warming/set-interval",
+ payload: 120,
+ conversationId: "conv-A",
+ },
+ ]);
+ });
+
+ it("does not mutate state", () => {
+ const s = initialState();
+ const result = invoke(s, "s1", "a1");
+ expect(result.state).toBe(s);
+ });
});
diff --git a/src/core/protocol/reducer.ts b/src/core/protocol/reducer.ts
index 3d6b1c8..976eb88 100644
--- a/src/core/protocol/reducer.ts
+++ b/src/core/protocol/reducer.ts
@@ -1,34 +1,34 @@
import type {
- InvokeMessage,
- SubscribeMessage,
- SurfaceServerMessage,
- SurfaceSpec,
- UnsubscribeMessage,
+ InvokeMessage,
+ SubscribeMessage,
+ SurfaceServerMessage,
+ SurfaceSpec,
+ UnsubscribeMessage,
} from "@dispatch/ui-contract";
import type { ProtocolResult, ProtocolState } from "./types";
/** The initial protocol state: empty catalog, no subscriptions, no error. */
export function initialState(): ProtocolState {
- return {
- catalog: [],
- subscriptions: new Map(),
- lastError: null,
- };
+ return {
+ catalog: [],
+ subscriptions: new Map(),
+ lastError: null,
+ };
}
// ── Message builders (respect exactOptionalPropertyTypes: omit `conversationId`
// entirely for a global subscription rather than setting it to `undefined`). ──
function subMsg(surfaceId: string, conversationId: string | undefined): SubscribeMessage {
- return conversationId === undefined
- ? { type: "subscribe", surfaceId }
- : { type: "subscribe", surfaceId, conversationId };
+ return conversationId === undefined
+ ? { type: "subscribe", surfaceId }
+ : { type: "subscribe", surfaceId, conversationId };
}
function unsubMsg(surfaceId: string, conversationId: string | undefined): UnsubscribeMessage {
- return conversationId === undefined
- ? { type: "unsubscribe", surfaceId }
- : { type: "unsubscribe", surfaceId, conversationId };
+ return conversationId === undefined
+ ? { type: "unsubscribe", surfaceId }
+ : { type: "unsubscribe", surfaceId, conversationId };
}
/**
@@ -38,37 +38,37 @@ function unsubMsg(surfaceId: string, conversationId: string | undefined): Unsubs
* surface echoes nothing (`undefined`) and is always current.
*/
function isCurrent(desiredId: string | undefined, echoedId: string | undefined): boolean {
- return echoedId === undefined || echoedId === desiredId;
+ return echoedId === undefined || echoedId === desiredId;
}
/** Fold an inbound server message into the next protocol state. */
export function applyServerMessage(state: ProtocolState, msg: SurfaceServerMessage): ProtocolState {
- switch (msg.type) {
- case "catalog":
- return { ...state, catalog: msg.catalog };
+ switch (msg.type) {
+ case "catalog":
+ return { ...state, catalog: msg.catalog };
- case "surface": {
- const sub = state.subscriptions.get(msg.spec.id);
- if (sub === undefined) return state;
- if (!isCurrent(sub.conversationId, msg.conversationId)) return state;
- const subs = new Map(state.subscriptions);
- subs.set(msg.spec.id, { conversationId: sub.conversationId, spec: msg.spec });
- return { ...state, subscriptions: subs };
- }
+ case "surface": {
+ const sub = state.subscriptions.get(msg.spec.id);
+ if (sub === undefined) return state;
+ if (!isCurrent(sub.conversationId, msg.conversationId)) return state;
+ const subs = new Map(state.subscriptions);
+ subs.set(msg.spec.id, { conversationId: sub.conversationId, spec: msg.spec });
+ return { ...state, subscriptions: subs };
+ }
- case "update": {
- const { surfaceId, spec, conversationId } = msg.update;
- const sub = state.subscriptions.get(surfaceId);
- if (sub === undefined) return state;
- if (!isCurrent(sub.conversationId, conversationId)) return state;
- const subs = new Map(state.subscriptions);
- subs.set(surfaceId, { conversationId: sub.conversationId, spec });
- return { ...state, subscriptions: subs };
- }
+ case "update": {
+ const { surfaceId, spec, conversationId } = msg.update;
+ const sub = state.subscriptions.get(surfaceId);
+ if (sub === undefined) return state;
+ if (!isCurrent(sub.conversationId, conversationId)) return state;
+ const subs = new Map(state.subscriptions);
+ subs.set(surfaceId, { conversationId: sub.conversationId, spec });
+ return { ...state, subscriptions: subs };
+ }
- case "error":
- return { ...state, lastError: msg };
- }
+ case "error":
+ return { ...state, lastError: msg };
+ }
}
/**
@@ -82,23 +82,23 @@ export function applyServerMessage(state: ProtocolState, msg: SurfaceServerMessa
* one, retaining the previous spec until the new one arrives (no flicker).
*/
export function subscribe(
- state: ProtocolState,
- surfaceId: string,
- conversationId?: string,
+ state: ProtocolState,
+ surfaceId: string,
+ conversationId?: string,
): ProtocolResult {
- const existing = state.subscriptions.get(surfaceId);
- if (existing !== undefined && existing.conversationId === conversationId) {
- return { state, outgoing: [] };
- }
- const subs = new Map(state.subscriptions);
- const outgoing: (SubscribeMessage | UnsubscribeMessage)[] = [];
- const priorSpec: SurfaceSpec | null = existing?.spec ?? null;
- if (existing !== undefined) {
- outgoing.push(unsubMsg(surfaceId, existing.conversationId));
- }
- subs.set(surfaceId, { conversationId, spec: priorSpec });
- outgoing.push(subMsg(surfaceId, conversationId));
- return { state: { ...state, subscriptions: subs }, outgoing };
+ const existing = state.subscriptions.get(surfaceId);
+ if (existing !== undefined && existing.conversationId === conversationId) {
+ return { state, outgoing: [] };
+ }
+ const subs = new Map(state.subscriptions);
+ const outgoing: (SubscribeMessage | UnsubscribeMessage)[] = [];
+ const priorSpec: SurfaceSpec | null = existing?.spec ?? null;
+ if (existing !== undefined) {
+ outgoing.push(unsubMsg(surfaceId, existing.conversationId));
+ }
+ subs.set(surfaceId, { conversationId, spec: priorSpec });
+ outgoing.push(subMsg(surfaceId, conversationId));
+ return { state: { ...state, subscriptions: subs }, outgoing };
}
/**
@@ -107,16 +107,16 @@ export function subscribe(
* not subscribed.
*/
export function unsubscribe(state: ProtocolState, surfaceId: string): ProtocolResult {
- const existing = state.subscriptions.get(surfaceId);
- if (existing === undefined) {
- return { state, outgoing: [] };
- }
- const subs = new Map(state.subscriptions);
- subs.delete(surfaceId);
- return {
- state: { ...state, subscriptions: subs },
- outgoing: [unsubMsg(surfaceId, existing.conversationId)],
- };
+ const existing = state.subscriptions.get(surfaceId);
+ if (existing === undefined) {
+ return { state, outgoing: [] };
+ }
+ const subs = new Map(state.subscriptions);
+ subs.delete(surfaceId);
+ return {
+ state: { ...state, subscriptions: subs },
+ outgoing: [unsubMsg(surfaceId, existing.conversationId)],
+ };
}
/**
@@ -124,20 +124,20 @@ export function unsubscribe(state: ProtocolState, surfaceId: string): ProtocolRe
* `conversationId` for a scoped surface); no state change.
*/
export function invoke(
- state: ProtocolState,
- surfaceId: string,
- actionId: string,
- payload?: unknown,
- conversationId?: string,
+ state: ProtocolState,
+ surfaceId: string,
+ actionId: string,
+ payload?: unknown,
+ conversationId?: string,
): ProtocolResult {
- const outgoing: InvokeMessage =
- conversationId === undefined
- ? { type: "invoke", surfaceId, actionId, payload }
- : { type: "invoke", surfaceId, actionId, payload, conversationId };
- return { state, outgoing: [outgoing] };
+ const outgoing: InvokeMessage =
+ conversationId === undefined
+ ? { type: "invoke", surfaceId, actionId, payload }
+ : { type: "invoke", surfaceId, actionId, payload, conversationId };
+ return { state, outgoing: [outgoing] };
}
/** The current spec for a subscribed surface, or `null` if absent/unsubscribed. */
export function getSurfaceSpec(state: ProtocolState, surfaceId: string): SurfaceSpec | null {
- return state.subscriptions.get(surfaceId)?.spec ?? null;
+ return state.subscriptions.get(surfaceId)?.spec ?? null;
}
diff --git a/src/core/protocol/types.ts b/src/core/protocol/types.ts
index db8886a..6debb1d 100644
--- a/src/core/protocol/types.ts
+++ b/src/core/protocol/types.ts
@@ -1,8 +1,8 @@
import type {
- SurfaceCatalog,
- SurfaceClientMessage,
- SurfaceErrorMessage,
- SurfaceSpec,
+ SurfaceCatalog,
+ SurfaceClientMessage,
+ SurfaceErrorMessage,
+ SurfaceSpec,
} from "@dispatch/ui-contract";
/**
@@ -16,22 +16,22 @@ import type {
* is always accepted. `spec` is `null` until the first `surface` arrives.
*/
export interface Subscription {
- readonly conversationId: string | undefined;
- readonly spec: SurfaceSpec | null;
+ readonly conversationId: string | undefined;
+ readonly spec: SurfaceSpec | null;
}
/** The client-side view of the surface protocol state. */
export interface ProtocolState {
- /** The latest catalog received from the server (empty until first CatalogMessage). */
- readonly catalog: SurfaceCatalog;
- /** Surfaces the client intends to be subscribed to, keyed by surfaceId. */
- readonly subscriptions: ReadonlyMap<string, Subscription>;
- /** The last error received from the server, if any. */
- readonly lastError: SurfaceErrorMessage | null;
+ /** The latest catalog received from the server (empty until first CatalogMessage). */
+ readonly catalog: SurfaceCatalog;
+ /** Surfaces the client intends to be subscribed to, keyed by surfaceId. */
+ readonly subscriptions: ReadonlyMap<string, Subscription>;
+ /** The last error received from the server, if any. */
+ readonly lastError: SurfaceErrorMessage | null;
}
/** A state transition result: the next state plus any outgoing messages to send. */
export interface ProtocolResult {
- readonly state: ProtocolState;
- readonly outgoing: readonly SurfaceClientMessage[];
+ readonly state: ProtocolState;
+ readonly outgoing: readonly SurfaceClientMessage[];
}