Skip to content

Commit

Permalink
wip: dependency injection relaxed types
Browse files Browse the repository at this point in the history
  • Loading branch information
amydevs committed Aug 13, 2024
1 parent a4243c9 commit 230e491
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/WebSocketStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type {
StreamCodeToReason,
StreamReasonToCode,
} from './types.js';
import type WebSocketConnection from './WebSocketConnection.js';
import type { StreamId, StreamMessage, VarInt } from './message/index.js';
import type { Evented } from '@matrixai/events';
import type {
ReadableWritablePair,
WritableStreamDefaultController,
Expand Down Expand Up @@ -102,7 +102,7 @@ class WebSocketStream implements ReadableWritablePair<Uint8Array, Uint8Array> {
public readonly writable: WritableStream<Uint8Array>;

protected logger: Logger;
protected connection: WebSocketConnection;
protected connection: Evented & { meta: () => ConnectionMetadata };
protected reasonToCode: StreamReasonToCode;
protected codeToReason: StreamCodeToReason;
protected readableController: ReadableStreamDefaultController;
Expand Down Expand Up @@ -199,7 +199,7 @@ class WebSocketStream implements ReadableWritablePair<Uint8Array, Uint8Array> {
}: {
initiated: 'local' | 'peer';
streamId: StreamId;
connection: WebSocketConnection;
connection: Evented & { meta: () => ConnectionMetadata };
bufferSize: number;
reasonToCode?: StreamReasonToCode;
codeToReason?: StreamCodeToReason;
Expand Down
17 changes: 10 additions & 7 deletions tests/WebSocketStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ const logger2 = new Logger('stream 2', LogLevel.WARN, [

let streamIdCounter = 0n;

function createMockedWebSocketConnection(
streamOptions: StreamOptions = {},
): WebSocketConnection {
function createMockedWebSocketConnection(streamOptions: StreamOptions = {}) {
const instance = new EventTarget() as EventTarget & {
peerConnection: WebSocketConnection | undefined;
connectTo: (connection: WebSocketConnection) => void;
Expand Down Expand Up @@ -121,22 +119,27 @@ function createMockedWebSocketConnection(
}
await stream.streamRecv(remainder);
};
return instance as unknown as WebSocketConnection;
return instance;
}

describe(WebSocketStream.name, () => {
async function createConnectionPair(
streamOptions: StreamOptions = {},
): Promise<[WebSocketConnection, WebSocketConnection]> {
): Promise<
[
ReturnType<typeof createMockedWebSocketConnection>,
ReturnType<typeof createMockedWebSocketConnection>,
]
> {
const connection1 = createMockedWebSocketConnection(streamOptions);
const connection2 = createMockedWebSocketConnection(streamOptions);
(connection1 as any).connectTo(connection2);
return [connection1, connection2];
}

async function createStreamPairFrom(
connection1: WebSocketConnection,
connection2: WebSocketConnection,
connection1: ReturnType<typeof createMockedWebSocketConnection>,
connection2: ReturnType<typeof createMockedWebSocketConnection>,
): Promise<[WebSocketStream, WebSocketStream]> {
const stream1 = await connection1.newStream();
const createStream2Prom = utils.promise<WebSocketStream>();
Expand Down

0 comments on commit 230e491

Please sign in to comment.