Skip to content

Commit e2ed340

Browse files
authored
feat: add calculateDelay function to media controller (#609)
* feat: add calculateDelay function to media controller * test: fix test * ci: downgrade @types/node version
1 parent 05d604d commit e2ed340

File tree

6 files changed

+5788
-4565
lines changed

6 files changed

+5788
-4565
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"@biomejs/biome": "^1.7.3",
3030
"@changesets/changelog-github": "^0.5.0",
3131
"@changesets/cli": "^2.27.3",
32-
"@types/node": "22.3.0",
32+
"@types/node": "^20.12.12",
3333
"@vitest/coverage-c8": "^0.33.0",
3434
"@vitest/coverage-v8": "^2.0.5",
3535
"@vitest/ui": "^2.0.5",

packages/core-web/src/broadcast.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ it("should expose correct exports", () => {
77
[
88
"addBroadcastEventListeners",
99
"createBroadcastStore",
10+
"createSilentAudioTrack",
1011
"getBroadcastDeviceInfo",
1112
]
1213
`);

packages/core-web/src/media/controls/controller.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ const addEffectsToStore = (
366366
autoPlay: __initialProps.autoPlay,
367367
backoff: __initialProps.backoff,
368368
backoffMax: __initialProps.backoffMax,
369+
calculateDelay: __initialProps.calculateDelay,
369370
errorCount,
370371
hlsConfig: __controls.hlsConfig,
371372
mounted,
@@ -379,6 +380,7 @@ const addEffectsToStore = (
379380
autoPlay,
380381
backoff,
381382
backoffMax,
383+
calculateDelay,
382384
errorCount,
383385
hlsConfig,
384386
mounted,
@@ -393,10 +395,9 @@ const addEffectsToStore = (
393395

394396
await cleanupSource?.();
395397

396-
if (errorCount > 0) {
397-
const delayTime = Math.min(backoff * 2 ** (errorCount - 1), backoffMax);
398-
await delay(delayTime);
399-
}
398+
await delay(
399+
Math.max(calculateDelay(errorCount), errorCount === 0 ? 0 : 100),
400+
);
400401

401402
let unmounted = false;
402403

packages/core/src/media/controller.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ export type InitialProps = {
6363
*/
6464
backoffMax: number;
6565

66+
/**
67+
* A function that calculates the delay for the backoff.
68+
*
69+
* This is used to calculate the delay for the backoff.
70+
*/
71+
calculateDelay: (attempt: number) => number;
72+
6673
/**
6774
* The length of the clip. This is usually used alongside `ClipTrigger`. Specifies the duration of the media clip, in seconds.
6875
*
@@ -653,7 +660,20 @@ export const createControllerStore = ({
653660
aspectRatio: initialProps?.aspectRatio ?? null,
654661
autoPlay: initialProps.autoPlay ?? false,
655662
backoff: Math.max(initialProps.backoff ?? 500, 100),
656-
backoffMax: Math.max(initialProps.backoffMax ?? 30000, 1000),
663+
backoffMax: Math.max(initialProps.backoffMax ?? 30000, 10000),
664+
calculateDelay:
665+
initialProps.calculateDelay ??
666+
((count) => {
667+
if (count === 0) {
668+
return 0;
669+
}
670+
671+
const delayTime = Math.min(
672+
Math.max(initialProps.backoff ?? 500, 100) * 2 ** (count - 1),
673+
Math.max(initialProps.backoffMax ?? 30000, 10000),
674+
);
675+
return delayTime;
676+
}),
657677
clipLength: initialProps.clipLength ?? null,
658678
cacheWebRTCFailureMs: initialProps.cacheWebRTCFailureMs ?? null,
659679
hotkeys: initialProps?.hotkeys ?? true,

packages/core/src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const core = "@livepeer/[email protected]";
2-
const react = "@livepeer/[email protected].2";
2+
const react = "@livepeer/[email protected].3";
33

44
export const version = {
55
core,

0 commit comments

Comments
 (0)