Skip to content

Commit 1368b69

Browse files
committed
Refactor: Deduplicate assertions with assertGet helper
Consolidate 4 duplicate assertions into single private assertGet() method. Cleaner and ensures consistent error messages across all workspace access.
1 parent be1e343 commit 1368b69

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/stores/WorkspaceStore.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,16 @@ export class WorkspaceStore {
326326
return this.states.subscribeKey(workspaceId, listener);
327327
};
328328

329+
/**
330+
* Assert that workspace exists and return its aggregator.
331+
* Centralized assertion for all workspace access methods.
332+
*/
333+
private assertGet(workspaceId: string): StreamingMessageAggregator {
334+
const aggregator = this.aggregators.get(workspaceId);
335+
assert(aggregator, `Workspace ${workspaceId} not found - must call addWorkspace() first`);
336+
return aggregator;
337+
}
338+
329339
/**
330340
* Get state for a specific workspace.
331341
* Lazy computation - only runs when version changes.
@@ -334,8 +344,7 @@ export class WorkspaceStore {
334344
*/
335345
getWorkspaceState(workspaceId: string): WorkspaceState {
336346
return this.states.get(workspaceId, () => {
337-
const aggregator = this.aggregators.get(workspaceId);
338-
assert(aggregator, `Workspace ${workspaceId} not found - must call addWorkspace() first`);
347+
const aggregator = this.assertGet(workspaceId);
339348

340349
const hasMessages = aggregator.hasMessages();
341350
const isCaughtUp = this.caughtUp.get(workspaceId) ?? false;
@@ -425,9 +434,7 @@ export class WorkspaceStore {
425434
* REQUIRES: Workspace must have been added via addWorkspace() first.
426435
*/
427436
getAggregator(workspaceId: string): StreamingMessageAggregator {
428-
const aggregator = this.aggregators.get(workspaceId);
429-
assert(aggregator, `Workspace ${workspaceId} not found - must call addWorkspace() first`);
430-
return aggregator;
437+
return this.assertGet(workspaceId);
431438
}
432439

433440
/**
@@ -447,8 +454,7 @@ export class WorkspaceStore {
447454
*/
448455
getWorkspaceUsage(workspaceId: string): WorkspaceUsageState {
449456
return this.usageStore.get(workspaceId, () => {
450-
const aggregator = this.aggregators.get(workspaceId);
451-
assert(aggregator, `Workspace ${workspaceId} not found - must call addWorkspace() first`);
457+
const aggregator = this.assertGet(workspaceId);
452458

453459
const messages = aggregator.getAllMessages();
454460

@@ -893,11 +899,7 @@ export class WorkspaceStore {
893899

894900
private handleChatMessage(workspaceId: string, data: WorkspaceChatMessage): void {
895901
// Aggregator must exist - IPC subscription happens in addWorkspace()
896-
const aggregator = this.aggregators.get(workspaceId);
897-
assert(
898-
aggregator,
899-
`Workspace ${workspaceId} not found - IPC message arrived before addWorkspace()`
900-
);
902+
const aggregator = this.assertGet(workspaceId);
901903

902904
const isCaughtUp = this.caughtUp.get(workspaceId) ?? false;
903905
const historicalMsgs = this.historicalMessages.get(workspaceId) ?? [];

0 commit comments

Comments
 (0)