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

feat: Put custom event definitions in class #7640

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions build/types/core
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
+../../lib/deprecate/enforcer.js
+../../lib/deprecate/version.js

+../../lib/event/error_event.js

+../../lib/media/adaptation_set.js
+../../lib/media/adaptation_set_criteria.js
+../../lib/media/buffering_observer.js
Expand Down
5 changes: 2 additions & 3 deletions lib/cast/cast_proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ goog.require('shaka.Player');
goog.require('shaka.cast.CastSender');
goog.require('shaka.cast.CastUtils');
goog.require('shaka.log');
goog.require('shaka.Player.ErrorEvent');
goog.require('shaka.util.Error');
goog.require('shaka.util.EventManager');
goog.require('shaka.util.FakeEvent');
Expand Down Expand Up @@ -553,9 +554,7 @@ shaka.cast.CastProxy = class extends shaka.util.FakeEventTarget {
// Pass any errors through to the app.
goog.asserts.assert(error instanceof shaka.util.Error,
'Wrong error type!');
const eventType = shaka.util.FakeEvent.EventName.Error;
const data = (new Map()).set('detail', error);
const event = new shaka.util.FakeEvent(eventType, data);
const event = new shaka.Player.ErrorEvent(error);
this.localPlayer_.dispatchEvent(event);
});
}
Expand Down
5 changes: 2 additions & 3 deletions lib/cast/cast_receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ goog.require('goog.asserts');
goog.require('shaka.Player');
goog.require('shaka.cast.CastUtils');
goog.require('shaka.log');
goog.require('shaka.Player.ErrorEvent');
goog.require('shaka.util.Error');
goog.require('shaka.util.EventManager');
goog.require('shaka.util.FakeEvent');
Expand Down Expand Up @@ -395,9 +396,7 @@ shaka.cast.CastReceiver = class extends shaka.util.FakeEventTarget {
// Pass any errors through to the app.
goog.asserts.assert(error instanceof shaka.util.Error,
'Wrong error type! Error: ' + error);
const eventType = shaka.util.FakeEvent.EventName.Error;
const data = (new Map()).set('detail', error);
const event = new shaka.util.FakeEvent(eventType, data);
const event = new shaka.Player.ErrorEvent(error);
// Only dispatch the event if the player still exists.
if (this.player_) {
this.player_.dispatchEvent(event);
Expand Down
39 changes: 39 additions & 0 deletions lib/event/error_event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

goog.provide('shaka.Player.ErrorEvent');

goog.require('shaka.util.Error');
goog.require('shaka.util.FakeEvent');


/**
* @event shaka.Player.ErrorEvent
* @description Fired when a playback error occurs.
* @property {string} type
* 'error'
* @property {!shaka.util.Error} detail
* An object which contains details on the error. The error's
* <code>category</code> and <code>code</code> properties will identify the
* specific error that occurred. In an uncompiled build, you can also use the
* <code>message</code> and <code>stack</code> properties to debug.
* @extends {shaka.util.FakeEvent}
* @exportDoc
Copy link
Member

Choose a reason for hiding this comment

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

Not sure, but should this be @export so it can be called by apps?

Copy link
Member Author

Choose a reason for hiding this comment

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

If I change to @export the documentation is not generated correctly.... I don't know why...

*/
shaka.Player.ErrorEvent = class extends shaka.util.FakeEvent {
/**
* @param {shaka.util.Error} error
*/
constructor(error) {
/** @type {!shaka.util.FakeEvent.EventName} */
const name = shaka.util.FakeEvent.EventName.Error;

/** @type {Map.<string, Object>} */
const data = new Map().set('detail', error);

super(name, data);
}
};
4 changes: 2 additions & 2 deletions lib/media/preload_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ goog.require('shaka.media.QualityObserver');
goog.require('shaka.util.StreamUtils');
goog.require('shaka.media.StreamingEngine');
goog.require('shaka.media.SegmentPrefetch');
goog.require('shaka.Player.ErrorEvent');
goog.require('shaka.util.ConfigUtils');
goog.require('shaka.util.FakeEvent');
goog.require('shaka.util.FakeEventTarget');
Expand Down Expand Up @@ -436,8 +437,7 @@ shaka.media.PreloadManager = class extends shaka.util.FakeEventTarget {
this.destroy();
}

const eventName = shaka.util.FakeEvent.EventName.Error;
const event = this.makeEvent_(eventName, (new Map()).set('detail', error));
const event = new shaka.Player.ErrorEvent(error);
this.dispatchEvent(event);
if (event.defaultPrevented) {
error.handled = true;
Expand Down
18 changes: 2 additions & 16 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ goog.require('shaka.media.StreamingEngine');
goog.require('shaka.media.TimeRangesUtils');
goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.net.NetworkingUtils');
goog.require('shaka.Player.ErrorEvent');
Copy link
Member

Choose a reason for hiding this comment

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

I think the errors are caused by ordering and namespaces here. goog.require here will assign the ErrorEvent class to shaka.Player.ErrorEvent. Later in this file, we assign shaka.Player = class ..., overwriting what was assigned to ErrorEvent by replacing the whole shaka.Player namespace.

So we might need to do something like this:

  1. Rename shaka.Player.ErrorEvent to shaka.errors.ErrorEvent, giving errors their own namespace
  2. For backward compatibility, assign shaka.Player.ErrorEvent = shaka.errors.ErrorEvent after the definition of shaka.Player
  3. Actually, no need for backward compatibility. That class never existed at runtime, and was also never in our externs. It only ever existed as a doc, so it's fine to rename it AFAICT. But the new name should be exported so it's in the generated externs/defs, where other projects can refer to it as a strong type.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, but I used shaka.events.ErrorEvent instead

goog.require('shaka.text.SimpleTextDisplayer');
goog.require('shaka.text.StubTextDisplayer');
goog.require('shaka.text.TextEngine');
Expand Down Expand Up @@ -69,19 +70,6 @@ goog.require('shaka.lcevc.Dec');
goog.requireType('shaka.media.PresentationTimeline');


/**
* @event shaka.Player.ErrorEvent
* @description Fired when a playback error occurs.
* @property {string} type
* 'error'
* @property {!shaka.util.Error} detail
* An object which contains details on the error. The error's
* <code>category</code> and <code>code</code> properties will identify the
* specific error that occurred. In an uncompiled build, you can also use the
* <code>message</code> and <code>stack</code> properties to debug.
* @exportDoc
*/

/**
* @event shaka.Player.StateChangeEvent
* @description Fired when the player changes load states.
Expand Down Expand Up @@ -7737,9 +7725,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
this.restoreDisabledVariants_(/* updateAbrManager= */ false);
}

const eventName = shaka.util.FakeEvent.EventName.Error;
const event = shaka.Player.makeEvent_(
eventName, (new Map()).set('detail', error));
const event = new shaka.Player.ErrorEvent(error);
this.dispatchEvent(event);
if (event.defaultPrevented) {
error.handled = true;
Expand Down
Loading