callstackjs
Advanced tools
Comparing version 0.4.0 to 0.5.0
235
callStack.js
/**! | ||
* Call stack controller | ||
* @author RubaXa <ibnRubaXa@gmail.com> | ||
* @author RubaXa <ibnRubaXa@gmail.com> | ||
*/ | ||
@@ -8,9 +8,11 @@ | ||
/*global define, module, window*/ | ||
(function (factory){ | ||
(function (factory) { | ||
"use strict"; | ||
if( typeof define === "function" && define.amd ){ | ||
define("callStack", [], factory); | ||
if (typeof define === "function" && define.amd) { | ||
define([], function () { | ||
return factory(); | ||
}); | ||
} | ||
else if( typeof module != "undefined" && typeof module.exports != "undefined" ){ | ||
else if (typeof module != "undefined" && typeof module.exports != "undefined") { | ||
module.exports = factory(); | ||
@@ -21,3 +23,3 @@ } | ||
} | ||
})(function (){ | ||
})(function () { | ||
"use strict"; | ||
@@ -32,10 +34,11 @@ | ||
var _simpleSort = function (arr){ | ||
var _simpleSort = function (arr) { | ||
var n = arr.length, swap; | ||
if( n > 1 ){ | ||
while( n-- > 1 ){ | ||
if( arr[n].weight > arr[n-1].weight ){ | ||
if (n > 1) { | ||
while (n-- > 1) { | ||
if (arr[n].weight > arr[n - 1].weight) { | ||
swap = arr[n]; | ||
arr[n] = arr[n-1]; | ||
arr[n-1] = swap; | ||
arr[n] = arr[n - 1]; | ||
arr[n - 1] = swap; | ||
} | ||
@@ -49,3 +52,6 @@ else { | ||
var _setImmediate = window.setImmediate || function (fn){ window.setTimeout(fn, 0); }; | ||
var _setImmediate = window.setImmediate || function (fn) { | ||
window.setTimeout(fn, 0); | ||
}; | ||
var _clearImmediate = window.clearImmediate || window.clearTimeout; | ||
@@ -59,14 +65,15 @@ | ||
*/ | ||
function _ifNotInStack(stack, fn, args, uniq){ | ||
function _ifNotInStack(stack, fn, args, uniq) { | ||
var i = stack.length, callee; | ||
while( i-- ){ | ||
while (i--) { | ||
callee = stack[i]; | ||
if( callee.fn === fn ){ | ||
if( uniq === 'once' ){ | ||
if (callee.fn === fn) { | ||
if (uniq === 'once') { | ||
stack.splice(i, 1); | ||
return true; | ||
return true; | ||
} | ||
else { | ||
return _argsNotEqual(callee.args, args); | ||
return _argsNotEqual(callee.args, args); | ||
} | ||
@@ -76,3 +83,3 @@ } | ||
return true; | ||
return true; | ||
} | ||
@@ -86,5 +93,5 @@ | ||
*/ | ||
function _argsNotEqual(left, right){ | ||
if( left.length !== right.length ){ | ||
return true; | ||
function _argsNotEqual(left, right) { | ||
if (left.length !== right.length) { | ||
return true; | ||
} | ||
@@ -94,9 +101,9 @@ else { | ||
while( i-- ){ | ||
if( left[i] !== right[i] ){ | ||
return true; | ||
while (i--) { | ||
if (left[i] !== right[i]) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
return false; | ||
} | ||
@@ -110,4 +117,4 @@ } | ||
*/ | ||
function _walkStackTick(){ | ||
if( _pid === void 0 || callStack.debounce === true ){ | ||
function _walkStackTick() { | ||
if (_pid === void 0 || callStack.debounce === true) { | ||
_clearImmediate(_pid); | ||
@@ -123,13 +130,22 @@ _pid = _setImmediate(_walkStack); | ||
*/ | ||
function _walkStack(){ | ||
function _walkStack() { | ||
_pid = void 0; | ||
if( _pause === false ){ | ||
var name, stack, i, n, callee, s = 0, sn = _order.length, ctx, fn, args; | ||
if (_pause === false) { | ||
var name, | ||
stack, | ||
i, | ||
n, | ||
callee, | ||
s = 0, | ||
sn = _order.length, | ||
ctx, | ||
fn, | ||
args; | ||
for( ; s < sn; s++ ){ | ||
name = _order[s]; | ||
stack = _stacks[name]; | ||
for (; s < sn; s++) { | ||
name = _order[s]; | ||
stack = _stacks[name]; | ||
if( (stack !== void 0) && (stack.paused === false) ){ | ||
if ((stack !== void 0) && (stack.paused === false)) { | ||
stack = stack.calls; | ||
@@ -141,15 +157,32 @@ _stacks[name].calls = []; | ||
for( ; i < n; i++ ){ | ||
callee = stack[i]; | ||
ctx = callee.ctx; | ||
fn = callee.fn; | ||
args = callee.args; | ||
for (; i < n; i++) { | ||
callee = stack[i]; | ||
ctx = callee.ctx; | ||
fn = callee.fn; | ||
args = callee.args; | ||
switch( args.length ){ | ||
case 0: fn.call(ctx); break; | ||
case 1: fn.call(ctx, args[0]); break; | ||
case 2: fn.call(ctx, args[0], args[1]); break; | ||
case 3: fn.call(ctx, args[0], args[1], args[2]); break; | ||
case 4: fn.call(ctx, args[0], args[1], args[2], args[3]); break; | ||
default: fn.apply(ctx, args); break; | ||
switch (args.length) { | ||
case 0: | ||
fn.call(ctx); | ||
break; | ||
case 1: | ||
fn.call(ctx, args[0]); | ||
break; | ||
case 2: | ||
fn.call(ctx, args[0], args[1]); | ||
break; | ||
case 3: | ||
fn.call(ctx, args[0], args[1], args[2]); | ||
break; | ||
case 4: | ||
fn.call(ctx, args[0], args[1], args[2], args[3]); | ||
break; | ||
default: | ||
fn.apply(ctx, args); | ||
break; | ||
} | ||
@@ -161,3 +194,3 @@ } | ||
i = _tickFns.length; | ||
while( i-- ){ | ||
while (i--) { | ||
_tickFns[i](); | ||
@@ -169,3 +202,2 @@ } | ||
/** | ||
@@ -176,13 +208,13 @@ * Wrap call | ||
*/ | ||
function _wrapCall(ctx, fn, opts){ | ||
/*jshint validthis:true*/ | ||
function _wrapCall(ctx, fn, opts) { | ||
/*jshint validthis:true*/ | ||
var stack = this; | ||
if( typeof fn === 'string' ){ | ||
if (typeof fn === 'string') { | ||
return ctx[fn] = this.wrap(ctx, ctx[fn], opts); | ||
} | ||
else if( (fn === void 0) || !(fn instanceof Function) ){ | ||
opts = fn; | ||
fn = ctx; | ||
ctx = null; | ||
else if ((fn === void 0) || !(fn instanceof Function)) { | ||
opts = fn; | ||
fn = ctx; | ||
ctx = null; | ||
} | ||
@@ -198,8 +230,8 @@ | ||
if( callStack.disabled ){ | ||
return fn; | ||
if (callStack.disabled) { | ||
return fn; | ||
} | ||
return function (){ | ||
if( !opts.uniq || _ifNotInStack(stack.calls, fn, arguments, opts.uniq) ){ | ||
return function callStackWrapper() { | ||
if (!opts.uniq || _ifNotInStack(stack.calls, fn, arguments, opts.uniq)) { | ||
var calls = stack.calls; | ||
@@ -209,9 +241,9 @@ | ||
calls.push({ | ||
fn: fn | ||
, ctx: ctx | ||
, args: arguments | ||
, weight: opts.weight|0 | ||
fn: fn, | ||
ctx: ctx, | ||
args: arguments, | ||
weight: opts.weight | 0 | ||
}); | ||
if( opts.weight !== 0 ){ | ||
if (opts.weight !== 0) { | ||
// Sort call stack | ||
@@ -232,4 +264,4 @@ _simpleSort(calls); | ||
*/ | ||
function callStack(name){ | ||
if( _stacks[name] == void 0 ){ | ||
function callStack(name) { | ||
if (_stacks[name] == void 0) { | ||
_stacks[name] = { | ||
@@ -241,15 +273,15 @@ calls: [], | ||
add: function (ctx, fn, opts){ | ||
add: function (ctx, fn, opts) { | ||
this.wrap(ctx, fn, opts)(); | ||
}, | ||
pause: function (){ | ||
pause: function () { | ||
this.paused = true; | ||
}, | ||
unpause: function (){ | ||
unpause: function () { | ||
this.paused = false; | ||
}, | ||
clear: function (){ | ||
clear: function () { | ||
this.calls = []; | ||
@@ -261,3 +293,3 @@ } | ||
return _stacks[name]; | ||
return _stacks[name]; | ||
} | ||
@@ -275,4 +307,4 @@ | ||
*/ | ||
callStack.wrap = function (ctx, fn, opts){ | ||
return callStack('default').wrap(ctx, fn, opts); | ||
callStack.wrap = function (ctx, fn, opts) { | ||
return callStack('default').wrap(ctx, fn, opts); | ||
}; | ||
@@ -289,3 +321,3 @@ | ||
*/ | ||
callStack.add = function (ctx, fn, opts){ | ||
callStack.add = function (ctx, fn, opts) { | ||
callStack('default').add(ctx, fn, opts); | ||
@@ -301,3 +333,3 @@ }; | ||
*/ | ||
callStack.tick = function (fn){ | ||
callStack.tick = function (fn) { | ||
_tickFns.push(fn); | ||
@@ -313,6 +345,7 @@ }; | ||
*/ | ||
callStack.untick = function (fn){ | ||
callStack.untick = function (fn) { | ||
var i = _tickFns.length; | ||
while( i-- ){ | ||
if( _tickFns[i] === fn ){ | ||
while (i--) { | ||
if (_tickFns[i] === fn) { | ||
_tickFns.splice(i, 1); | ||
@@ -331,7 +364,9 @@ break; | ||
*/ | ||
callStack.tick.one = function (fn){ | ||
callStack.tick(function _(){ | ||
callStack.untick(_); | ||
callStack.tick.one = function (fn) { | ||
var wrapper = function () { | ||
callStack.untick(wrapper); | ||
fn(); | ||
}); | ||
}; | ||
callStack.tick(wrapper); | ||
}; | ||
@@ -343,9 +378,12 @@ | ||
*/ | ||
callStack.order = function (){ | ||
var args = arguments, i = args.length, j; | ||
callStack.order = function () { | ||
var args = arguments, | ||
i = args.length, | ||
j; | ||
while( i-- ){ | ||
while (i--) { | ||
j = _order.length; | ||
while( j-- ){ | ||
if( _order[j] === args[i] ){ | ||
while (j--) { | ||
if (_order[j] === args[i]) { | ||
_order.splice(j, 1); | ||
@@ -365,3 +403,3 @@ break; | ||
*/ | ||
callStack.pause = function (){ | ||
callStack.pause = function () { | ||
_pause = true; | ||
@@ -375,3 +413,3 @@ }; | ||
*/ | ||
callStack.unpause = function (){ | ||
callStack.unpause = function () { | ||
_pause = false; | ||
@@ -385,3 +423,3 @@ _walkStackTick(); | ||
*/ | ||
callStack.clear = function (name){ | ||
callStack.clear = function (name) { | ||
callStack(name || 'default').clear(); | ||
@@ -399,7 +437,7 @@ }; | ||
*/ | ||
callStack.override = function (ctx, fn, callback){ | ||
if( typeof fn === 'string' ){ | ||
callStack.override = function (ctx, fn, callback) { | ||
if (typeof fn === 'string') { | ||
return ctx[fn] = callStack.override(ctx, ctx[fn], callback); | ||
} | ||
else if( callback === void 0 ){ | ||
else if (callback === void 0) { | ||
callback = fn; | ||
@@ -420,6 +458,5 @@ fn = ctx; | ||
// @export | ||
callStack.version = '0.4.0'; | ||
return callStack; | ||
// Export | ||
callStack.version = '0.5.0'; | ||
return callStack; | ||
}); | ||
@@ -1,2 +0,2 @@ | ||
/*! callStack 0.4.0 - MIT | git://github.com/rubaxa/callStack.git */ | ||
(function(n){"use strict";"function"==typeof define&&define.amd?define("callStack",[],n):"undefined"!=typeof module&&module.exports!==void 0?module.exports=n():window.callStack=n()})(function(){"use strict";function n(n,t,i,a){for(var c,u=n.length;u--;)if(c=n[u],c.fn===t)return"once"===a?(n.splice(u,1),!0):e(c.args,i);return!0}function e(n,e){if(n.length!==e.length)return!0;for(var t=s(n.length,e.length);t--;)if(n[t]!==e[t])return!0;return!1}function t(){(void 0===u||c.debounce===!0)&&(p(u),u=h(i))}function i(){if(u=void 0,o===!1){for(var n,e,t,i,a,c,s,d,h=0,p=r.length;p>h;h++)if(n=r[h],e=l[n],void 0!==e&&e.paused===!1)for(e=e.calls,l[n].calls=[],t=0,i=e.length;i>t;t++)switch(a=e[t],c=a.ctx,s=a.fn,d=a.args,d.length){case 0:s.call(c);break;case 1:s.call(c,d[0]);break;case 2:s.call(c,d[0],d[1]);break;case 3:s.call(c,d[0],d[1],d[2]);break;case 4:s.call(c,d[0],d[1],d[2],d[3]);break;default:s.apply(c,d)}for(t=f.length;t--;)f[t]()}}function a(e,i,a){var u=this;return"string"==typeof i?e[i]=this.wrap(e,e[i],a):(void 0!==i&&i instanceof Function||(a=i,i=e,e=null),a=a||{uniq:!1,weight:0},c.disabled?i:function(){if(!a.uniq||n(u.calls,i,arguments,a.uniq)){var c=u.calls;c.push({fn:i,ctx:e,args:arguments,weight:0|a.weight}),0!==a.weight&&d(c),t()}})}function c(n){return void 0==l[n]&&(l[n]={calls:[],paused:!1,wrap:a,add:function(n,e,t){this.wrap(n,e,t)()},pause:function(){this.paused=!0},unpause:function(){this.paused=!1},clear:function(){this.calls=[]}},r.push(n)),l[n]}var u,r=["default"],o=!1,l={},f=[],s=Math.max,d=function(n){var e,t=n.length;if(t>1)for(;t-->1&&n[t].weight>n[t-1].weight;)e=n[t],n[t]=n[t-1],n[t-1]=e},h=window.setImmediate||function(n){window.setTimeout(n,0)},p=window.clearImmediate||window.clearTimeout;return c.wrap=function(n,e,t){return c("default").wrap(n,e,t)},c.add=function(n,e,t){c("default").add(n,e,t)},c.tick=function(n){f.push(n)},c.untick=function(n){for(var e=f.length;e--;)if(f[e]===n){f.splice(e,1);break}},c.tick.one=function(n){c.tick(function e(){c.untick(e),n()})},c.order=function(){for(var n,e=arguments,t=e.length;t--;)for(n=r.length;n--;)if(r[n]===e[t]){r.splice(n,1);break}r.splice.apply(r,[1,0].concat([].slice.call(e)))},c.pause=function(){o=!0},c.unpause=function(){o=!1,t()},c.clear=function(n){c(n||"default").clear()},c.override=function(n,e,t){return"string"==typeof e?n[e]=c.override(n,n[e],t):(void 0===t&&(t=e,e=n),t(e))},c.debounce=!1,c.version="0.4.0",c}); | ||
/*! callStack 0.5.0 - MIT | git://github.com/rubaxa/callStack.git */ | ||
!function(a){"use strict";"function"==typeof define&&define.amd?define([],function(){return a()}):"undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports=a():window.callStack=a()}(function(){"use strict";function a(a,c,d,e){for(var f,g=a.length;g--;)if(f=a[g],f.fn===c)return"once"===e?(a.splice(g,1),!0):b(f.args,d);return!0}function b(a,b){if(a.length!==b.length)return!0;for(var c=l(a.length,b.length);c--;)if(a[c]!==b[c])return!0;return!1}function c(){(void 0===g||f.debounce===!0)&&(o(g),g=n(d))}function d(){if(g=void 0,i===!1){for(var a,b,c,d,e,f,l,m,n=0,o=h.length;o>n;n++)if(a=h[n],b=j[a],void 0!==b&&b.paused===!1)for(b=b.calls,j[a].calls=[],c=0,d=b.length;d>c;c++)switch(e=b[c],f=e.ctx,l=e.fn,m=e.args,m.length){case 0:l.call(f);break;case 1:l.call(f,m[0]);break;case 2:l.call(f,m[0],m[1]);break;case 3:l.call(f,m[0],m[1],m[2]);break;case 4:l.call(f,m[0],m[1],m[2],m[3]);break;default:l.apply(f,m)}for(c=k.length;c--;)k[c]()}}function e(b,d,e){var g=this;return"string"==typeof d?b[d]=this.wrap(b,b[d],e):(void 0!==d&&d instanceof Function||(e=d,d=b,b=null),e=e||{uniq:!1,weight:0},f.disabled?d:function(){if(!e.uniq||a(g.calls,d,arguments,e.uniq)){var f=g.calls;f.push({fn:d,ctx:b,args:arguments,weight:0|e.weight}),0!==e.weight&&m(f),c()}})}function f(a){return void 0==j[a]&&(j[a]={calls:[],paused:!1,wrap:e,add:function(a,b,c){this.wrap(a,b,c)()},pause:function(){this.paused=!0},unpause:function(){this.paused=!1},clear:function(){this.calls=[]}},h.push(a)),j[a]}var g,h=["default"],i=!1,j={},k=[],l=Math.max,m=function(a){var b,c=a.length;if(c>1)for(;c-- >1&&a[c].weight>a[c-1].weight;)b=a[c],a[c]=a[c-1],a[c-1]=b},n=window.setImmediate||function(a){window.setTimeout(a,0)},o=window.clearImmediate||window.clearTimeout;return f.wrap=function(a,b,c){return f("default").wrap(a,b,c)},f.add=function(a,b,c){f("default").add(a,b,c)},f.tick=function(a){k.push(a)},f.untick=function(a){for(var b=k.length;b--;)if(k[b]===a){k.splice(b,1);break}},f.tick.one=function(a){var b=function(){f.untick(b),a()};f.tick(b)},f.order=function(){for(var a,b=arguments,c=b.length;c--;)for(a=h.length;a--;)if(h[a]===b[c]){h.splice(a,1);break}h.splice.apply(h,[1,0].concat([].slice.call(b)))},f.pause=function(){i=!0},f.unpause=function(){i=!1,c()},f.clear=function(a){f(a||"default").clear()},f.override=function(a,b,c){return"string"==typeof b?a[b]=f.override(a,a[b],c):(void 0===c&&(c=b,b=a),c(b))},f.debounce=!1,f.version="0.5.0",f}); |
{ | ||
"name": "callstackjs", | ||
"exportName": "callStack", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"devDependencies": { | ||
"grunt": "~0.4.0", | ||
"grunt": "~0.4.5", | ||
"grunt-version": "*", | ||
"grunt-contrib-jshint": "~0.2.0", | ||
"grunt-contrib-qunit": "~0.2.0", | ||
"grunt-contrib-uglify": "~0.1" | ||
"grunt-contrib-qunit": "~0.5.1", | ||
"grunt-contrib-uglify": "~0.5.0" | ||
}, | ||
@@ -12,0 +12,0 @@ "description": "JavaScript call stack controller", |
19467
594