Use unminified source for chain.js
This commit is contained in:
parent
423c8043a6
commit
507ec8c8ee
154
external/load.js
vendored
154
external/load.js
vendored
@ -1,7 +1,159 @@
|
|||||||
/* Copyright (c) 2010 Chris O'Hara <cohara87@gmail.com>. MIT Licensed */
|
/* Copyright (c) 2010 Chris O'Hara <cohara87@gmail.com>. MIT Licensed */
|
||||||
|
|
||||||
//Include the chain.js microframework (http://github.com/chriso/chain.js)
|
//Include the chain.js microframework (http://github.com/chriso/chain.js)
|
||||||
(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);
|
(function(exports) {
|
||||||
|
|
||||||
|
exports = exports || {};
|
||||||
|
|
||||||
|
var handlers = {}, createChain, add;
|
||||||
|
|
||||||
|
createChain = function (context, stack, lastMethod) {
|
||||||
|
|
||||||
|
var inHandler = context.halt = false;
|
||||||
|
|
||||||
|
//The default error handler
|
||||||
|
context.error = function (e) {
|
||||||
|
throw e;
|
||||||
|
};
|
||||||
|
|
||||||
|
//Run the next method in the chain
|
||||||
|
context.next = function (exit) {
|
||||||
|
if (exit) {
|
||||||
|
inHandler = false;
|
||||||
|
}
|
||||||
|
if (!context.halt && stack && stack.length) {
|
||||||
|
var args = stack.shift(), method = args.shift();
|
||||||
|
inHandler = true;
|
||||||
|
try {
|
||||||
|
handlers[method].apply(context, [args, args.length, method]);
|
||||||
|
} catch (e) {
|
||||||
|
context.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return context;
|
||||||
|
};
|
||||||
|
|
||||||
|
//Bind each method to the context
|
||||||
|
for (var alias in handlers) {
|
||||||
|
if (typeof context[alias] === 'function') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
(function (alias) {
|
||||||
|
context[alias] = function () {
|
||||||
|
var args = Array.prototype.slice.call(arguments);
|
||||||
|
if (alias === 'onError') {
|
||||||
|
if (stack) {
|
||||||
|
handlers.onError.apply(context, [args, args.length]);
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
var new_context = {};
|
||||||
|
handlers.onError.apply(new_context, [args, args.length]);
|
||||||
|
return createChain(new_context, null, 'onError');
|
||||||
|
}
|
||||||
|
args.unshift(alias);
|
||||||
|
if (!stack) {
|
||||||
|
return createChain({}, [args], alias);
|
||||||
|
}
|
||||||
|
context.then = context[alias];
|
||||||
|
stack.push(args);
|
||||||
|
return inHandler ? context : context.next();
|
||||||
|
};
|
||||||
|
}(alias));
|
||||||
|
}
|
||||||
|
|
||||||
|
//'then' is an alias for the last method that was called
|
||||||
|
if (lastMethod) {
|
||||||
|
context.then = context[lastMethod];
|
||||||
|
}
|
||||||
|
|
||||||
|
//Used to call run(), chain() or another existing method when defining a new method
|
||||||
|
//See load.js (https://github.com/chriso/load.js/blob/master/load.js) for an example
|
||||||
|
context.call = function (method, args) {
|
||||||
|
args.unshift(method);
|
||||||
|
stack.unshift(args);
|
||||||
|
context.next(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
return context.next();
|
||||||
|
};
|
||||||
|
|
||||||
|
//Add a custom method/handler (see below)
|
||||||
|
add = exports.addMethod = function (method /*, alias1, alias2, ..., callback */) {
|
||||||
|
var args = Array.prototype.slice.call(arguments),
|
||||||
|
handler = args.pop();
|
||||||
|
for (var i = 0, len = args.length; i < len; i++) {
|
||||||
|
if (typeof args[i] === 'string') {
|
||||||
|
handlers[args[i]] = handler;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//When no aliases have been defined, automatically add 'then<Method>'
|
||||||
|
//e.g. adding 'run' also adds 'thenRun' as a method
|
||||||
|
if (!--len) {
|
||||||
|
handlers['then' + method.substr(0,1).toUpperCase() + method.substr(1)] = handler;
|
||||||
|
}
|
||||||
|
createChain(exports);
|
||||||
|
};
|
||||||
|
|
||||||
|
//chain() - Run each function sequentially
|
||||||
|
add('chain', function (args) {
|
||||||
|
var self = this, next = function () {
|
||||||
|
if (self.halt) {
|
||||||
|
return;
|
||||||
|
} else if (!args.length) {
|
||||||
|
return self.next(true);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (null != args.shift().call(self, next, self.error)) {
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
self.error(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
//run() - Run each function in parallel and progress once all functions are complete
|
||||||
|
add('run', function (args, arg_len) {
|
||||||
|
var self = this, chain = function () {
|
||||||
|
if (self.halt) {
|
||||||
|
return;
|
||||||
|
} else if (!--arg_len) {
|
||||||
|
self.next(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
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, error)) {
|
||||||
|
chain();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//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
|
||||||
|
add('onError', function (args, arg_len) {
|
||||||
|
var self = this;
|
||||||
|
this.error = function (err) {
|
||||||
|
self.halt = true;
|
||||||
|
for (var i = 0; i < arg_len; i++) {
|
||||||
|
args[i].call(self, err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
}(this));
|
||||||
|
|
||||||
|
|
||||||
var head = document.getElementsByTagName('head')[0] || document.documentElement;
|
var head = document.getElementsByTagName('head')[0] || document.documentElement;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user