Skip to content

Commit

Permalink
fix: stopLoad() after destroy() becomes no-op, startLoad() after dest…
Browse files Browse the repository at this point in the history
…roy() throws nicer error message.
  • Loading branch information
jwalton committed Apr 1, 2021
1 parent f20e155 commit 0c32415
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/hls.ts
Expand Up @@ -40,7 +40,7 @@ export default class Hls implements HlsEventEmitter {
public readonly userConfig: Partial<HlsConfig>;

private coreComponents: ComponentAPI[];
private networkControllers: NetworkComponentAPI[];
private networkControllers: NetworkComponentAPI[] | undefined;

private _emitter: HlsEventEmitter = new EventEmitter();
private _autoLevelCapping: number;
Expand Down Expand Up @@ -284,8 +284,7 @@ export default class Hls implements HlsEventEmitter {
this.url = null;
if (this.networkControllers) {
this.networkControllers.forEach((component) => component.destroy());
// @ts-ignore
this.networkControllers = null;
this.networkControllers = undefined;
}
if (this.coreComponents) {
this.coreComponents.forEach((component) => component.destroy());
Expand Down Expand Up @@ -342,6 +341,9 @@ export default class Hls implements HlsEventEmitter {
*/
startLoad(startPosition: number = -1) {
logger.log(`startLoad(${startPosition})`);
if (!this.networkControllers) {
throw new Error('Cannot call `startLoad()` on destroyed instance of hls');
}
this.networkControllers.forEach((controller) => {
controller.startLoad(startPosition);
});
Expand All @@ -352,9 +354,11 @@ export default class Hls implements HlsEventEmitter {
*/
stopLoad() {
logger.log('stopLoad');
this.networkControllers.forEach((controller) => {
controller.stopLoad();
});
if (this.networkControllers) {
this.networkControllers.forEach((controller) => {
controller.stopLoad();
});
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/hls.js
@@ -1,5 +1,6 @@
import Hls from '../../src/hls';
import { hlsDefaultConfig } from '../../src/config';
import { expect } from 'chai';

describe('Hls', function () {
describe('bandwidthEstimate', function () {
Expand All @@ -21,4 +22,20 @@ describe('Hls', function () {
);
});
});

describe('destroy', function () {
it('should allow stopLoad() after destroy()', function () {
const hls = new Hls();
hls.destroy();
expect(() => hls.stopLoad()).to.not.throw();
});

it('should not allow startLoad() after destroy()', function () {
const hls = new Hls();
hls.destroy();
expect(() => hls.startLoad()).to.throw(
'Cannot call `startLoad()` on destroyed instance of hls'
);
});
});
});

0 comments on commit 0c32415

Please sign in to comment.