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 5 commits
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
49 changes: 37 additions & 12 deletions src/dash-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ import {Error} from 'playkit-js'
import Widevine from './drm/widevine'
import PlayReady from './drm/playready'

type ShakaEventType = { [event: string]: string };

/**
* Shaka events enum
* @type {Object}
* @const
*/
const ShakaEvent: ShakaEventType = {
ERROR: 'error',
ADAPTION: 'adaption',
BUFFERING: 'buffering'
};

/**
* Adapter of shaka lib for dash content
* @classdesc
Expand Down Expand Up @@ -53,6 +66,19 @@ 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: { [name: string]: Function } = {
[ShakaEvent.ERROR]: (event) => this._onError(event),
[ShakaEvent.ADAPTION] : () => this._onAdaptation(),
[ShakaEvent.BUFFERING] : (event) => this._onBuffering(event),
[BaseMediaSourceAdapter.Html5Events.WAITING] : () => this._onWaiting(),
[BaseMediaSourceAdapter.Html5Events.PLAYING] : () => this._onPlaying()
};
/**
* The load promise
* @member {Promise<Object>} - _loadPromise
Expand Down Expand Up @@ -209,12 +235,11 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
* @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));
//TODO use events enum when available
this._videoElement.addEventListener('waiting', this._onWaiting.bind(this));
this._videoElement.addEventListener('playing', this._onPlaying.bind(this));
this._shaka.addEventListener(ShakaEvent.ADAPTION, this._adapterEventsBindings.adaption);
this._shaka.addEventListener(ShakaEvent.ERROR, this._adapterEventsBindings.error);
this._shaka.addEventListener(ShakaEvent.BUFFERING, this._adapterEventsBindings.buffering);
this._videoElement.addEventListener(BaseMediaSourceAdapter.Html5Events.WAITING, this._adapterEventsBindings.waiting);
this._videoElement.addEventListener(BaseMediaSourceAdapter.Html5Events.PLAYING, this._adapterEventsBindings.playing);
}

/**
Expand All @@ -224,12 +249,11 @@ 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));
//TODO use events enum when available
this._videoElement.removeEventListener('waiting', this._onWaiting.bind(this));
this._videoElement.removeEventListener('playing', this._onPlaying.bind(this));
this._shaka.removeEventListener(ShakaEvent.ADAPTION, this._adapterEventsBindings.adaption);
this._shaka.removeEventListener(ShakaEvent.ERROR, this._adapterEventsBindings.error);
this._shaka.removeEventListener(ShakaEvent.BUFFERING, this._adapterEventsBindings.buffering);
this._videoElement.removeEventListener(BaseMediaSourceAdapter.Html5Events.WAITING, this._adapterEventsBindings.waiting);
this._videoElement.removeEventListener(BaseMediaSourceAdapter.Html5Events.PLAYING, this._adapterEventsBindings.playing);
}

/**
Expand Down Expand Up @@ -282,6 +306,7 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
this._removeBindings();
return this._shaka.destroy();
}
this._adapterEventsBindings = {};
});
}

Expand Down