Browse Source

refactor(host): reuse workspace stream baseline

_Kerman 2 months ago
parent
commit
230f3b8295

+ 3 - 2
packages/host/apiproxy/src/api-proxy.ts

@@ -3379,10 +3379,11 @@ export function createApiProxy(ctx: Context, defaults: ApiProxyDefaults): ApiPro
 
 
       host(_request, signal) {
       host(_request, signal) {
         const queue = new FrameQueue<RpcRequest<HostFrame>>()
         const queue = new FrameQueue<RpcRequest<HostFrame>>()
+        const committedWorkspaces = ctx.workspace.list()
         const committedWorkspaceIds = new Set(
         const committedWorkspaceIds = new Set(
-          ctx.workspace.list().map(workspace => String(workspace.id)),
+          committedWorkspaces.map(workspace => String(workspace.id)),
         )
         )
-        let committedWorkspaceOrder = ctx.workspace.list().map(workspace => workspaceView(workspace).workspaceId)
+        let committedWorkspaceOrder = committedWorkspaces.map(workspace => workspace.id)
         // Frame-dedup baseline, same posture as committedWorkspaceIds: the
         // Frame-dedup baseline, same posture as committedWorkspaceIds: the
         // stream opens against the current set; workspace.list re-baselines
         // stream opens against the current set; workspace.list re-baselines
         // reconnecting clients, so only later changes need frames.
         // reconnecting clients, so only later changes need frames.

+ 3 - 1
packages/host/apiproxy/tests/api-proxy-workspace.spec.ts

@@ -310,14 +310,16 @@ describe('workspace.create', () => {
 
 
 describe('workspace.insertBefore', () => {
 describe('workspace.insertBefore', () => {
   it('commits the complete order, streams one order frame, and maps unknown ids', async () => {
   it('commits the complete order, streams one order frame, and maps unknown ids', async () => {
-    const { api, root } = await harness()
+    const { api, ctx, root } = await harness()
     const first = expectOk(await api.workspace.create(request({ path: stageDir(root, 'first') }))).workspace
     const first = expectOk(await api.workspace.create(request({ path: stageDir(root, 'first') }))).workspace
     const second = expectOk(await api.workspace.create(request({ path: stageDir(root, 'second') }))).workspace
     const second = expectOk(await api.workspace.create(request({ path: stageDir(root, 'second') }))).workspace
     const third = expectOk(await api.workspace.create(request({ path: stageDir(root, 'third') }))).workspace
     const third = expectOk(await api.workspace.create(request({ path: stageDir(root, 'third') }))).workspace
 
 
     const abort = new AbortController()
     const abort = new AbortController()
+    const listWorkspaces = vi.spyOn(ctx.workspace, 'list')
     const stream: AsyncIterator<RpcRequest<HostFrame>> =
     const stream: AsyncIterator<RpcRequest<HostFrame>> =
       api.events.host(request({}), abort.signal)[Symbol.asyncIterator]()
       api.events.host(request({}), abort.signal)[Symbol.asyncIterator]()
+    expect(listWorkspaces).toHaveBeenCalledTimes(1)
     const changed = nextHostFrame(stream)
     const changed = nextHostFrame(stream)
     const reordered = expectOk(await api.workspace.insertBefore(request({
     const reordered = expectOk(await api.workspace.insertBefore(request({
       workspaceId: first.workspaceId,
       workspaceId: first.workspaceId,