Skip to content

Commit 88eb47a

Browse files
committed
more comments
1 parent 7d3acf7 commit 88eb47a

File tree

4 files changed

+47
-95
lines changed

4 files changed

+47
-95
lines changed

src/common/managedTimeout.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export interface ManagedTimeout {
2+
cancel: () => void;
3+
restart: () => void;
4+
}
5+
6+
export function setManagedTimeout(callback: () => Promise<void> | void, timeoutMS: number): ManagedTimeout {
7+
let timeoutId: NodeJS.Timeout | undefined = setTimeout(() => {
8+
void callback();
9+
}, timeoutMS);
10+
11+
function cancel() {
12+
clearTimeout(timeoutId);
13+
timeoutId = undefined;
14+
}
15+
16+
function restart() {
17+
cancel();
18+
timeoutId = setTimeout(() => {
19+
void callback();
20+
}, timeoutMS);
21+
}
22+
23+
return {
24+
cancel,
25+
restart,
26+
};
27+
}

src/common/sessionStore.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
22
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
33
import logger, { LogId, LoggerBase, McpLogger } from "./logger.js";
4-
import { TimeoutManager } from "./timeoutManager.js";
4+
import { ManagedTimeout, setManagedTimeout } from "./managedTimeout.js";
55

66
export class SessionStore {
77
private sessions: {
88
[sessionId: string]: {
99
logger: LoggerBase;
1010
transport: StreamableHTTPServerTransport;
11-
abortTimeout: TimeoutManager;
12-
notificationTimeout: TimeoutManager;
11+
abortTimeout: ManagedTimeout;
12+
notificationTimeout: ManagedTimeout;
1313
};
1414
} = {};
1515

@@ -39,9 +39,9 @@ export class SessionStore {
3939
return;
4040
}
4141

42-
session.abortTimeout.reset();
42+
session.abortTimeout.restart();
4343

44-
session.notificationTimeout.reset();
44+
session.notificationTimeout.restart();
4545
}
4646

4747
private sendNotification(sessionId: string): void {
@@ -66,7 +66,7 @@ export class SessionStore {
6666
if (session) {
6767
throw new Error(`Session ${sessionId} already exists`);
6868
}
69-
const abortTimeout = new TimeoutManager(async () => {
69+
const abortTimeout = setManagedTimeout(async () => {
7070
if (this.sessions[sessionId]) {
7171
this.sessions[sessionId].logger.info(
7272
LogId.streamableHttpTransportSessionCloseNotification,
@@ -77,7 +77,7 @@ export class SessionStore {
7777
await this.closeSession(sessionId);
7878
}
7979
}, this.idleTimeoutMS);
80-
const notificationTimeout = new TimeoutManager(
80+
const notificationTimeout = setManagedTimeout(
8181
() => this.sendNotification(sessionId),
8282
this.notificationTimeoutMS
8383
);
@@ -89,8 +89,8 @@ export class SessionStore {
8989
if (!session) {
9090
throw new Error(`Session ${sessionId} not found`);
9191
}
92-
session.abortTimeout.clear();
93-
session.notificationTimeout.clear();
92+
session.abortTimeout.cancel();
93+
session.notificationTimeout.cancel();
9494
if (closeTransport) {
9595
try {
9696
await session.transport.close();

src/common/timeoutManager.ts

Lines changed: 0 additions & 63 deletions
This file was deleted.

tests/unit/common/timeoutManager.test.ts renamed to tests/unit/common/managedTimeout.test.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
2-
import { TimeoutManager } from "../../../src/common/timeoutManager.js";
2+
import { setManagedTimeout } from "../../../src/common/managedTimeout.js";
33

4-
describe("TimeoutManager", () => {
4+
describe("setManagedTimeout", () => {
55
beforeAll(() => {
66
vi.useFakeTimers();
77
});
@@ -13,7 +13,7 @@ describe("TimeoutManager", () => {
1313
it("calls the timeout callback", () => {
1414
const callback = vi.fn();
1515

16-
new TimeoutManager(callback, 1000);
16+
setManagedTimeout(callback, 1000);
1717

1818
vi.advanceTimersByTime(1000);
1919
expect(callback).toHaveBeenCalled();
@@ -22,10 +22,10 @@ describe("TimeoutManager", () => {
2222
it("does not call the timeout callback if the timeout is cleared", () => {
2323
const callback = vi.fn();
2424

25-
const timeoutManager = new TimeoutManager(callback, 1000);
25+
const timeout = setManagedTimeout(callback, 1000);
2626

2727
vi.advanceTimersByTime(500);
28-
timeoutManager.clear();
28+
timeout.cancel();
2929
vi.advanceTimersByTime(500);
3030

3131
expect(callback).not.toHaveBeenCalled();
@@ -34,44 +34,32 @@ describe("TimeoutManager", () => {
3434
it("does not call the timeout callback if the timeout is reset", () => {
3535
const callback = vi.fn();
3636

37-
const timeoutManager = new TimeoutManager(callback, 1000);
37+
const timeout = setManagedTimeout(callback, 1000);
3838

3939
vi.advanceTimersByTime(500);
40-
timeoutManager.reset();
40+
timeout.restart();
4141
vi.advanceTimersByTime(500);
4242
expect(callback).not.toHaveBeenCalled();
4343
});
4444

45-
it("calls the onerror callback", () => {
46-
const onerrorCallback = vi.fn();
47-
48-
const tm = new TimeoutManager(() => {
49-
throw new Error("test");
50-
}, 1000);
51-
tm.onerror = onerrorCallback;
52-
53-
vi.advanceTimersByTime(1000);
54-
expect(onerrorCallback).toHaveBeenCalled();
55-
});
56-
5745
describe("if timeout is reset", () => {
5846
it("does not call the timeout callback within the timeout period", () => {
5947
const callback = vi.fn();
6048

61-
const timeoutManager = new TimeoutManager(callback, 1000);
49+
const timeout = setManagedTimeout(callback, 1000);
6250

6351
vi.advanceTimersByTime(500);
64-
timeoutManager.reset();
52+
timeout.restart();
6553
vi.advanceTimersByTime(500);
6654
expect(callback).not.toHaveBeenCalled();
6755
});
6856
it("calls the timeout callback after the timeout period", () => {
6957
const callback = vi.fn();
7058

71-
const timeoutManager = new TimeoutManager(callback, 1000);
59+
const timeout = setManagedTimeout(callback, 1000);
7260

7361
vi.advanceTimersByTime(500);
74-
timeoutManager.reset();
62+
timeout.restart();
7563
vi.advanceTimersByTime(1000);
7664
expect(callback).toHaveBeenCalled();
7765
});

0 commit comments

Comments
 (0)