Comparing version 0.1.2 to 0.1.3
@@ -1,2 +0,1 @@ | ||
/* Copyright (c) 2010 Chris O'Hara <cohara87@gmail.com>. MIT Licensed */ | ||
(function(a){a=a||{};var b={},c,d,e=function(){};c=function(a,d,e){var f=a.halt=!1;a.error=function(a){throw a},a.next=function(c){c&&(f=!1);if(!a.halt&&d&&d.length){var e=d.shift(),g=e.shift();f=!0;try{b[g].apply(a,[e,e.length,g])}catch(h){a.error(h)}}return a};for(var g in b){if(typeof a[g]==="function")continue;(function(b){a[b]=function(){var e=Array.prototype.slice.call(arguments);e.unshift(b);if(!d)return c({},[e],b);a.then=a[b],d.push(e);return f?a:a.next()}})(g)}e&&(a.then=a[e]),a.call=function(b,c){c.unshift(b),d.unshift(c),a.next(!0)};return a.next()},d=a.addMethod=function(d){var e=Array.prototype.slice.call(arguments),f=e.pop();for(var g=0,h=e.length;g<h;g++)typeof e[g]==="string"&&(b[e[g]]=f);--h||(b["then"+d[0].toUpperCase()+d.substr(1)]=f),c(a)},d("chain",function(a){var b=this,c=function(){if(!b.halt){if(!a.length)return b.next(!0);try{null!=a.shift().call(b,c,b.error)&&c()}catch(d){b.error(d)}}};c()}),d("run",function(a,b){var c=this,d=function(){c.halt||(--b||c.next(!0))};for(var e=0,f=b;!c.halt&&e<f;e++)null!=a[e].call(c,d,c.error)&&d()}),d("onError",function(a,b){var c=this;this.error=function(d){c.halt=!0;for(var e=0;e<b;e++)a[e].call(c,d)},this.next(!0)})})(this) | ||
/* Copyright (c) 2010 Chris O'Hara <cohara87@gmail.com>. MIT Licensed */(function(a){a=a||{};var b={},c,d;c=function(a,d,e){var f=a.halt=!1;a.error=function(a){throw a},a.next=function(c){c&&(f=!1);if(!a.halt&&d&&d.length){var e=d.shift(),g=e.shift();f=!0;try{b[g].apply(a,[e,e.length,g])}catch(h){a.error(h)}}return a};for(var g in b){if(typeof a[g]==="function")continue;(function(e){a[e]=function(){var g=Array.prototype.slice.call(arguments);if(e==="onError"){if(d){b.onError.apply(a,[g,g.length]);return a}var h={};b.onError.apply(h,[g,g.length]);return c(h,null,"onError")}g.unshift(e);if(!d)return c({},[g],e);a.then=a[e],d.push(g);return f?a:a.next()}})(g)}e&&(a.then=a[e]),a.call=function(b,c){c.unshift(b),d.unshift(c),a.next(!0)};return a.next()},d=a.addMethod=function(d){var e=Array.prototype.slice.call(arguments),f=e.pop();for(var g=0,h=e.length;g<h;g++)typeof e[g]==="string"&&(b[e[g]]=f);--h||(b["then"+d.substr(0,1).toUpperCase()+d.substr(1)]=f),c(a)},d("chain",function(a){var b=this,c=function(){if(!b.halt){if(!a.length)return b.next(!0);try{null!=a.shift().call(b,c,b.error)&&c()}catch(d){b.error(d)}}};c()}),d("run",function(a,b){var c=this,d=function(){c.halt||--b||c.next(!0)},e=function(a){c.error(a)};for(var f=0,g=b;!c.halt&&f<g;f++)null!=a[f].call(c,d,e)&&d()}),d("defer",function(a){var b=this;setTimeout(function(){b.next(!0)},a.shift())}),d("onError",function(a,b){var c=this;this.error=function(d){c.halt=!0;for(var e=0;e<b;e++)a[e].call(c,d)}})})(this) |
61
chain.js
/* Copyright (c) 2010 Chris O'Hara <cohara87@gmail.com>. MIT Licensed */ | ||
(function(exports) { | ||
(function(exports) { | ||
exports = exports || {}; | ||
var handlers = {}, createChain, add, nop = function(){}; | ||
var handlers = {}, createChain, add; | ||
createChain = function (context, stack, lastMethod) { | ||
var inHandler = context.halt = false; | ||
//The default error handler | ||
//The default error handler | ||
context.error = function (e) { | ||
throw e; | ||
} | ||
//Run the next method in the chain | ||
@@ -34,3 +33,3 @@ context.next = function (exit) { | ||
} | ||
//Bind each method to the context | ||
@@ -44,2 +43,12 @@ for (var alias in handlers) { | ||
var args = Array.prototype.slice.call(arguments); | ||
if (alias === 'onError') { | ||
if (stack) { | ||
handlers.onError.apply(context, [args, args.length]); | ||
return context; | ||
} else { | ||
var new_context = {}; | ||
handlers.onError.apply(new_context, [args, args.length]); | ||
return createChain(new_context, null, 'onError'); | ||
} | ||
} | ||
args.unshift(alias); | ||
@@ -55,3 +64,3 @@ if (!stack) { | ||
} | ||
//'then' is an alias for the last method that was called | ||
@@ -61,3 +70,3 @@ if (lastMethod) { | ||
} | ||
//Used to call run(), chain() or another existing method when defining a new method | ||
@@ -70,6 +79,6 @@ //See load.js (https://github.com/chriso/load.js/blob/master/load.js) for an example | ||
} | ||
return context.next(); | ||
} | ||
//Add a custom method/handler (see below) | ||
@@ -87,7 +96,7 @@ add = exports.addMethod = function (method /*, alias1, alias2, ..., callback */) { | ||
if (!--len) { | ||
handlers['then' + method[0].toUpperCase() + method.substr(1)] = handler; | ||
handlers['then' + method.substr(0,1).toUpperCase() + method.substr(1)] = handler; | ||
} | ||
createChain(exports); | ||
} | ||
//chain() - Run each function sequentially | ||
@@ -111,3 +120,3 @@ add('chain', function (args) { | ||
}); | ||
//run() - Run each function in parallel and progress once all functions are complete | ||
@@ -122,4 +131,7 @@ add('run', function (args, arg_len) { | ||
} | ||
var error = function (e) { | ||
self.error(e); | ||
}; | ||
for (var i = 0, len = arg_len; !self.halt && i < len; i++) { | ||
if (null != args[i].call(self, chain, self.error)) { | ||
if (null != args[i].call(self, chain, error)) { | ||
chain(); | ||
@@ -130,2 +142,10 @@ } | ||
//defer() - Defer execution of the next method | ||
add('defer', function (args) { | ||
var self = this; | ||
setTimeout(function () { | ||
self.next(true); | ||
}, args.shift()); | ||
}); | ||
//onError() - Attach an error handler | ||
@@ -140,5 +160,4 @@ add('onError', function (args, arg_len) { | ||
} | ||
this.next(true); | ||
}); | ||
}(this)); | ||
}(this)); |
{ "name" : "chain", | ||
"description" : "A microframework for handling async JS", | ||
"version" : "0.1.2", | ||
"version" : "0.1.3", | ||
"homepage" : "http://github.com/chriso/chain.js", | ||
@@ -5,0 +5,0 @@ "keywords" : ["async","asynchronous","events","parallel"], |
@@ -176,2 +176,16 @@ var chain = require('../chain'), assert = require('assert'); | ||
'test chain.defer()': function () { | ||
var num = 0; | ||
chain.defer(50).thenRun(function () { | ||
num = 1; | ||
}); | ||
setTimeout(function() { | ||
assert.equal(0, num); | ||
}, 10); | ||
setTimeout(function() { | ||
assert.equal(1, num); | ||
}, 80); | ||
}, | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
17553
286
122
0