Skip to content

Commit 39a8f53

Browse files
Introduce Abort Initialisation After TearDown (#400)
* add abort utils * add abort signal to bsp and playback strategy * add bsp abort tests * use the existing getError * add playercomponent abort signal tests * remove abortStage * move abortstage constants into models and update error messaging
1 parent 44997c0 commit 39a8f53

File tree

8 files changed

+376
-75
lines changed

8 files changed

+376
-75
lines changed

src/bigscreenplayer.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import ReadyHelper from "./readyhelper"
2222
import Subtitles from "./subtitles/subtitles"
2323
import { ManifestType } from "./models/manifesttypes"
2424
import { Timeline } from "./models/timeline"
25+
import { AbortSignal } from "./utils/abortutils"
26+
import { AbortStages } from "./models/abortstages"
2527

2628
/**
2729
* @import {
@@ -53,6 +55,8 @@ function BigscreenPlayer() {
5355

5456
const END_OF_STREAM_TOLERANCE = 10
5557

58+
const abortSignal = new AbortSignal()
59+
5660
function mediaStateUpdateCallback(evt) {
5761
if (evt.timeUpdate) {
5862
callCallbacks(_callbacks.timeUpdate, {
@@ -106,6 +110,8 @@ function BigscreenPlayer() {
106110
}
107111

108112
function bigscreenPlayerDataLoaded({ media, enableSubtitles, enableAudioDescribed }) {
113+
abortSignal.throwIfAborted(AbortStages.DATA_LOADED)
114+
109115
const initialPresentationTime =
110116
initialPlaybackTime == null ? undefined : convertPlaybackTimeToPresentationTimeInSeconds(initialPlaybackTime)
111117

@@ -120,7 +126,8 @@ function BigscreenPlayer() {
120126
mediaSources,
121127
mediaStateUpdateCallback,
122128
_callbacks.playerError,
123-
callAudioDescribedCallbacks
129+
callAudioDescribedCallbacks,
130+
abortSignal
124131
)
125132

126133
readyHelper = ReadyHelper(
@@ -311,6 +318,8 @@ function BigscreenPlayer() {
311318
* @name tearDown
312319
*/
313320
tearDown() {
321+
abortSignal.abort()
322+
314323
if (subtitles) {
315324
subtitles.tearDown()
316325
subtitles = undefined

src/bigscreenplayer.test.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import MediaState from "./models/mediastate"
1212
import PauseTriggers from "./models/pausetriggers"
1313
import { Timeline } from "./models/timeline"
1414
import getError, { NoErrorThrownError } from "./testutils/geterror"
15+
import { AbortStages } from "./models/abortstages"
1516

1617
let bigscreenPlayer
1718
let bigscreenPlayerData
@@ -194,7 +195,8 @@ describe("Bigscreen Player", () => {
194195
expect.any(Object),
195196
expect.any(Function),
196197
expect.any(Function),
197-
expect.any(Function)
198+
expect.any(Function),
199+
expect.objectContaining({ aborted: false })
198200
)
199201
})
200202

@@ -234,7 +236,8 @@ describe("Bigscreen Player", () => {
234236
expect.any(Object),
235237
expect.any(Function),
236238
expect.any(Function),
237-
expect.any(Function)
239+
expect.any(Function),
240+
expect.objectContaining({ aborted: false })
238241
)
239242
})
240243

@@ -253,7 +256,8 @@ describe("Bigscreen Player", () => {
253256
expect.any(Object),
254257
expect.any(Function),
255258
expect.any(Function),
256-
expect.any(Function)
259+
expect.any(Function),
260+
expect.objectContaining({ aborted: false })
257261
)
258262
})
259263

@@ -273,7 +277,8 @@ describe("Bigscreen Player", () => {
273277
expect.any(Object),
274278
expect.any(Function),
275279
expect.any(Function),
276-
expect.any(Function)
280+
expect.any(Function),
281+
expect.objectContaining({ aborted: false })
277282
)
278283
})
279284

@@ -297,7 +302,8 @@ describe("Bigscreen Player", () => {
297302
expect.any(Object),
298303
expect.any(Function),
299304
expect.any(Function),
300-
expect.any(Function)
305+
expect.any(Function),
306+
expect.objectContaining({ aborted: false })
301307
)
302308
})
303309

@@ -321,7 +327,8 @@ describe("Bigscreen Player", () => {
321327
expect.any(Object),
322328
expect.any(Function),
323329
expect.any(Function),
324-
expect.any(Function)
330+
expect.any(Function),
331+
expect.objectContaining({ aborted: false })
325332
)
326333
})
327334

@@ -346,10 +353,27 @@ describe("Bigscreen Player", () => {
346353
expect.any(Object),
347354
expect.any(Function),
348355
expect.any(Function),
349-
expect.any(Function)
356+
expect.any(Function),
357+
expect.objectContaining({ aborted: false })
350358
)
351359
})
352360
})
361+
362+
describe("aborting during initialization", () => {
363+
it("aborts if bigscreen player has been torn down", async () => {
364+
bigscreenPlayer.tearDown()
365+
const error = await getError(() => asyncInitialiseBigscreenPlayer(createPlaybackElement(), bigscreenPlayerData))
366+
367+
expect(error).toHaveProperty("name", "AbortError")
368+
expect(error).toHaveProperty("message", `bigscreen-player aborted at ${AbortStages.DATA_LOADED}`)
369+
})
370+
371+
it("does not abort if bigscreen player has not been torn down", async () => {
372+
const error = await getError(() => asyncInitialiseBigscreenPlayer(createPlaybackElement(), bigscreenPlayerData))
373+
374+
expect(error).toBeInstanceOf(NoErrorThrownError)
375+
})
376+
})
353377
})
354378

355379
describe("tearDown", () => {

src/models/abortstages.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const AbortStages = {
2+
DATA_LOADED: "bigscreen-player-data-loaded",
3+
PLAYER_COMPONENT: "bigscreen-player-player-component",
4+
STRATEGY: "bigscreen-player-strategy",
5+
} as const
6+
7+
export type AbortStages = (typeof AbortStages)[keyof typeof AbortStages]

src/models/mediakinds.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const AUDIO = "audio" as const
2-
const VIDEO = "video" as const
1+
const AUDIO = "audio"
2+
const VIDEO = "video"
33

44
export const MediaKinds = { AUDIO, VIDEO } as const
55

src/playercomponent.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import PluginData from "./plugindata"
99
import PluginEnums from "./pluginenums"
1010
import Plugins from "./plugins"
1111
import DebugTool from "./debugger/debugtool"
12+
import { AbortStages } from "./models/abortstages"
1213

1314
/**
1415
* @import { InitData } from './types.d.ts'
@@ -41,7 +42,8 @@ function PlayerComponent(
4142
mediaSources,
4243
stateUpdateCallback,
4344
errorCallback,
44-
audioDescribedCallback
45+
audioDescribedCallback,
46+
abortSignal
4547
) {
4648
let setSubtitlesState
4749
let _stateUpdateCallback = stateUpdateCallback
@@ -57,10 +59,13 @@ function PlayerComponent(
5759

5860
StrategyPicker()
5961
.then((strategy) => {
62+
abortSignal.throwIfAborted(AbortStages.PLAYER_COMPONENT)
63+
6064
playbackStrategy = strategy(
6165
mediaSources,
6266
mediaKind,
6367
playbackElement,
68+
abortSignal,
6469
bigscreenPlayerData.media.isUHD,
6570
bigscreenPlayerData.media.playerSettings,
6671
{

0 commit comments

Comments
 (0)