consolidated-events
Advanced tools
Comparing version 2.0.0 to 2.0.2
@@ -0,1 +1,5 @@ | ||
## v2.0.1 | ||
- Move TargetEventHandlers methods to prototype ([#15](https://github.com/lencioni/consolidated-events/pull/15)) | ||
## v2.0.0 | ||
@@ -2,0 +6,0 @@ |
@@ -99,23 +99,27 @@ var CAN_USE_DOM = !!(typeof window !== 'undefined' && window.document && window.document.createElement); | ||
function TargetEventHandlers(target) { | ||
var _this = this; | ||
this.target = target; | ||
this.events = {}; | ||
} | ||
this.getEventHandlers = function (eventName, options) { | ||
TargetEventHandlers.prototype.getEventHandlers = function () { | ||
function getEventHandlers(eventName, options) { | ||
var key = String(eventName) + ' ' + String(eventOptionsKey(options)); | ||
if (!_this.events[key]) { | ||
_this.events[key] = { | ||
if (!this.events[key]) { | ||
this.events[key] = { | ||
handlers: [], | ||
handleEvent: undefined | ||
}; | ||
_this.events[key].nextHandlers = _this.events[key].handlers; | ||
this.events[key].nextHandlers = this.events[key].handlers; | ||
} | ||
return _this.events[key]; | ||
}; | ||
return this.events[key]; | ||
} | ||
this.handleEvent = function (eventName, options, event) { | ||
var eventHandlers = _this.getEventHandlers(eventName, options); | ||
return getEventHandlers; | ||
}(); | ||
TargetEventHandlers.prototype.handleEvent = function () { | ||
function handleEvent(eventName, options, event) { | ||
var eventHandlers = this.getEventHandlers(eventName, options); | ||
eventHandlers.handlers = eventHandlers.nextHandlers; | ||
@@ -131,7 +135,13 @@ eventHandlers.handlers.forEach(function (handler) { | ||
}); | ||
}; | ||
} | ||
this.add = function (eventName, listener, options) { | ||
return handleEvent; | ||
}(); | ||
TargetEventHandlers.prototype.add = function () { | ||
function add(eventName, listener, options) { | ||
var _this = this; | ||
// options has already been normalized at this point. | ||
var eventHandlers = _this.getEventHandlers(eventName, options); | ||
var eventHandlers = this.getEventHandlers(eventName, options); | ||
@@ -141,5 +151,5 @@ ensureCanMutateNextEventHandlers(eventHandlers); | ||
if (eventHandlers.nextHandlers.length === 0) { | ||
eventHandlers.handleEvent = _this.handleEvent.bind(_this, eventName, options); | ||
eventHandlers.handleEvent = this.handleEvent.bind(this, eventName, options); | ||
_this.target.addEventListener(eventName, eventHandlers.handleEvent, options); | ||
this.target.addEventListener(eventName, eventHandlers.handleEvent, options); | ||
} | ||
@@ -150,33 +160,39 @@ | ||
var isSubscribed = true; | ||
var unsubscribe = function unsubscribe() { | ||
if (!isSubscribed) { | ||
return; | ||
} | ||
var unsubscribe = function () { | ||
function unsubscribe() { | ||
if (!isSubscribed) { | ||
return; | ||
} | ||
isSubscribed = false; | ||
isSubscribed = false; | ||
ensureCanMutateNextEventHandlers(eventHandlers); | ||
var index = eventHandlers.nextHandlers.indexOf(listener); | ||
eventHandlers.nextHandlers.splice(index, 1); | ||
ensureCanMutateNextEventHandlers(eventHandlers); | ||
var index = eventHandlers.nextHandlers.indexOf(listener); | ||
eventHandlers.nextHandlers.splice(index, 1); | ||
if (eventHandlers.nextHandlers.length === 0) { | ||
// All event handlers have been removed, so we want to remove the event | ||
// listener from the target node. | ||
if (eventHandlers.nextHandlers.length === 0) { | ||
// All event handlers have been removed, so we want to remove the event | ||
// listener from the target node. | ||
if (_this.target) { | ||
// There can be a race condition where the target may no longer exist | ||
// when this function is called, e.g. when a React component is | ||
// unmounting. Guarding against this prevents the following error: | ||
// | ||
// Cannot read property 'removeEventListener' of undefined | ||
_this.target.removeEventListener(eventName, eventHandlers.handleEvent, options); | ||
if (_this.target) { | ||
// There can be a race condition where the target may no longer exist | ||
// when this function is called, e.g. when a React component is | ||
// unmounting. Guarding against this prevents the following error: | ||
// | ||
// Cannot read property 'removeEventListener' of undefined | ||
_this.target.removeEventListener(eventName, eventHandlers.handleEvent, options); | ||
} | ||
eventHandlers.handleEvent = undefined; | ||
} | ||
} | ||
eventHandlers.handleEvent = undefined; | ||
} | ||
}; | ||
return unsubscribe; | ||
}(); | ||
return unsubscribe; | ||
}; | ||
} | ||
} | ||
return add; | ||
}(); | ||
var EVENT_HANDLERS_KEY = '__consolidated_events_handlers__'; | ||
@@ -183,0 +199,0 @@ |
@@ -103,23 +103,27 @@ 'use strict'; | ||
function TargetEventHandlers(target) { | ||
var _this = this; | ||
this.target = target; | ||
this.events = {}; | ||
} | ||
this.getEventHandlers = function (eventName, options) { | ||
TargetEventHandlers.prototype.getEventHandlers = function () { | ||
function getEventHandlers(eventName, options) { | ||
var key = String(eventName) + ' ' + String(eventOptionsKey(options)); | ||
if (!_this.events[key]) { | ||
_this.events[key] = { | ||
if (!this.events[key]) { | ||
this.events[key] = { | ||
handlers: [], | ||
handleEvent: undefined | ||
}; | ||
_this.events[key].nextHandlers = _this.events[key].handlers; | ||
this.events[key].nextHandlers = this.events[key].handlers; | ||
} | ||
return _this.events[key]; | ||
}; | ||
return this.events[key]; | ||
} | ||
this.handleEvent = function (eventName, options, event) { | ||
var eventHandlers = _this.getEventHandlers(eventName, options); | ||
return getEventHandlers; | ||
}(); | ||
TargetEventHandlers.prototype.handleEvent = function () { | ||
function handleEvent(eventName, options, event) { | ||
var eventHandlers = this.getEventHandlers(eventName, options); | ||
eventHandlers.handlers = eventHandlers.nextHandlers; | ||
@@ -135,7 +139,13 @@ eventHandlers.handlers.forEach(function (handler) { | ||
}); | ||
}; | ||
} | ||
this.add = function (eventName, listener, options) { | ||
return handleEvent; | ||
}(); | ||
TargetEventHandlers.prototype.add = function () { | ||
function add(eventName, listener, options) { | ||
var _this = this; | ||
// options has already been normalized at this point. | ||
var eventHandlers = _this.getEventHandlers(eventName, options); | ||
var eventHandlers = this.getEventHandlers(eventName, options); | ||
@@ -145,5 +155,5 @@ ensureCanMutateNextEventHandlers(eventHandlers); | ||
if (eventHandlers.nextHandlers.length === 0) { | ||
eventHandlers.handleEvent = _this.handleEvent.bind(_this, eventName, options); | ||
eventHandlers.handleEvent = this.handleEvent.bind(this, eventName, options); | ||
_this.target.addEventListener(eventName, eventHandlers.handleEvent, options); | ||
this.target.addEventListener(eventName, eventHandlers.handleEvent, options); | ||
} | ||
@@ -154,33 +164,39 @@ | ||
var isSubscribed = true; | ||
var unsubscribe = function unsubscribe() { | ||
if (!isSubscribed) { | ||
return; | ||
} | ||
var unsubscribe = function () { | ||
function unsubscribe() { | ||
if (!isSubscribed) { | ||
return; | ||
} | ||
isSubscribed = false; | ||
isSubscribed = false; | ||
ensureCanMutateNextEventHandlers(eventHandlers); | ||
var index = eventHandlers.nextHandlers.indexOf(listener); | ||
eventHandlers.nextHandlers.splice(index, 1); | ||
ensureCanMutateNextEventHandlers(eventHandlers); | ||
var index = eventHandlers.nextHandlers.indexOf(listener); | ||
eventHandlers.nextHandlers.splice(index, 1); | ||
if (eventHandlers.nextHandlers.length === 0) { | ||
// All event handlers have been removed, so we want to remove the event | ||
// listener from the target node. | ||
if (eventHandlers.nextHandlers.length === 0) { | ||
// All event handlers have been removed, so we want to remove the event | ||
// listener from the target node. | ||
if (_this.target) { | ||
// There can be a race condition where the target may no longer exist | ||
// when this function is called, e.g. when a React component is | ||
// unmounting. Guarding against this prevents the following error: | ||
// | ||
// Cannot read property 'removeEventListener' of undefined | ||
_this.target.removeEventListener(eventName, eventHandlers.handleEvent, options); | ||
if (_this.target) { | ||
// There can be a race condition where the target may no longer exist | ||
// when this function is called, e.g. when a React component is | ||
// unmounting. Guarding against this prevents the following error: | ||
// | ||
// Cannot read property 'removeEventListener' of undefined | ||
_this.target.removeEventListener(eventName, eventHandlers.handleEvent, options); | ||
} | ||
eventHandlers.handleEvent = undefined; | ||
} | ||
} | ||
eventHandlers.handleEvent = undefined; | ||
} | ||
}; | ||
return unsubscribe; | ||
}(); | ||
return unsubscribe; | ||
}; | ||
} | ||
} | ||
return add; | ||
}(); | ||
var EVENT_HANDLERS_KEY = '__consolidated_events_handlers__'; | ||
@@ -187,0 +203,0 @@ |
{ | ||
"name": "consolidated-events", | ||
"version": "2.0.0", | ||
"version": "2.0.2", | ||
"description": "Manage multiple event handlers using few event listeners", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -13,83 +13,83 @@ import eventOptionsKey from './eventOptionsKey'; | ||
this.events = {}; | ||
} | ||
this.getEventHandlers = (eventName, options) => { | ||
const key = `${eventName} ${eventOptionsKey(options)}`; | ||
TargetEventHandlers.prototype.getEventHandlers = function getEventHandlers(eventName, options) { | ||
const key = `${eventName} ${eventOptionsKey(options)}`; | ||
if (!this.events[key]) { | ||
this.events[key] = { | ||
handlers: [], | ||
handleEvent: undefined, | ||
}; | ||
this.events[key].nextHandlers = this.events[key].handlers; | ||
if (!this.events[key]) { | ||
this.events[key] = { | ||
handlers: [], | ||
handleEvent: undefined, | ||
}; | ||
this.events[key].nextHandlers = this.events[key].handlers; | ||
} | ||
return this.events[key]; | ||
}; | ||
TargetEventHandlers.prototype.handleEvent = function handleEvent(eventName, options, event) { | ||
const eventHandlers = this.getEventHandlers(eventName, options); | ||
eventHandlers.handlers = eventHandlers.nextHandlers; | ||
eventHandlers.handlers.forEach((handler) => { | ||
if (handler) { | ||
// We need to check for presence here because a handler function may | ||
// cause later handlers to get removed. This can happen if you for | ||
// instance have a waypoint that unmounts another waypoint as part of an | ||
// onEnter/onLeave handler. | ||
handler(event); | ||
} | ||
}); | ||
}; | ||
return this.events[key]; | ||
}; | ||
TargetEventHandlers.prototype.add = function add(eventName, listener, options) { | ||
// options has already been normalized at this point. | ||
const eventHandlers = this.getEventHandlers(eventName, options); | ||
this.handleEvent = (eventName, options, event) => { | ||
const eventHandlers = this.getEventHandlers(eventName, options); | ||
eventHandlers.handlers = eventHandlers.nextHandlers; | ||
eventHandlers.handlers.forEach((handler) => { | ||
if (handler) { | ||
// We need to check for presence here because a handler function may | ||
// cause later handlers to get removed. This can happen if you for | ||
// instance have a waypoint that unmounts another waypoint as part of an | ||
// onEnter/onLeave handler. | ||
handler(event); | ||
} | ||
}); | ||
}; | ||
ensureCanMutateNextEventHandlers(eventHandlers); | ||
this.add = (eventName, listener, options) => { | ||
// options has already been normalized at this point. | ||
const eventHandlers = this.getEventHandlers(eventName, options); | ||
if (eventHandlers.nextHandlers.length === 0) { | ||
eventHandlers.handleEvent = this.handleEvent.bind(this, eventName, options); | ||
ensureCanMutateNextEventHandlers(eventHandlers); | ||
this.target.addEventListener( | ||
eventName, | ||
eventHandlers.handleEvent, | ||
options, | ||
); | ||
} | ||
if (eventHandlers.nextHandlers.length === 0) { | ||
eventHandlers.handleEvent = this.handleEvent.bind(this, eventName, options); | ||
eventHandlers.nextHandlers.push(listener); | ||
this.target.addEventListener( | ||
eventName, | ||
eventHandlers.handleEvent, | ||
options, | ||
); | ||
let isSubscribed = true; | ||
const unsubscribe = () => { | ||
if (!isSubscribed) { | ||
return; | ||
} | ||
eventHandlers.nextHandlers.push(listener); | ||
isSubscribed = false; | ||
let isSubscribed = true; | ||
const unsubscribe = () => { | ||
if (!isSubscribed) { | ||
return; | ||
} | ||
ensureCanMutateNextEventHandlers(eventHandlers); | ||
const index = eventHandlers.nextHandlers.indexOf(listener); | ||
eventHandlers.nextHandlers.splice(index, 1); | ||
isSubscribed = false; | ||
if (eventHandlers.nextHandlers.length === 0) { | ||
// All event handlers have been removed, so we want to remove the event | ||
// listener from the target node. | ||
ensureCanMutateNextEventHandlers(eventHandlers); | ||
const index = eventHandlers.nextHandlers.indexOf(listener); | ||
eventHandlers.nextHandlers.splice(index, 1); | ||
if (this.target) { | ||
// There can be a race condition where the target may no longer exist | ||
// when this function is called, e.g. when a React component is | ||
// unmounting. Guarding against this prevents the following error: | ||
// | ||
// Cannot read property 'removeEventListener' of undefined | ||
this.target.removeEventListener( | ||
eventName, | ||
eventHandlers.handleEvent, | ||
options, | ||
); | ||
} | ||
if (eventHandlers.nextHandlers.length === 0) { | ||
// All event handlers have been removed, so we want to remove the event | ||
// listener from the target node. | ||
if (this.target) { | ||
// There can be a race condition where the target may no longer exist | ||
// when this function is called, e.g. when a React component is | ||
// unmounting. Guarding against this prevents the following error: | ||
// | ||
// Cannot read property 'removeEventListener' of undefined | ||
this.target.removeEventListener( | ||
eventName, | ||
eventHandlers.handleEvent, | ||
options, | ||
); | ||
} | ||
eventHandlers.handleEvent = undefined; | ||
} | ||
}; | ||
return unsubscribe; | ||
eventHandlers.handleEvent = undefined; | ||
} | ||
}; | ||
} | ||
return unsubscribe; | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40512
895