From cc3ad3bf4c0780dfddc4f1a55c56fc3f76a23b4a Mon Sep 17 00:00:00 2001 From: Gary Katsevman Date: Mon, 28 Oct 2019 16:35:26 -0400 Subject: [PATCH 1/5] fix: fallback to a simple WeakMap sham If we are running on a browser that doesn't support WeakMap, use a simple WeakMap sham as a fallback. --- src/js/utils/dom-data.js | 58 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/src/js/utils/dom-data.js b/src/js/utils/dom-data.js index 43cf1a915e..d5c0b76cf8 100644 --- a/src/js/utils/dom-data.js +++ b/src/js/utils/dom-data.js @@ -3,6 +3,62 @@ * @module dom-data */ +import log from './log.js'; +import * as Guid from './guid.js'; +import window from 'global/window'; + +let FakeWeakMap; + +if (!WeakMap) { + FakeWeakMap = class { + constructor() { + this.vdata = 'vdata' + Math.floor(window.performance && window.performance.now() || Date.now()); + this.data = {}; + } + + set(key, value) { + const access = key[this.vdata] || Guid.newGUID(); + + if (!key[this.vdata]) { + key[this.vdata] = access; + } + + this.data[access] = value; + + return this; + } + + get(key) { + const access = key[this.vdata]; + + // we have data, return it + if (access) { + return this.data[access]; + } + + // we don't have data, return nothing. + // return undefined explicitly as that's the contract for this method + log('We have no data for this element', key); + return undefined; + } + + has(key) { + const access = key[this.vdata]; + + return access in this.data; + } + + delete(key) { + const access = key[this.vdata]; + + if (access) { + delete this.data[access]; + delete key[this.vdata]; + } + } + }; +} + /** * Element Data Store. * @@ -13,4 +69,4 @@ * @type {Object} * @private */ -export default new WeakMap(); +export default WeakMap ? new WeakMap() : new FakeWeakMap(); From 4ef0e1c602f0f36f496b6f8aec5393fdc7c78416 Mon Sep 17 00:00:00 2001 From: Gary Katsevman Date: Tue, 29 Oct 2019 11:51:57 -0400 Subject: [PATCH 2/5] check for weakmap properly --- src/js/utils/dom-data.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/js/utils/dom-data.js b/src/js/utils/dom-data.js index d5c0b76cf8..cd077ebfa8 100644 --- a/src/js/utils/dom-data.js +++ b/src/js/utils/dom-data.js @@ -9,7 +9,7 @@ import window from 'global/window'; let FakeWeakMap; -if (!WeakMap) { +if (!window.WeakMap) { FakeWeakMap = class { constructor() { this.vdata = 'vdata' + Math.floor(window.performance && window.performance.now() || Date.now()); @@ -69,4 +69,4 @@ if (!WeakMap) { * @type {Object} * @private */ -export default WeakMap ? new WeakMap() : new FakeWeakMap(); +export default window.WeakMap ? new WeakMap() : new FakeWeakMap(); From 4b23d70a65e755c6e8ab624f6689641112f1e846 Mon Sep 17 00:00:00 2001 From: Gary Katsevman Date: Tue, 29 Oct 2019 11:53:18 -0400 Subject: [PATCH 3/5] Use a SetSham if no Set --- src/js/component.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/js/component.js b/src/js/component.js index 7bf0ae6f63..33624600a6 100644 --- a/src/js/component.js +++ b/src/js/component.js @@ -98,9 +98,33 @@ class Component { this.childIndex_ = {}; this.childNameIndex_ = {}; - this.setTimeoutIds_ = new Set(); - this.setIntervalIds_ = new Set(); - this.rafIds_ = new Set(); + let SetSham; + + if (!window.Set) { + SetSham = class { + constructor() { + this.set_ = {}; + } + has(key) { + return key in this.set_; + } + delete(key) { + const has = this.has(key); + + delete this.set_[key]; + + return has; + } + add(key) { + this.set_[key] = 1; + return this; + } + }; + } + + this.setTimeoutIds_ = window.Set ? new Set() : new SetSham(); + this.setIntervalIds_ = window.Set ? new Set() : new SetSham(); + this.rafIds_ = window.Set ? new Set() : new SetSham(); this.clearingTimersOnDispose_ = false; // Add any child components in options From cf75e27856f840a5805fe3c9f6729e04549f82ee Mon Sep 17 00:00:00 2001 From: Gary Katsevman Date: Tue, 29 Oct 2019 11:54:08 -0400 Subject: [PATCH 4/5] ignore some props in element copy --- src/js/player.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/js/player.js b/src/js/player.js index 5241209046..19856ae278 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -655,7 +655,11 @@ class Player extends Component { // `src` or `controls` that were set via js before the player // was initialized. Object.keys(el).forEach((k) => { - tag[k] = el[k]; + try { + tag[k] = el[k]; + } catch (e) { + // we got a a property like outerHTML which we can't actually copy, ignore it + } }); } From 8dbadf34044a719e9024aac7908ea5779419d5f8 Mon Sep 17 00:00:00 2001 From: Gary Katsevman Date: Wed, 30 Oct 2019 17:32:37 -0400 Subject: [PATCH 5/5] add forEach method to SetSham --- src/js/component.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/js/component.js b/src/js/component.js index 33624600a6..da83f6fa94 100644 --- a/src/js/component.js +++ b/src/js/component.js @@ -119,6 +119,11 @@ class Component { this.set_[key] = 1; return this; } + forEach(callback, thisArg) { + for (const key in this.set_) { + callback.call(thisArg, key, key, this); + } + } }; }