Skip to content

Commit

Permalink
Simplify FakeEvent and FakeEventTarget
Browse files Browse the repository at this point in the history
We now avoid tricky things like CustomEvent and setting properties on
native Events.  This gives us better cross-browser compatibility and
less complexity.

Change-Id: Idc9fcc69c33257e4540d956bcbc949de6d992cf0
  • Loading branch information
joeyparrish committed Feb 29, 2016
1 parent 303ddae commit 9d70cad
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 257 deletions.
1 change: 0 additions & 1 deletion build/types/polyfill
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Polyfills used to emulate missing browsers features.

+../../lib/polyfill/customevent.js
+../../lib/polyfill/fullscreen.js
+../../lib/polyfill/mediakeys.js
+../../lib/polyfill/patchedmediakeys_20140218.js
Expand Down
68 changes: 0 additions & 68 deletions lib/polyfill/customevent.js

This file was deleted.

10 changes: 4 additions & 6 deletions lib/polyfill/patchedmediakeys_20140218.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ shaka.polyfill.PatchedMediaKeys.v20140218.MediaKeys.prototype.
shaka.polyfill.PatchedMediaKeys.v20140218.
MediaKeySession = function(nativeMediaKeys, sessionType) {
shaka.log.debug('v20140218.MediaKeySession');
shaka.util.FakeEventTarget.call(this, null);
shaka.util.FakeEventTarget.call(this);

// Native MediaKeySession, which will be created in generateRequest
/** @private {MSMediaKeySession} */
Expand Down Expand Up @@ -520,8 +520,7 @@ shaka.polyfill.PatchedMediaKeys.v20140218.onMsNeedKey_ = function(event) {
// Alias
var v20140218 = shaka.polyfill.PatchedMediaKeys.v20140218;

var event2 = shaka.util.FakeEvent.create({
type: 'encrypted',
var event2 = new shaka.util.FakeEvent('encrypted', {
initDataType: 'cenc',
initData: v20140218.NormaliseInitData_(event.initData)
});
Expand Down Expand Up @@ -615,8 +614,7 @@ shaka.polyfill.PatchedMediaKeys.v20140218.MediaKeySession.prototype.

var isNew = this.keyStatuses.getStatus() == undefined;

var event2 = shaka.util.FakeEvent.create({
type: 'message',
var event2 = new shaka.util.FakeEvent('message', {
messageType: isNew ? 'licenserequest' : 'licenserenewal',
message: event.message.buffer
});
Expand Down Expand Up @@ -711,7 +709,7 @@ shaka.polyfill.PatchedMediaKeys.v20140218.MediaKeySession.prototype.
shaka.polyfill.PatchedMediaKeys.v20140218.MediaKeySession.prototype.
updateKeyStatus_ = function(status) {
this.keyStatuses.setStatus(status);
var event = shaka.util.FakeEvent.create({type: 'keystatuseschange'});
var event = new shaka.util.FakeEvent('keystatuseschange');
this.dispatchEvent(event);
};

Expand Down
10 changes: 4 additions & 6 deletions lib/polyfill/patchedmediakeys_v01b.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,7 @@ shaka.polyfill.PatchedMediaKeys.v01b.MediaKeys.prototype.onWebkitNeedKey_ =
shaka.log.debug('v01b.onWebkitNeedKey_', event);
shaka.asserts.assert(this.media_, 'media_ not set in onWebkitNeedKey_');

var event2 = shaka.util.FakeEvent.create({
type: 'encrypted',
var event2 = new shaka.util.FakeEvent('encrypted', {
initDataType: 'webm', // not used by v0.1b EME, but given a valid value
initData: event.initData
});
Expand All @@ -430,8 +429,7 @@ shaka.polyfill.PatchedMediaKeys.v01b.MediaKeys.prototype.onWebkitKeyMessage_ =

var isNew = session.keyStatuses.getStatus() == undefined;

var event2 = shaka.util.FakeEvent.create({
type: 'message',
var event2 = new shaka.util.FakeEvent('message', {
messageType: isNew ? 'licenserequest' : 'licenserenewal',
message: event.message
});
Expand Down Expand Up @@ -514,7 +512,7 @@ shaka.polyfill.PatchedMediaKeys.v01b.MediaKeys.prototype.findSession_ =
shaka.polyfill.PatchedMediaKeys.v01b.MediaKeySession =
function(media, keySystem, sessionType) {
shaka.log.debug('v01b.MediaKeySession');
shaka.util.FakeEventTarget.call(this, null);
shaka.util.FakeEventTarget.call(this);

/** @private {!HTMLMediaElement} */
this.media_ = media;
Expand Down Expand Up @@ -788,7 +786,7 @@ shaka.polyfill.PatchedMediaKeys.v01b.MediaKeySession.prototype.update_ =
shaka.polyfill.PatchedMediaKeys.v01b.MediaKeySession.prototype.
updateKeyStatus_ = function(status) {
this.keyStatuses.setStatus(status);
var event = shaka.util.FakeEvent.create({type: 'keystatuseschange'});
var event = new shaka.util.FakeEvent('keystatuseschange');
this.dispatchEvent(event);
};

Expand Down
96 changes: 73 additions & 23 deletions lib/util/fake_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,85 @@

goog.provide('shaka.util.FakeEvent');

goog.require('shaka.asserts');


/**
* @namespace shaka.util.FakeEvent
* @summary A utility to simplify the creation of fake events.
* Create an Event work-alike object based on the dictionary.
* The event should contain all of the same properties from the dict.
*
* @param {string} type
* @param {Object=} opt_dict
* @constructor
* @extends {Event}
*/
shaka.util.FakeEvent = function(type, opt_dict) {
// Take properties from dict if present.
var dict = opt_dict || {};
for (var key in dict) {
this[key] = dict[key];
}


// These Properties below cannot be set by dict. They are all provided for
// compatibility with native events.

/** @const {boolean} */
this.bubbles = false;

/** @const {boolean} */
this.cancelable = false;

/** @const {boolean} */
this.defaultPrevented = false;

/**
* According to MDN, Chrome uses high-res timers instead of epoch time.
* Follow suit so that timeStamps on FakeEvents use the same base as
* on native Events.
* @const {number}
* @see https://developer.mozilla.org/en-US/docs/Web/API/Event/timeStamp
*/
this.timeStamp = window.performance ? window.performance.now() : Date.now();

/** @const {string} */
this.type = type;

/** @const {boolean} */
this.isTrusted = false;

/** @type {EventTarget} */
this.currentTarget = null;

/** @type {EventTarget} */
this.target = null;


/**
* Non-standard property read by FakeEventTarget to stop processing listeners.
* @type {boolean}
*/
this.stopped = false;
};


/**
* Does nothing, since FakeEvents have no default. Provided for compatibility
* with native Events.
*/
shaka.util.FakeEvent.prototype.preventDefault = function() {};


/**
* Return an Event object based on the dictionary.
* The event should contain all of the same properties from the dict.
* @param {!Object} dict
* @return {!Event}
* Stops processing event listeners for this event. Provided for compatibility
* with native Events.
*/
shaka.util.FakeEvent.create = function(dict) {
var event = new CustomEvent(dict.type, {
detail: dict.detail,
bubbles: !!dict.bubbles
});
// NOTE: In strict mode, we can't overwrite existing properties, so we only
// set properties on "event" which don't exist yet. If a property does exist
// on "event", we assert that it has the correct value already.
for (var key in dict) {
if (key in event) {
shaka.asserts.assert(event[key] == dict[key], 'key = ' + key);
} else {
event[key] = dict[key];
}
}
return event;
shaka.util.FakeEvent.prototype.stopImmediatePropagation = function() {
this.stopped = true;
};


/**
* Does nothing, since FakeEvents do not bubble. Provided for compatibility
* with native Events.
*/
shaka.util.FakeEvent.prototype.stopPropagation = function() {};
Loading

0 comments on commit 9d70cad

Please sign in to comment.