Skip to content

Commit

Permalink
feat(Synchronizer): Change Scope of Event handlers (#1291)
Browse files Browse the repository at this point in the history
Set fireEvent and onEvent functions from anonymous inner functions to object keys, to allow
consumers to monkeypatch the functions of need be.
  • Loading branch information
mix3d authored Aug 10, 2020
1 parent 84d518e commit 15120f0
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/synchronization/Synchronizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function Synchronizer(event, handler) {
* @param {Object} eventData - The data object for the source event
* @returns {void}
*/
function fireEvent(sourceElement, eventData) {
this.fireEvent = function(sourceElement, eventData) {
const isDisabled = !that.enabled;
const noElements = !sourceElements.length || !targetElements.length;

Expand Down Expand Up @@ -199,7 +199,7 @@ function Synchronizer(event, handler) {
);
});
ignoreFiredEvents = false;
}
};

/**
* Call fireEvent if not ignoring events, and pass along event data
Expand All @@ -208,15 +208,15 @@ function Synchronizer(event, handler) {
* @param {Event} e - The source event object
* @returns {void}
*/
function onEvent(e) {
this.onEvent = function(e) {
const eventData = e.detail;

if (ignoreFiredEvents === true) {
return;
}

fireEvent(e.currentTarget, eventData);
}
that.fireEvent(e.currentTarget, eventData);
};

/**
* Add a source element to this synchronizer
Expand All @@ -237,7 +237,7 @@ function Synchronizer(event, handler) {

// Subscribe to the event
event.split(' ').forEach(oneEvent => {
element.addEventListener(oneEvent, onEvent);
element.addEventListener(oneEvent, that.onEvent);
});

// Update the initial distances between elements
Expand Down Expand Up @@ -302,14 +302,14 @@ function Synchronizer(event, handler) {

// Stop listening for the event
event.split(' ').forEach(oneEvent => {
element.removeEventListener(oneEvent, onEvent);
element.removeEventListener(oneEvent, that.onEvent);
});

// Update the initial distances between elements
that.getDistances();

// Update everyone listening for events
fireEvent(element);
that.fireEvent(element);
that.updateDisableHandlers();
};

Expand Down

0 comments on commit 15120f0

Please sign in to comment.