Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stopLoad() after destroy() becomes no-op, startLoad() after destroy() throws nicer error message. #3710

Merged
merged 2 commits into from Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we just did this?

Suggested change
this.networkControllers = undefined;
this.networkControllers.length = 0;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a much simpler fix. :) I made the same change to coreComponents below, too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

}
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'
);
});
});
});