@palantir/pack.state.foundry-event 0.0.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-lint.log +57 -0
- package/.turbo/turbo-transpileBrowser.log +5 -0
- package/.turbo/turbo-transpileCjs.log +5 -0
- package/.turbo/turbo-transpileEsm.log +5 -0
- package/.turbo/turbo-transpileTypes.log +5 -0
- package/.turbo/turbo-typecheck.log +4 -0
- package/LICENSE.txt +13 -0
- package/build/browser/index.js +403 -0
- package/build/browser/index.js.map +1 -0
- package/build/cjs/index.cjs +427 -0
- package/build/cjs/index.cjs.map +1 -0
- package/build/cjs/index.d.cts +78 -0
- package/build/esm/index.js +403 -0
- package/build/esm/index.js.map +1 -0
- package/build/types/FoundryEventService.d.ts +21 -0
- package/build/types/FoundryEventService.d.ts.map +1 -0
- package/build/types/__tests__/EventServiceCometD.reconnection.test.d.ts +1 -0
- package/build/types/__tests__/EventServiceCometD.reconnection.test.d.ts.map +1 -0
- package/build/types/cometd/EventServiceCometD.d.ts +20 -0
- package/build/types/cometd/EventServiceCometD.d.ts.map +1 -0
- package/build/types/index.d.ts +4 -0
- package/build/types/index.d.ts.map +1 -0
- package/build/types/types/EventService.d.ts +37 -0
- package/build/types/types/EventService.d.ts.map +1 -0
- package/package.json +71 -0
- package/src/FoundryEventService.ts +285 -0
- package/src/__tests__/EventServiceCometD.reconnection.test.ts +498 -0
- package/src/cometd/EventServiceCometD.ts +291 -0
- package/src/index.ts +27 -0
- package/src/types/EventService.ts +69 -0
- package/tsconfig.json +21 -0
- package/vitest.config.mjs +26 -0
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 Palantir Technologies, Inc. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { PackAppInternal } from "@palantir/pack.core";
|
|
18
|
+
import type { Callback, CometD, ListenerHandle, SubscriptionHandle } from "cometd";
|
|
19
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
20
|
+
import type { MockProxy } from "vitest-mock-extended";
|
|
21
|
+
import { mock } from "vitest-mock-extended";
|
|
22
|
+
import { EventServiceCometD } from "../cometd/EventServiceCometD.js";
|
|
23
|
+
import type { TypedReceiveChannelId } from "../types/EventService.js";
|
|
24
|
+
|
|
25
|
+
const mockAuthModule = {
|
|
26
|
+
getToken: vi.fn().mockResolvedValue("mock-token"),
|
|
27
|
+
onTokenChange: vi.fn(),
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const mockLogger = {
|
|
31
|
+
child: vi.fn(),
|
|
32
|
+
debug: vi.fn((...args: unknown[]) => {
|
|
33
|
+
console.log("[DEBUG]", ...args);
|
|
34
|
+
}),
|
|
35
|
+
error: vi.fn((...args: unknown[]) => {
|
|
36
|
+
console.error("[ERROR]", ...args);
|
|
37
|
+
}),
|
|
38
|
+
info: vi.fn((...args: unknown[]) => {
|
|
39
|
+
console.log("[INFO]", ...args);
|
|
40
|
+
}),
|
|
41
|
+
warn: vi.fn((...args: unknown[]) => {
|
|
42
|
+
console.warn("[WARN]", ...args);
|
|
43
|
+
}),
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
mockLogger.child.mockReturnValue(mockLogger);
|
|
47
|
+
|
|
48
|
+
const mockApp = {
|
|
49
|
+
config: {
|
|
50
|
+
logger: mockLogger,
|
|
51
|
+
remote: {
|
|
52
|
+
packWsPath: "/ws",
|
|
53
|
+
baseUrl: "https://test.example.com",
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
getModule: vi.fn().mockReturnValue(mockAuthModule),
|
|
57
|
+
} as unknown as PackAppInternal;
|
|
58
|
+
|
|
59
|
+
describe("EventServiceCometD Reconnection Handling", () => {
|
|
60
|
+
let mockCometD: MockProxy<CometD>;
|
|
61
|
+
let service: EventServiceCometD;
|
|
62
|
+
let handshakeCallback: Callback | undefined;
|
|
63
|
+
|
|
64
|
+
beforeEach(() => {
|
|
65
|
+
vi.useFakeTimers();
|
|
66
|
+
mockCometD = mock();
|
|
67
|
+
handshakeCallback = undefined;
|
|
68
|
+
|
|
69
|
+
mockCometD.addListener.mockImplementation((channel: string, callback: Callback) => {
|
|
70
|
+
if (channel === "/meta/handshake") {
|
|
71
|
+
handshakeCallback = callback;
|
|
72
|
+
setTimeout(() => {
|
|
73
|
+
callback({
|
|
74
|
+
channel: "/meta/handshake",
|
|
75
|
+
clientId: "test-client-id",
|
|
76
|
+
connectionType: "websocket",
|
|
77
|
+
successful: true,
|
|
78
|
+
});
|
|
79
|
+
}, 0);
|
|
80
|
+
}
|
|
81
|
+
return { id: `listener-${Math.random()}` } as ListenerHandle;
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
mockCometD.configure.mockImplementation(() => {});
|
|
85
|
+
mockCometD.registerExtension.mockImplementation(() => true);
|
|
86
|
+
mockCometD.handshake.mockImplementation(() => {});
|
|
87
|
+
mockCometD.batch.mockImplementation(callback => {
|
|
88
|
+
callback();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
service = new EventServiceCometD(mockApp, mockCometD);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
afterEach(() => {
|
|
95
|
+
vi.useRealTimers();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
async function completeHandshake(clientId = "test-client-id"): Promise<void> {
|
|
99
|
+
if (handshakeCallback == null) {
|
|
100
|
+
throw new Error("Handshake callback not captured");
|
|
101
|
+
}
|
|
102
|
+
handshakeCallback({
|
|
103
|
+
channel: "/meta/handshake",
|
|
104
|
+
clientId,
|
|
105
|
+
connectionType: "websocket",
|
|
106
|
+
successful: true,
|
|
107
|
+
});
|
|
108
|
+
await vi.runAllTimersAsync();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
describe("initial handshake", () => {
|
|
112
|
+
it("should detect initial handshake and log appropriately", async () => {
|
|
113
|
+
// Trigger lazy initialization by subscribing
|
|
114
|
+
const subscribeCapture = createSubscribeCapture(mockCometD);
|
|
115
|
+
const subscriptionPromise = service.subscribe(
|
|
116
|
+
"/test/channel" as TypedReceiveChannelId<object>,
|
|
117
|
+
vi.fn(),
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
await vi.runAllTimersAsync();
|
|
121
|
+
|
|
122
|
+
subscribeCapture.subscribeCallback?.({ channel: "/test/channel", successful: true });
|
|
123
|
+
await subscriptionPromise;
|
|
124
|
+
|
|
125
|
+
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
126
|
+
"CometD handshake successful",
|
|
127
|
+
expect.objectContaining({
|
|
128
|
+
clientId: "test-client-id",
|
|
129
|
+
connectionType: "websocket",
|
|
130
|
+
}),
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
expect(mockLogger.info).not.toHaveBeenCalledWith(
|
|
134
|
+
"CometD reconnection successful",
|
|
135
|
+
expect.anything(),
|
|
136
|
+
);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("should not attempt resubscription on initial handshake with no subscriptions", async () => {
|
|
140
|
+
// Trigger lazy initialization by subscribing
|
|
141
|
+
const subscribeCapture = createSubscribeCapture(mockCometD);
|
|
142
|
+
const subscriptionPromise = service.subscribe(
|
|
143
|
+
"/test/channel" as TypedReceiveChannelId<object>,
|
|
144
|
+
vi.fn(),
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
await vi.runAllTimersAsync();
|
|
148
|
+
|
|
149
|
+
subscribeCapture.subscribeCallback?.({ channel: "/test/channel", successful: true });
|
|
150
|
+
await subscriptionPromise;
|
|
151
|
+
|
|
152
|
+
expect(mockCometD.resubscribe).not.toHaveBeenCalled();
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
describe("reconnection after disconnect", () => {
|
|
157
|
+
it("should trigger handshake callback on reconnection", async () => {
|
|
158
|
+
// Trigger initial handshake first
|
|
159
|
+
const subscribeCapture = createSubscribeCapture(mockCometD);
|
|
160
|
+
const subscriptionPromise = service.subscribe(
|
|
161
|
+
"/test/channel" as TypedReceiveChannelId<object>,
|
|
162
|
+
vi.fn(),
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
await vi.runAllTimersAsync();
|
|
166
|
+
|
|
167
|
+
subscribeCapture.subscribeCallback?.({ channel: "/test/channel", successful: true });
|
|
168
|
+
await subscriptionPromise;
|
|
169
|
+
|
|
170
|
+
mockLogger.info.mockClear();
|
|
171
|
+
|
|
172
|
+
await completeHandshake("test-client-id-2");
|
|
173
|
+
|
|
174
|
+
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
175
|
+
"CometD handshake successful",
|
|
176
|
+
expect.objectContaining({
|
|
177
|
+
clientId: "test-client-id-2",
|
|
178
|
+
connectionType: "websocket",
|
|
179
|
+
}),
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("should automatically resubscribe single channel after reconnection", async () => {
|
|
184
|
+
await vi.runAllTimersAsync();
|
|
185
|
+
|
|
186
|
+
const subscribeCapture = createSubscribeCapture(mockCometD);
|
|
187
|
+
|
|
188
|
+
const channelId = "/test/channel" as TypedReceiveChannelId;
|
|
189
|
+
const onMessage = vi.fn();
|
|
190
|
+
const getSubscriptionRequest = vi.fn(() => ({ param1: "value1" }));
|
|
191
|
+
|
|
192
|
+
const subscriptionPromise = service.subscribe(channelId, onMessage, getSubscriptionRequest);
|
|
193
|
+
|
|
194
|
+
await vi.runAllTimersAsync();
|
|
195
|
+
|
|
196
|
+
subscribeCapture.subscribeCallback?.({
|
|
197
|
+
channel: channelId,
|
|
198
|
+
successful: true,
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
await vi.runAllTimersAsync();
|
|
202
|
+
await subscriptionPromise;
|
|
203
|
+
|
|
204
|
+
mockCometD.resubscribe.mockClear();
|
|
205
|
+
|
|
206
|
+
await completeHandshake("reconnect-client-id");
|
|
207
|
+
|
|
208
|
+
expect(mockCometD.resubscribe).toHaveBeenCalledTimes(1);
|
|
209
|
+
expect(mockCometD.resubscribe).toHaveBeenCalledWith(
|
|
210
|
+
subscribeCapture.handle,
|
|
211
|
+
expect.objectContaining({
|
|
212
|
+
ext: { param1: "value1" },
|
|
213
|
+
}),
|
|
214
|
+
);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("should resubscribe multiple channels after reconnection", async () => {
|
|
218
|
+
await vi.runAllTimersAsync();
|
|
219
|
+
|
|
220
|
+
const capture1 = createSubscribeCapture(mockCometD);
|
|
221
|
+
const capture2 = createSubscribeCapture(mockCometD);
|
|
222
|
+
const capture3 = createSubscribeCapture(mockCometD);
|
|
223
|
+
|
|
224
|
+
const channel1 = "/test/channel1" as TypedReceiveChannelId<object>;
|
|
225
|
+
const channel2 = "/test/channel2" as TypedReceiveChannelId<object>;
|
|
226
|
+
const channel3 = "/test/channel3" as TypedReceiveChannelId<object>;
|
|
227
|
+
|
|
228
|
+
const sub1Promise = service.subscribe(channel1, vi.fn(), () => ({ id: "sub1" }));
|
|
229
|
+
const sub2Promise = service.subscribe(channel2, vi.fn(), () => ({ id: "sub2" }));
|
|
230
|
+
const sub3Promise = service.subscribe(channel3, vi.fn(), () => ({ id: "sub3" }));
|
|
231
|
+
|
|
232
|
+
await vi.runAllTimersAsync();
|
|
233
|
+
|
|
234
|
+
capture1.subscribeCallback?.({ channel: channel1, successful: true });
|
|
235
|
+
capture2.subscribeCallback?.({ channel: channel2, successful: true });
|
|
236
|
+
capture3.subscribeCallback?.({ channel: channel3, successful: true });
|
|
237
|
+
|
|
238
|
+
await vi.runAllTimersAsync();
|
|
239
|
+
await Promise.all([sub1Promise, sub2Promise, sub3Promise]);
|
|
240
|
+
|
|
241
|
+
mockCometD.resubscribe.mockClear();
|
|
242
|
+
|
|
243
|
+
await completeHandshake("reconnect-client-id");
|
|
244
|
+
|
|
245
|
+
expect(mockCometD.resubscribe).toHaveBeenCalledTimes(3);
|
|
246
|
+
expect(mockCometD.resubscribe).toHaveBeenCalledWith(
|
|
247
|
+
capture1.handle,
|
|
248
|
+
expect.objectContaining({ ext: { id: "sub1" } }),
|
|
249
|
+
);
|
|
250
|
+
expect(mockCometD.resubscribe).toHaveBeenCalledWith(
|
|
251
|
+
capture2.handle,
|
|
252
|
+
expect.objectContaining({ ext: { id: "sub2" } }),
|
|
253
|
+
);
|
|
254
|
+
expect(mockCometD.resubscribe).toHaveBeenCalledWith(
|
|
255
|
+
capture3.handle,
|
|
256
|
+
expect.objectContaining({ ext: { id: "sub3" } }),
|
|
257
|
+
);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("should preserve subscription parameters on resubscribe", async () => {
|
|
261
|
+
await vi.runAllTimersAsync();
|
|
262
|
+
|
|
263
|
+
const subscribeCapture = createSubscribeCapture(mockCometD);
|
|
264
|
+
|
|
265
|
+
const channelId = "/document/test-doc/updates" as TypedReceiveChannelId<object>;
|
|
266
|
+
const onMessage = vi.fn();
|
|
267
|
+
const getSubscriptionRequest = vi.fn(() => ({
|
|
268
|
+
clientId: "client-123",
|
|
269
|
+
lastRevisionId: "revision-456",
|
|
270
|
+
}));
|
|
271
|
+
|
|
272
|
+
const subscriptionPromise = service.subscribe(channelId, onMessage, getSubscriptionRequest);
|
|
273
|
+
|
|
274
|
+
await vi.runAllTimersAsync();
|
|
275
|
+
|
|
276
|
+
subscribeCapture.subscribeCallback?.({
|
|
277
|
+
channel: channelId,
|
|
278
|
+
successful: true,
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
await vi.runAllTimersAsync();
|
|
282
|
+
await subscriptionPromise;
|
|
283
|
+
|
|
284
|
+
expect(getSubscriptionRequest).toHaveBeenCalledTimes(1);
|
|
285
|
+
|
|
286
|
+
mockCometD.resubscribe.mockClear();
|
|
287
|
+
getSubscriptionRequest.mockClear();
|
|
288
|
+
|
|
289
|
+
await completeHandshake("reconnect-client-id");
|
|
290
|
+
|
|
291
|
+
expect(getSubscriptionRequest).toHaveBeenCalledTimes(1);
|
|
292
|
+
expect(mockCometD.resubscribe).toHaveBeenCalledWith(
|
|
293
|
+
subscribeCapture.handle,
|
|
294
|
+
expect.objectContaining({
|
|
295
|
+
ext: {
|
|
296
|
+
clientId: "client-123",
|
|
297
|
+
lastRevisionId: "revision-456",
|
|
298
|
+
},
|
|
299
|
+
}),
|
|
300
|
+
);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("should batch resubscriptions together", async () => {
|
|
304
|
+
await vi.runAllTimersAsync();
|
|
305
|
+
|
|
306
|
+
const capture1 = createSubscribeCapture(mockCometD);
|
|
307
|
+
const capture2 = createSubscribeCapture(mockCometD);
|
|
308
|
+
|
|
309
|
+
const sub1Promise = service.subscribe("/channel1" as TypedReceiveChannelId<object>, vi.fn());
|
|
310
|
+
const sub2Promise = service.subscribe("/channel2" as TypedReceiveChannelId<object>, vi.fn());
|
|
311
|
+
|
|
312
|
+
await vi.runAllTimersAsync();
|
|
313
|
+
|
|
314
|
+
capture1.subscribeCallback?.({ channel: "/channel1", successful: true });
|
|
315
|
+
capture2.subscribeCallback?.({ channel: "/channel2", successful: true });
|
|
316
|
+
|
|
317
|
+
await vi.runAllTimersAsync();
|
|
318
|
+
await Promise.all([sub1Promise, sub2Promise]);
|
|
319
|
+
|
|
320
|
+
mockCometD.batch.mockClear();
|
|
321
|
+
|
|
322
|
+
await completeHandshake("reconnect-client-id");
|
|
323
|
+
|
|
324
|
+
expect(mockCometD.batch).toHaveBeenCalledTimes(1);
|
|
325
|
+
expect(mockCometD.batch).toHaveBeenCalledWith(expect.any(Function));
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it("should handle resubscription with no getSubscriptionRequest callback", async () => {
|
|
329
|
+
await vi.runAllTimersAsync();
|
|
330
|
+
|
|
331
|
+
const subscribeCapture = createSubscribeCapture(mockCometD);
|
|
332
|
+
|
|
333
|
+
const channelId = "/test/simple-channel" as TypedReceiveChannelId<object>;
|
|
334
|
+
const subscriptionPromise = service.subscribe(channelId, vi.fn());
|
|
335
|
+
|
|
336
|
+
await vi.runAllTimersAsync();
|
|
337
|
+
|
|
338
|
+
subscribeCapture.subscribeCallback?.({
|
|
339
|
+
channel: channelId,
|
|
340
|
+
successful: true,
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
await vi.runAllTimersAsync();
|
|
344
|
+
await subscriptionPromise;
|
|
345
|
+
|
|
346
|
+
mockCometD.resubscribe.mockClear();
|
|
347
|
+
|
|
348
|
+
await completeHandshake("reconnect-client-id");
|
|
349
|
+
|
|
350
|
+
expect(mockCometD.resubscribe).toHaveBeenCalledTimes(1);
|
|
351
|
+
expect(mockCometD.resubscribe).toHaveBeenCalledWith(
|
|
352
|
+
subscribeCapture.handle,
|
|
353
|
+
expect.objectContaining({
|
|
354
|
+
ext: undefined,
|
|
355
|
+
}),
|
|
356
|
+
);
|
|
357
|
+
});
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
describe("failed reconnection", () => {
|
|
361
|
+
it("should handle failed handshake during reconnection", async () => {
|
|
362
|
+
await vi.runAllTimersAsync();
|
|
363
|
+
|
|
364
|
+
const subscribeCapture = createSubscribeCapture(mockCometD);
|
|
365
|
+
|
|
366
|
+
const subscriptionPromise = service.subscribe(
|
|
367
|
+
"/test/channel" as TypedReceiveChannelId<object>,
|
|
368
|
+
vi.fn(),
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
await vi.runAllTimersAsync();
|
|
372
|
+
|
|
373
|
+
subscribeCapture.subscribeCallback?.({ channel: "/test/channel", successful: true });
|
|
374
|
+
|
|
375
|
+
await vi.runAllTimersAsync();
|
|
376
|
+
await subscriptionPromise;
|
|
377
|
+
|
|
378
|
+
mockCometD.resubscribe.mockClear();
|
|
379
|
+
mockLogger.warn.mockClear();
|
|
380
|
+
|
|
381
|
+
if (handshakeCallback == null) {
|
|
382
|
+
throw new Error("Handshake callback not captured");
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
handshakeCallback({
|
|
386
|
+
channel: "/meta/handshake",
|
|
387
|
+
clientId: "reconnect-client-id",
|
|
388
|
+
error: "Connection failed",
|
|
389
|
+
successful: false,
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
expect(mockLogger.warn).toHaveBeenCalledWith(
|
|
393
|
+
"CometD handshake failed",
|
|
394
|
+
expect.objectContaining({
|
|
395
|
+
clientId: "reconnect-client-id",
|
|
396
|
+
error: "Connection failed",
|
|
397
|
+
}),
|
|
398
|
+
);
|
|
399
|
+
|
|
400
|
+
expect(mockCometD.resubscribe).not.toHaveBeenCalled();
|
|
401
|
+
});
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
describe("subscription lifecycle during reconnection", () => {
|
|
405
|
+
it("should not resubscribe to channels that were unsubscribed before reconnection", async () => {
|
|
406
|
+
await vi.runAllTimersAsync();
|
|
407
|
+
|
|
408
|
+
const capture1 = createSubscribeCapture(mockCometD);
|
|
409
|
+
const capture2 = createSubscribeCapture(mockCometD);
|
|
410
|
+
|
|
411
|
+
createUnsubscribeCapture(mockCometD);
|
|
412
|
+
|
|
413
|
+
const sub1Promise = service.subscribe("/channel1" as TypedReceiveChannelId<object>, vi.fn());
|
|
414
|
+
const sub2Promise = service.subscribe("/channel2" as TypedReceiveChannelId<object>, vi.fn());
|
|
415
|
+
|
|
416
|
+
await vi.runAllTimersAsync();
|
|
417
|
+
|
|
418
|
+
capture1.subscribeCallback?.({ channel: "/channel1", successful: true });
|
|
419
|
+
capture2.subscribeCallback?.({ channel: "/channel2", successful: true });
|
|
420
|
+
|
|
421
|
+
await vi.runAllTimersAsync();
|
|
422
|
+
const sub1Id = await sub1Promise;
|
|
423
|
+
await sub2Promise;
|
|
424
|
+
|
|
425
|
+
service.unsubscribe(sub1Id);
|
|
426
|
+
|
|
427
|
+
mockCometD.resubscribe.mockClear();
|
|
428
|
+
|
|
429
|
+
await completeHandshake("reconnect-client-id");
|
|
430
|
+
|
|
431
|
+
expect(mockCometD.resubscribe).toHaveBeenCalledTimes(1);
|
|
432
|
+
expect(mockCometD.resubscribe).toHaveBeenCalledWith(capture2.handle, expect.anything());
|
|
433
|
+
expect(mockCometD.resubscribe).not.toHaveBeenCalledWith(capture1.handle, expect.anything());
|
|
434
|
+
});
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
interface SubscribeCapture {
|
|
439
|
+
handle: SubscriptionHandle;
|
|
440
|
+
messageCallback: Callback | undefined;
|
|
441
|
+
subscribeCallback: Callback | undefined;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function createSubscribeCapture(mockCometD: MockProxy<CometD>): SubscribeCapture {
|
|
445
|
+
const handle = { id: `handle-${Math.random()}` } as SubscriptionHandle;
|
|
446
|
+
const capture: SubscribeCapture = {
|
|
447
|
+
handle,
|
|
448
|
+
messageCallback: undefined,
|
|
449
|
+
subscribeCallback: undefined,
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
mockCometD.subscribe.mockImplementationOnce(
|
|
453
|
+
(
|
|
454
|
+
_channel: string,
|
|
455
|
+
messageCallback: Callback,
|
|
456
|
+
subscribeCallbackOrProps: Callback | object,
|
|
457
|
+
maybeSubscribeCallback?: Callback,
|
|
458
|
+
) => {
|
|
459
|
+
capture.messageCallback = messageCallback;
|
|
460
|
+
const callback = typeof subscribeCallbackOrProps === "function"
|
|
461
|
+
? subscribeCallbackOrProps
|
|
462
|
+
: maybeSubscribeCallback;
|
|
463
|
+
capture.subscribeCallback = callback as Callback | undefined;
|
|
464
|
+
return handle;
|
|
465
|
+
},
|
|
466
|
+
);
|
|
467
|
+
|
|
468
|
+
mockCometD.resubscribe.mockImplementation((oldHandle: SubscriptionHandle, _props?: object) => {
|
|
469
|
+
return oldHandle;
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
return capture;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
interface UnsubscribeCapture {
|
|
476
|
+
unsubscribeCallback: Callback | undefined;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
function createUnsubscribeCapture(mockCometD: MockProxy<CometD>): UnsubscribeCapture {
|
|
480
|
+
const capture: UnsubscribeCapture = {
|
|
481
|
+
unsubscribeCallback: undefined,
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
mockCometD.unsubscribe.mockImplementation(
|
|
485
|
+
(
|
|
486
|
+
_handle: SubscriptionHandle,
|
|
487
|
+
unsubscribeCallbackOrProps: Callback | object,
|
|
488
|
+
maybeUnsubscribeCallback?: Callback,
|
|
489
|
+
) => {
|
|
490
|
+
const callback = typeof unsubscribeCallbackOrProps === "function"
|
|
491
|
+
? (unsubscribeCallbackOrProps as Callback)
|
|
492
|
+
: maybeUnsubscribeCallback;
|
|
493
|
+
capture.unsubscribeCallback = callback;
|
|
494
|
+
},
|
|
495
|
+
);
|
|
496
|
+
|
|
497
|
+
return capture;
|
|
498
|
+
}
|