Skip to content

Commit

Permalink
improv/rearch-simplify-expand-mue-association: only use Map for assoc…
Browse files Browse the repository at this point in the history
…iated elements to reduce redundancy. Bugfix in unsubscribe.
  • Loading branch information
cjpillsbury committed Sep 24, 2021
1 parent fa06b5d commit 7f30d88
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/js/media-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ class MediaController extends MediaContainer {

associateElement(element) {
if (!element) return;
const { associatedElements: els = [], associatedElementSubscriptions } = this;
if (els.some(elObj => elObj.element === element)) return;
const { associatedElementSubscriptions } = this;
if (associatedElementSubscriptions.has(element)) return;

const registerMediaStateReceiver = this.registerMediaStateReceiver.bind(this);
const unregisterMediaStateReceiver = this.unregisterMediaStateReceiver.bind(this);
Expand All @@ -279,20 +279,16 @@ class MediaController extends MediaContainer {
unregisterMediaStateReceiver,
);

els.push(element);
associatedElementSubscriptions.set(element, unsubscribe);
}

unassociateElement(element) {
if (!element) return;
const { associatedElements: els = [], associatedElementSubscriptions } = this;

const index = els.findIndex(elObj => elObj.element === element);
if (index < 0) return;

const { associatedElementSubscriptions } = this;
if (!associatedElementSubscriptions.has(element)) return;
const unsubscribe = associatedElementSubscriptions.get(element);
unsubscribe();
els.splice(index, 1);
associatedElementSubscriptions.delete(element);
}

registerMediaStateReceiver(el) {
Expand Down Expand Up @@ -557,8 +553,8 @@ const monitorForMediaStateReceivers = (rootNode, registerMediaStateReceiver, unr
// Stop observing for Media State Receivers
observer.disconnect();
// Stop listening for Media State Receiver events.
rootNode.removeEventListener(MediaUIEvents.REGISTER_MEDIA_STATE_RECEIVER, associateElementHandler);
rootNode.removeEventListener(MediaUIEvents.UNREGISTER_MEDIA_STATE_RECEIVER, unassociateElementHandler);
rootNode.removeEventListener(MediaUIEvents.REGISTER_MEDIA_STATE_RECEIVER, registerMediaStateReceiverHandler);
rootNode.removeEventListener(MediaUIEvents.UNREGISTER_MEDIA_STATE_RECEIVER, unregisterMediaStateReceiverHandler);
};

return unsubscribe;
Expand Down

0 comments on commit 7f30d88

Please sign in to comment.