Skip to content

Commit

Permalink
fix: cancel endlessly gathering connections
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisguttandin committed Nov 13, 2023
1 parent aae401e commit 15312ac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/functions/create-backoff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const createBackoff = (base: number) =>
<const>[
() => base ** 2,
() => {
// tslint:disable-next-line:no-parameter-reassignment
base += 1;
}
];
20 changes: 20 additions & 0 deletions src/operators/negotiate-data-channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ import {
finalize,
from,
ignoreElements,
iif,
interval,
merge,
mergeMap,
of,
retry,
switchMap,
take,
takeUntil,
tap,
throwError,
timer,
zip
} from 'rxjs';
import { inexorably } from 'rxjs-etc/operators';
import { on } from 'subscribable-things';
import { createBackoff } from '../functions/create-backoff';
import { IErrorEvent, IPingEvent, IPongEvent, IUpdateEvent } from '../interfaces';
import { TDataChannelTuple, TIncomingNegotiationEvent, TOutgoingSignalingEvent, TSendPeerToPeerMessageFunction } from '../types';
import { echo } from './echo';
Expand Down Expand Up @@ -138,8 +142,24 @@ export const negotiateDataChannels =

return () => unsubscribeFunctions.forEach((unsubscribeFunction) => unsubscribeFunction());
};
const [getBackoff, incrementBackoff] = createBackoff(1);
const subscribeToPeerConnection = () => {
const subscription = merge(on(peerConnection, 'icecandidate'), on(peerConnection, 'icegatheringstatechange'))
.pipe(
switchMap(() =>
iif(
() => peerConnection.iceGatheringState === 'gathering',
defer(() => timer(10_000 * getBackoff())),
EMPTY
)
)
)
.subscribe(() => {
incrementBackoff();
errorSubject.next(new Error('RTCPeerConnection seems to be stuck at iceGatheringState "gathering".'));
});
const unsubscribeFunctions = [
() => subscription.unsubscribe(),
on(
peerConnection,
'connectionstatechange'
Expand Down

0 comments on commit 15312ac

Please sign in to comment.