eventcache
Advanced tools
Comparing version 0.1.4 to 0.1.5
/** | ||
* eventcache v0.1.4 build Oct 26 2015 | ||
* eventcache v0.1.5 build Nov 06 2015 | ||
* https://github.com/vanruesc/eventcache | ||
* Copyright 2015 Raoul van Rueschen, Zlib | ||
* Copyright 2015 Raoul van Rüschen, Zlib | ||
*/ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
factory((global.EventCache = {})); | ||
}(this, function (exports) { 'use strict'; | ||
'use strict'; | ||
/** | ||
* The caches. | ||
* | ||
* @property caches | ||
* @type Array | ||
* @private | ||
* @static | ||
*/ | ||
/** | ||
* The caches. | ||
* | ||
* @property caches | ||
* @type Array | ||
* @private | ||
* @static | ||
*/ | ||
var caches = [[]]; | ||
var caches = [[]]; | ||
/** | ||
* Validates the arguments and tries to extract an event signature. | ||
* | ||
* @method extractSignature | ||
* @private | ||
* @static | ||
* @param {Object} args - The original arguments array from a call to add or remove. | ||
* @return {Array} The event signature. | ||
*/ | ||
/** | ||
* Validates the arguments and tries to extract an event signature. | ||
* | ||
* @method extractSignature | ||
* @private | ||
* @static | ||
* @param {Object} args - The original arguments array from a call to add or remove. | ||
* @return {Array} The event signature. | ||
*/ | ||
function extractSignature(args) { | ||
function extractSignature(args) { | ||
var signature = [null, null, null, null]; | ||
var signature = [null, null, null, null]; | ||
if(args.length && (args.length >= 3 || args[0].length >= 3)) { | ||
if(args.length && (args.length >= 3 || args[0].length >= 3)) { | ||
if(args.length === 1) { args = args[0]; } | ||
if(args.length === 1) { args = args[0]; } | ||
signature[0] = args[0]; | ||
signature[1] = args[1]; | ||
signature[2] = args[2]; | ||
signature[3] = (args.length === 4) ? args[3] : 0; | ||
signature[0] = args[0]; | ||
signature[1] = args[1]; | ||
signature[2] = args[2]; | ||
signature[3] = (args.length === 4) ? args[3] : 0; | ||
if(signature[3] < 0 || signature[3] >= caches.length) { | ||
if(signature[3] < 0 || signature[3] >= caches.length) { | ||
throw new Error("The given cache index is out of bounds."); | ||
throw new Error("The given cache index is out of bounds."); | ||
} | ||
} | ||
} else { | ||
} else { | ||
throw new Error("Invalid arguments."); | ||
throw new Error("Invalid arguments."); | ||
} | ||
return signature; | ||
} | ||
/** | ||
* Creates a new cache and returns its reference id. | ||
* | ||
* @method createCache | ||
* @return {Number} The index of the newly created cache. | ||
*/ | ||
return signature; | ||
} | ||
function createCache() { return caches.push([]) - 1; } | ||
/** | ||
* Creates a new cache and returns its reference id. | ||
* | ||
* @method createCache | ||
* @return {Number} The index of the newly created cache. | ||
*/ | ||
/** | ||
* Creates an event listener and adds its signature to the associated cache. | ||
* | ||
* @method bind | ||
* @static | ||
* @param {Object} obj - The object. | ||
* @param {String} event - The event name. | ||
* @param {Function} listener - The listener. | ||
* @param {Number} [cache=0] - The cache. | ||
*/ | ||
function createCache() { return caches.push([]) - 1; } | ||
function bind() { | ||
/** | ||
* Creates an event listener and adds its signature to the associated cache. | ||
* | ||
* @method bind | ||
* @static | ||
* @param {Object} obj - The object. | ||
* @param {String} event - The event name. | ||
* @param {Function} listener - The listener. | ||
* @param {Number} [cache=0] - The cache. | ||
*/ | ||
var s = extractSignature(arguments), | ||
obj = s[0], type = s[1], fn = s[2], cache = s[3]; | ||
function bind() { | ||
if(obj.addEventListener) { | ||
var s = extractSignature(arguments), | ||
obj = s[0], type = s[1], fn = s[2], cache = s[3]; | ||
obj.addEventListener(type, fn, false); | ||
if(obj.addEventListener) { | ||
} else if(obj.attachEvent) { | ||
obj.addEventListener(type, fn, false); | ||
obj.attachEvent("on" + type, fn); | ||
} else if(obj.attachEvent) { | ||
} else { | ||
obj.attachEvent("on" + type, fn); | ||
obj["on" + type] = fn; | ||
} else { | ||
} | ||
obj["on" + type] = fn; | ||
caches[cache].push(s); | ||
return s; | ||
} | ||
/** | ||
* Unbinds an event and removes its signature from the associated cache. | ||
* | ||
* @method unbind | ||
* @static | ||
* @param {Object} obj - The object. | ||
* @param {String} event - The event name. | ||
* @param {Function} listener - The listener. | ||
* @param {Number} [cache=0] - The cache. | ||
*/ | ||
caches[cache].push(s); | ||
return s; | ||
} | ||
function unbind() { | ||
/** | ||
* Unbinds an event and removes its signature from the associated cache. | ||
* | ||
* @method unbind | ||
* @static | ||
* @param {Object} obj - The object. | ||
* @param {String} event - The event name. | ||
* @param {Function} listener - The listener. | ||
* @param {Number} [cache=0] - The cache. | ||
*/ | ||
var i, s = extractSignature(arguments), | ||
obj = s[0], type = s[1], fn = s[2], cache = s[3]; | ||
function unbind() { | ||
for(i = caches[cache].length - 1; i >= 0; --i) { | ||
var i, s = extractSignature(arguments), | ||
obj = s[0], type = s[1], fn = s[2], cache = s[3]; | ||
s = caches[cache][i]; | ||
for(i = caches[cache].length - 1; i >= 0; --i) { | ||
if(s[0] === obj && s[1] === type && s[2] === fn) { | ||
s = caches[cache][i]; | ||
if(obj.removeEventListener) { | ||
if(s[0] === obj && s[1] === type && s[2] === fn) { | ||
obj.removeEventListener(type, fn, false); | ||
if(obj.removeEventListener) { | ||
} else if(obj.detachEvent) { | ||
obj.removeEventListener(type, fn, false); | ||
obj.detachEvent("on" + type, fn); | ||
} else if(obj.detachEvent) { | ||
} else { | ||
obj.detachEvent("on" + type, fn); | ||
obj["on" + type] = null; | ||
} else { | ||
} | ||
obj["on" + type] = null; | ||
caches[cache].splice(i, 1); | ||
i = 0; | ||
} | ||
caches[cache].splice(i, 1); | ||
i = 0; | ||
} | ||
@@ -149,73 +143,73 @@ | ||
/** | ||
* Removes all event signatures from the given cache | ||
* and releases the respective event listeners. | ||
* Calling flush without specifying a cache will unbind | ||
* and remove all listeners in all caches. Empty caches | ||
* will then be dropped and can no longer be used. | ||
* Only the default cache will remain. | ||
* | ||
* @method flush | ||
* @static | ||
* @param {Number} [cache] - The index of the cache to flush. If not provided, all caches will be flushed. | ||
*/ | ||
} | ||
function flush(cache) { | ||
/** | ||
* Removes all event signatures from the given cache | ||
* and releases the respective event listeners. | ||
* Calling flush without specifying a cache will unbind | ||
* and remove all listeners in all caches. Empty caches | ||
* will then be dropped and can no longer be used. | ||
* Only the default cache will remain. | ||
* | ||
* @method flush | ||
* @static | ||
* @param {Number} [cache] - The index of the cache to flush. If not provided, all caches will be flushed. | ||
*/ | ||
var i, len, j, reset = false; | ||
function flush(cache) { | ||
if(typeof cache !== "number" || isNaN(cache)) { | ||
var i, len, j, reset = false; | ||
cache = 0; | ||
len = caches.length; | ||
reset = true; | ||
if(typeof cache !== "number" || isNaN(cache)) { | ||
} else { | ||
cache = 0; | ||
len = caches.length; | ||
reset = true; | ||
if(cache < 0) { cache = 0; } | ||
else if(cache >= caches.length) { cache = caches.length - 1; } | ||
len = cache + 1; | ||
} else { | ||
} | ||
if(cache < 0) { cache = 0; } | ||
else if(cache >= caches.length) { cache = caches.length - 1; } | ||
len = cache + 1; | ||
for(i = cache; i < len; ++i) { | ||
} | ||
for(j = caches[i].length - 1; j >= 0; --j) { | ||
for(i = cache; i < len; ++i) { | ||
unbind(caches[i][j]); | ||
for(j = caches[i].length - 1; j >= 0; --j) { | ||
} | ||
unbind(caches[i][j]); | ||
} | ||
// Drop all caches except for the default one. | ||
if(reset) { caches.length = 1; } | ||
} | ||
/** | ||
* Provides insight into the internals. | ||
* | ||
* @method status | ||
* @static | ||
* @return {Object} The default cache and all additional caches. | ||
*/ | ||
// Drop all caches except for the default one. | ||
if(reset) { caches.length = 1; } | ||
function status() { | ||
} | ||
return { | ||
/** | ||
* Provides insight into the internals. | ||
* | ||
* @method status | ||
* @static | ||
* @return {Object} The default cache and all additional caches. | ||
*/ | ||
defaultCache: caches.slice(0, 1), | ||
additionalCaches: caches.slice(1) | ||
function status() { | ||
}; | ||
return { | ||
} | ||
defaultCache: caches.slice(0, 1), | ||
additionalCaches: caches.slice(1) | ||
exports.createCache = createCache; | ||
exports.bind = bind; | ||
exports.unbind = unbind; | ||
exports.flush = flush; | ||
exports.status = status; | ||
}; | ||
})); | ||
} | ||
exports.createCache = createCache; | ||
exports.bind = bind; | ||
exports.unbind = unbind; | ||
exports.flush = flush; | ||
exports.status = status; |
/** | ||
* eventcache v0.1.4 build Oct 26 2015 | ||
* eventcache v0.1.5 build Nov 06 2015 | ||
* https://github.com/vanruesc/eventcache | ||
* Copyright 2015 Raoul van Rueschen, Zlib | ||
* Copyright 2015 Raoul van Rüschen, Zlib | ||
*/ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(e.EventCache={})}(this,function(e){"use strict";function n(e){var n=[null,null,null,null];if(!e.length||!(e.length>=3||e[0].length>=3))throw new Error("Invalid arguments.");if(1===e.length&&(e=e[0]),n[0]=e[0],n[1]=e[1],n[2]=e[2],n[3]=4===e.length?e[3]:0,n[3]<0||n[3]>=l.length)throw new Error("The given cache index is out of bounds.");return n}function t(){return l.push([])-1}function r(){var e=n(arguments),t=e[0],r=e[1],o=e[2],i=e[3];return t.addEventListener?t.addEventListener(r,o,!1):t.attachEvent?t.attachEvent("on"+r,o):t["on"+r]=o,l[i].push(e),e}function o(){var e,t=n(arguments),r=t[0],o=t[1],i=t[2],u=t[3];for(e=l[u].length-1;e>=0;--e)t=l[u][e],t[0]===r&&t[1]===o&&t[2]===i&&(r.removeEventListener?r.removeEventListener(o,i,!1):r.detachEvent?r.detachEvent("on"+o,i):r["on"+o]=null,l[u].splice(e,1),e=0)}function i(e){var n,t,r,i=!1;for("number"!=typeof e||isNaN(e)?(e=0,t=l.length,i=!0):(0>e?e=0:e>=l.length&&(e=l.length-1),t=e+1),n=e;t>n;++n)for(r=l[n].length-1;r>=0;--r)o(l[n][r]);i&&(l.length=1)}function u(){return{defaultCache:l.slice(0,1),additionalCaches:l.slice(1)}}var l=[[]];e.createCache=t,e.bind=r,e.unbind=o,e.flush=i,e.status=u}); | ||
"use strict";function extractSignature(e){var n=[null,null,null,null];if(!e.length||!(e.length>=3||e[0].length>=3))throw new Error("Invalid arguments.");if(1===e.length&&(e=e[0]),n[0]=e[0],n[1]=e[1],n[2]=e[2],n[3]=4===e.length?e[3]:0,n[3]<0||n[3]>=caches.length)throw new Error("The given cache index is out of bounds.");return n}function createCache(){return caches.push([])-1}function bind(){var e=extractSignature(arguments),n=e[0],t=e[1],c=e[2],a=e[3];return n.addEventListener?n.addEventListener(t,c,!1):n.attachEvent?n.attachEvent("on"+t,c):n["on"+t]=c,caches[a].push(e),e}function unbind(){var e,n=extractSignature(arguments),t=n[0],c=n[1],a=n[2],r=n[3];for(e=caches[r].length-1;e>=0;--e)n=caches[r][e],n[0]===t&&n[1]===c&&n[2]===a&&(t.removeEventListener?t.removeEventListener(c,a,!1):t.detachEvent?t.detachEvent("on"+c,a):t["on"+c]=null,caches[r].splice(e,1),e=0)}function flush(e){var n,t,c,a=!1;for("number"!=typeof e||isNaN(e)?(e=0,t=caches.length,a=!0):(0>e?e=0:e>=caches.length&&(e=caches.length-1),t=e+1),n=e;t>n;++n)for(c=caches[n].length-1;c>=0;--c)unbind(caches[n][c]);a&&(caches.length=1)}function status(){return{defaultCache:caches.slice(0,1),additionalCaches:caches.slice(1)}}var caches=[[]];exports.createCache=createCache,exports.bind=bind,exports.unbind=unbind,exports.flush=flush,exports.status=status; |
@@ -5,3 +5,3 @@ { | ||
"description": "This small module aims to simplify the binding and unbinding of event listeners by creating distinct event caches.", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"url": "https://github.com/vanruesc/eventcache" | ||
@@ -8,0 +8,0 @@ }, |
{ | ||
"name": "eventcache", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "This small module aims to simplify the binding and unbinding of event listeners by creating distinct event caches.", | ||
@@ -17,3 +17,3 @@ "homepage": "https://github.com/vanruesc/eventcache", | ||
"author": { | ||
"name": "Raoul van Rueschen", | ||
"name": "Raoul van Rüschen", | ||
"email": "vanruesc@fh-brandenburg.de" | ||
@@ -37,3 +37,3 @@ }, | ||
"rollup": "rollup -f umd -n EventCache -i src/eventcache.js -o build/eventcache.js", | ||
"rollup": "rollup -f cjs -n EventCache -i src/eventcache.js -o build/eventcache.js", | ||
"uglify": "uglifyjs build/eventcache.js -c -m -o build/eventcache.min.js", | ||
@@ -53,3 +53,3 @@ | ||
"engines": { | ||
"node": ">= 0.10.0" | ||
"node": ">= 0.12.0" | ||
}, | ||
@@ -60,3 +60,3 @@ | ||
"devDependencies": { | ||
"@zayesh/eventdispatcher": "x.x.x", | ||
"@vanruesc/eventdispatcher": "x.x.x", | ||
"jshint": "2.x.x", | ||
@@ -63,0 +63,0 @@ "mocha": "2.x.x", |
@@ -81,3 +81,3 @@ # EventCache | ||
## License | ||
Copyright (c) 2015 Raoul van Rueschen | ||
Copyright (c) 2015 Raoul van Rüschen | ||
Licensed under the Zlib license. |
{ | ||
"name": "EventCache API", | ||
"description": "This small module aims to simplify the binding and unbinding of event listeners by creating distinct event caches.", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"url": "https://github.com/vanruesc/eventcache", | ||
@@ -6,0 +6,0 @@ "options": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
157678
1888