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: clear event binding function references #38

Merged
merged 6 commits into from
Dec 12, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions src/dash-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
* @private
*/
_shaka: any;
/**
* an object containing all the events we bind and unbind to.
* @member {Object} - _adapterEventsBindings
* @type {Object}
* @private
*/
_adapterEventsBindings: Object = {};
Copy link
Contributor

Choose a reason for hiding this comment

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

flow type {[name: string]: function}

/**
* The load promise
* @member {Promise<Object>} - _loadPromise
Expand Down Expand Up @@ -174,6 +181,7 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
constructor(videoElement: HTMLVideoElement, source: Object, config: Object = {}) {
DashAdapter._logger.debug('Creating adapter. Shaka version: ' + shaka.Player.version);
super(videoElement, source, config);
this._createAdapterFunctionBindings();
}

/**
Expand Down Expand Up @@ -202,19 +210,29 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
}
}


Copy link
Contributor

Choose a reason for hiding this comment

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

remove redundant lines (!!!)

_createAdapterFunctionBindings(): void{

Copy link
Contributor

Choose a reason for hiding this comment

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

why empty line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there is no spoon (empty line).

this._adapterEventsBindings['error'] = this._onError.bind(this);
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use arrow function?

Copy link
Contributor

Choose a reason for hiding this comment

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

also, need to clear these bounded methods on destroy or we will have memory leaks possibly

this._adapterEventsBindings['adaption'] = this._onAdaptation.bind(this);
this._adapterEventsBindings['buffering'] = this._onBuffering.bind(this);
this._adapterEventsBindings['waiting'] = this._onWaiting.bind(this);
this._adapterEventsBindings['playing'] = this._onPlaying.bind(this);
}

/**
* Add the required bindings to shaka.
* @function _addBindings
* @private
* @returns {void}
*/
_addBindings(): void {
this._shaka.addEventListener('adaptation', this._onAdaptation.bind(this));
this._shaka.addEventListener('error', this._onError.bind(this));
this._shaka.addEventListener('buffering', this._onBuffering.bind(this));
this._shaka.addEventListener('adaptation', this._adapterEventsBindings.adaption);
this._shaka.addEventListener('error', this._adapterEventsBindings.error);
Copy link
Contributor

Choose a reason for hiding this comment

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

Create local ENUM for Shaka event names

this._shaka.addEventListener('buffering', this._adapterEventsBindings.buffering);
//TODO use events enum when available
this._videoElement.addEventListener('waiting', this._onWaiting.bind(this));
this._videoElement.addEventListener('playing', this._onPlaying.bind(this));
this._videoElement.addEventListener('waiting', this._adapterEventsBindings.waiting);
Copy link
Contributor

Choose a reason for hiding this comment

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

Use ENUM for HTML5 events

this._videoElement.addEventListener('playing', this._adapterEventsBindings.playing);
}

/**
Expand All @@ -224,12 +242,12 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
* @returns {void}
*/
_removeBindings(): void {
this._shaka.removeEventListener('adaptation', this._onAdaptation);
this._shaka.removeEventListener('error', this._onError.bind(this));
this._shaka.removeEventListener('buffering', this._onBuffering.bind(this));
this._shaka.removeEventListener('adaptation', this._adapterEventsBindings.adaption);
this._shaka.removeEventListener('error', this._adapterEventsBindings.error);
this._shaka.removeEventListener('buffering', this._adapterEventsBindings.buffering);
//TODO use events enum when available
this._videoElement.removeEventListener('waiting', this._onWaiting.bind(this));
this._videoElement.removeEventListener('playing', this._onPlaying.bind(this));
this._videoElement.removeEventListener('waiting', this._adapterEventsBindings.waiting);
this._videoElement.removeEventListener('playing', this._adapterEventsBindings.playing);
}

/**
Expand Down