Skip to content

Commit

Permalink
refactor: extract factory function to create peer connections
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisguttandin committed Nov 25, 2023
1 parent 0eb6be2 commit ee76b98
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/factories/rtc-peer-connection-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const createRTCPeerConnectionFactory = (window: null | (Window & typeof globalThis)) => () => {
if (window === null) {
throw new Error('A native EventTarget could not be created.');
}

return new window.RTCPeerConnection({
iceCandidatePoolSize: 1,
iceServers: [{ urls: ['stun:stun.l.google.com:19302', 'stun:stun1.l.google.com:19302'] }]
});
};
11 changes: 3 additions & 8 deletions src/factories/timing-provider-constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ import {
TSendPeerToPeerMessageFunction,
TTimingProviderConstructor
} from '../types';
import type { createRTCPeerConnectionFactory } from './rtc-peer-connection-factory';
import type { createSignalingFactory } from './signaling-factory';
import type { createSortByHopsAndRoundTripTime } from './sort-by-hops-and-round-trip-time';

const SUENC_URL = 'wss://matchmaker.suenc.io';
const PROVIDER_ID_REGEX = /^[\dA-Za-z]{20}$/;

export const createTimingProviderConstructor = (
createRTCPeerConnection: ReturnType<typeof createRTCPeerConnectionFactory>,
createSignaling: ReturnType<typeof createSignalingFactory>,
eventTargetConstructor: TEventTargetConstructor,
performance: Window['performance'],
Expand Down Expand Up @@ -299,14 +301,7 @@ export const createTimingProviderConstructor = (
return of(event);
}),
demultiplexMessages(() => this._clientId, timer(10_000)),
negotiateDataChannels(
() =>
new RTCPeerConnection({
iceCandidatePoolSize: 1,
iceServers: [{ urls: ['stun:stun.l.google.com:19302', 'stun:stun1.l.google.com:19302'] }]
}),
sendSignalingEvent
)
negotiateDataChannels(createRTCPeerConnection, sendSignalingEvent)
);
})
).pipe(
Expand Down
5 changes: 4 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createEventTargetConstructor } from './factories/event-target-constructor';
import { createEventTargetFactory } from './factories/event-target-factory';
import { createRTCPeerConnectionFactory } from './factories/rtc-peer-connection-factory';
import { createSignalingFactory } from './factories/signaling-factory';
import { createSortByHopsAndRoundTripTime } from './factories/sort-by-hops-and-round-trip-time';
import { createTimingProviderConstructor } from './factories/timing-provider-constructor';
Expand All @@ -14,9 +15,11 @@ import { TTimingProviderConstructor } from './types';
*/
export * from './types/index';

const window = createWindow();
const timingProviderConstructor: TTimingProviderConstructor = createTimingProviderConstructor(
createRTCPeerConnectionFactory(window),
createSignalingFactory((url) => new WebSocket(url)),
createEventTargetConstructor(createEventTargetFactory(createWindow()), wrapEventListener),
createEventTargetConstructor(createEventTargetFactory(window), wrapEventListener),
performance,
setTimeout,
createSortByHopsAndRoundTripTime<[unknown, { hops: number[] }, number]>(
Expand Down

0 comments on commit ee76b98

Please sign in to comment.