@rest-vir/define-service 0.2.0 → 0.3.0
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.
|
@@ -78,7 +78,7 @@ export declare function createMockClientWebSocketConstructor<const WebSocketToCo
|
|
|
78
78
|
* @package [`@rest-vir/define-service`](https://www.npmjs.com/package/@rest-vir/define-service)
|
|
79
79
|
*/
|
|
80
80
|
export type MockClientWebSocketListeners = Partial<{
|
|
81
|
-
[EventName in keyof CommonWebSocketEventMap]: Set<(event: CommonWebSocketEventMap[EventName]) => void
|
|
81
|
+
[EventName in keyof CommonWebSocketEventMap]: Set<(event: CommonWebSocketEventMap[EventName]) => MaybePromise<void>>;
|
|
82
82
|
}>;
|
|
83
83
|
/**
|
|
84
84
|
* A mock WebSocket constructor for connections from the client side. This can be passed to
|
|
@@ -165,14 +165,14 @@ export declare class MockClientWebSocket<const WebSocketToConnect extends WebSoc
|
|
|
165
165
|
*
|
|
166
166
|
* @see https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener
|
|
167
167
|
*/
|
|
168
|
-
addEventListener<const EventName extends keyof CommonWebSocketEventMap>(eventName: EventName, listener: (event: CommonWebSocketEventMap[EventName]) => void): void;
|
|
168
|
+
addEventListener<const EventName extends keyof CommonWebSocketEventMap>(eventName: EventName, listener: (event: CommonWebSocketEventMap[EventName]) => MaybePromise<void>): void;
|
|
169
169
|
/**
|
|
170
170
|
* Implements and mocks the standard `WebSocket.removeEventListener` property but with message
|
|
171
171
|
* type safety.
|
|
172
172
|
*
|
|
173
173
|
* @see https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener
|
|
174
174
|
*/
|
|
175
|
-
removeEventListener<const EventName extends keyof CommonWebSocketEventMap>(eventName: EventName, listener: (event: CommonWebSocketEventMap[EventName]) => void): void;
|
|
175
|
+
removeEventListener<const EventName extends keyof CommonWebSocketEventMap>(eventName: EventName, listener: (event: CommonWebSocketEventMap[EventName]) => MaybePromise<void>): void;
|
|
176
176
|
/**
|
|
177
177
|
* Implements and mocks the standard `WebSocket.send` property but with message type safety.
|
|
178
178
|
*
|
|
@@ -139,11 +139,13 @@ export class MockClientWebSocket {
|
|
|
139
139
|
* methods instead (which appropriately dispatch their events).
|
|
140
140
|
*/
|
|
141
141
|
dispatchEvent(eventName, event) {
|
|
142
|
-
this.listeners[eventName]?.forEach((listener) =>
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
142
|
+
this.listeners[eventName]?.forEach((listener) => {
|
|
143
|
+
void listener({
|
|
144
|
+
...event,
|
|
145
|
+
target: this,
|
|
146
|
+
type: eventName,
|
|
147
|
+
});
|
|
148
|
+
});
|
|
147
149
|
}
|
|
148
150
|
/**
|
|
149
151
|
* Implements and mocks the standard `WebSocket.addEventListener` property but with message type
|
|
@@ -187,7 +189,7 @@ export class MockClientWebSocket {
|
|
|
187
189
|
return;
|
|
188
190
|
}
|
|
189
191
|
this.dispatchEvent('message', {
|
|
190
|
-
data,
|
|
192
|
+
data: JSON.stringify(data),
|
|
191
193
|
});
|
|
192
194
|
void this.sendCallback?.({
|
|
193
195
|
messageData: data,
|
|
@@ -69,6 +69,13 @@ export type SendAndWaitForReplyParams<Location extends WebSocketLocation, WebSoc
|
|
|
69
69
|
* @default {seconds: 10}
|
|
70
70
|
*/
|
|
71
71
|
timeout?: Readonly<AnyDuration> | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* An optional function to check if the current reply is the one you were waiting for.
|
|
74
|
+
*
|
|
75
|
+
* If this is set, `sendAndWaitForReply` will wait until a reply is received that matches this
|
|
76
|
+
* condition. If this not set, the first reply is used.
|
|
77
|
+
*/
|
|
78
|
+
replyCheck?: (messageFromHost: WebSocketToConnect extends NoParam ? any : Exclude<WebSocketToConnect, NoParam>['MessageFromHostType']) => MaybePromise<boolean>;
|
|
72
79
|
};
|
|
73
80
|
/**
|
|
74
81
|
* Collapsed version of {@link SendAndWaitForReplyParams} for the `sendAndWaitForReply` method that
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { waitUntil } from '@augment-vir/assert';
|
|
2
|
-
import { callAsynchronously, DeferredPromise, ensureErrorAndPrependMessage, getOrSet, stringify, } from '@augment-vir/common';
|
|
2
|
+
import { callAsynchronously, DeferredPromise, ensureErrorAndPrependMessage, getOrSet, stringify, wrapInTry, } from '@augment-vir/common';
|
|
3
3
|
import { convertDuration } from 'date-vir';
|
|
4
4
|
import { assertValidShape } from 'object-shape-tester';
|
|
5
5
|
import { parseJsonWithUndefined } from '../augments/json.js';
|
|
@@ -103,11 +103,16 @@ export function overwriteWebSocketMethods(webSocketDefinition, rawWebSocket, web
|
|
|
103
103
|
originalRemoveEventListener.call(webSocket, eventName, existing);
|
|
104
104
|
}
|
|
105
105
|
},
|
|
106
|
-
async sendAndWaitForReply({ message, timeout = { seconds: 10 }, } = {}) {
|
|
106
|
+
async sendAndWaitForReply({ message, timeout = { seconds: 10 }, replyCheck, } = {}) {
|
|
107
107
|
const deferredReply = new DeferredPromise();
|
|
108
|
-
function listener({ message, }) {
|
|
108
|
+
async function listener({ message, }) {
|
|
109
109
|
if (!deferredReply.isSettled) {
|
|
110
|
-
|
|
110
|
+
const matchesChecker = replyCheck
|
|
111
|
+
? (await wrapInTry(() => replyCheck(message))) === true
|
|
112
|
+
: true;
|
|
113
|
+
if (matchesChecker) {
|
|
114
|
+
deferredReply.resolve(message);
|
|
115
|
+
}
|
|
111
116
|
}
|
|
112
117
|
}
|
|
113
118
|
setTimeout(() => {
|