From aa8250516bb17832b6bf5c5f07b36d872d28d2e6 Mon Sep 17 00:00:00 2001 From: ciaranj Date: Thu, 27 Jun 2013 12:05:02 +0100 Subject: [PATCH] Avoids in-advertant bloating of internal _handlers array during unbind Removes extraneous empty object literals sitting within the internal _handlers array when an unbind occurs. Current approach does mean a new array is constructed for each unbind that happens, do we think this is acceptable? Signed-off-by: ciaranj --- keymaster.js | 8 +++++--- keymaster.min.js | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/keymaster.js b/keymaster.js index 947712e..2a842cf 100644 --- a/keymaster.js +++ b/keymaster.js @@ -180,14 +180,16 @@ if (!_handlers[key]) { return; } + var newHandlers = []; for (i in _handlers[key]) { obj = _handlers[key][i]; // only clear handlers if correct scope and mods match - if (obj.scope === scope && compareArray(obj.mods, mods)) { - _handlers[key][i] = {}; + if (!(obj.scope === scope && compareArray(obj.mods, mods))) { + newHandlers.push( _handlers[key][i] ); } } - }; + _handlers[key] = newHandlers; + } // Returns true if the key with code 'keyCode' is currently down // Converts strings into key codes. diff --git a/keymaster.min.js b/keymaster.min.js index 24fcaab..8b3085a 100644 --- a/keymaster.min.js +++ b/keymaster.min.js @@ -1,4 +1,4 @@ // keymaster.js // (c) 2011-2012 Thomas Fuchs // keymaster.js may be freely distributed under the MIT license. -(function(e){function f(e,t){var n=e.length;while(n--)if(e[n]===t)return n;return-1}function l(e,t){if(e.length!=t.length)return!1;for(var n=0;n0;for(u in r)if(!r[u]&&f(o.mods,+u)>-1||r[u]&&f(o.mods,+u)==-1)c=!1;(o.mods.length==0&&!r[16]&&!r[18]&&!r[17]&&!r[91]||c)&&o.method(e,o)===!1&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}}function d(e){var t=e.keyCode,n,i=f(a,t);i>=0&&a.splice(i,1);if(t==93||t==224)t=91;if(t in r){r[t]=!1;for(n in s)s[n]==t&&(m[n]=!1)}}function v(){for(t in r)r[t]=!1;for(t in s)m[t]=!1}function m(e,t,r){var i,s,o,a;i=T(e),r===undefined&&(r=t,t="all");for(o=0;o1&&(s=N(e),e=[e[e.length-1]]),e=e[0],e=u(e),e in n||(n[e]=[]),n[e].push({shortcut:i[o],scope:t,method:r,key:i[o],mods:s})}function g(e,t){var r=e.split("+"),i=[],s,o;r.length>1&&(i=N(r),e=r[r.length-1]),e=u(e),t===undefined&&(t=S());if(!n[e])return;for(s in n[e])o=n[e][s],o.scope===t&&l(o.mods,i)&&(n[e][s]={})}function y(e){return typeof e=="string"&&(e=u(e)),f(a,e)!=-1}function b(){return a.slice(0)}function w(e){var t=(e.target||e.srcElement).tagName;return t!="INPUT"&&t!="SELECT"&&t!="TEXTAREA"}function E(e){i=e||"all"}function S(){return i||"all"}function x(e){var t,r,i;for(t in n){r=n[t];for(i=0;i0;for(u in r)if(!r[u]&&f(o.mods,+u)>-1||r[u]&&f(o.mods,+u)==-1)c=!1;(o.mods.length==0&&!r[16]&&!r[18]&&!r[17]&&!r[91]||c)&&o.method(e,o)===!1&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}}function d(e){var t=e.keyCode,n,i=f(a,t);i>=0&&a.splice(i,1);if(t==93||t==224)t=91;if(t in r){r[t]=!1;for(n in s)s[n]==t&&(m[n]=!1)}}function v(){for(t in r)r[t]=!1;for(t in s)m[t]=!1}function m(e,t,r){var i,s;i=T(e),r===undefined&&(r=t,t="all");for(var o=0;o1&&(s=N(e),e=[e[e.length-1]]),e=e[0],e=u(e),e in n||(n[e]=[]),n[e].push({shortcut:i[o],scope:t,method:r,key:i[o],mods:s})}function g(e,t){var r=e.split("+"),i=[],s,o;r.length>1&&(i=N(r),e=r[r.length-1]),e=u(e),t===undefined&&(t=S());if(!n[e])return;var a=[];for(s in n[e])o=n[e][s],(o.scope!==t||!l(o.mods,i))&&a.push(n[e][s]);n[e]=a}function y(e){return typeof e=="string"&&(e=u(e)),f(a,e)!=-1}function b(){return a.slice(0)}function w(e){var t=(e.target||e.srcElement).tagName;return t!="INPUT"&&t!="SELECT"&&t!="TEXTAREA"}function E(e){i=e||"all"}function S(){return i||"all"}function x(e){var t,r,i;for(t in n){r=n[t];for(i=0;i