Comparing version 0.2.2 to 0.2.3
@@ -0,1 +1,6 @@ | ||
# 0.2.3 | ||
* Updated define to be more performant | ||
* Rewrote Promise#chain | ||
#0.2.1 | ||
@@ -2,0 +7,0 @@ * Added check for `getgid` when logging on non posix systems |
@@ -1221,3 +1221,3 @@ /** | ||
promise: function () { | ||
return asyncArray(this) | ||
return asyncArray(this); | ||
} | ||
@@ -1224,0 +1224,0 @@ }); |
@@ -19,2 +19,23 @@ var string = require("./string"), | ||
function spreadArgs(f, args, scope) { | ||
var ret; | ||
switch ((args || []).length) { | ||
case 0: | ||
ret = f.call(scope); | ||
break; | ||
case 1: | ||
ret = f.call(scope, args[0]); | ||
break; | ||
case 2: | ||
ret = f.call(scope, args[0], args[1]); | ||
break; | ||
case 3: | ||
ret = f.call(scope, args[0], args[1], args[2]); | ||
break; | ||
default: | ||
ret = f.apply(scope, args); | ||
} | ||
return ret; | ||
} | ||
/** | ||
@@ -44,3 +65,3 @@ * Binds a method to a particular scope | ||
var scopeArgs = args.concat(Array.prototype.slice.call(arguments)); | ||
return func.apply(scope, scopeArgs); | ||
return spreadArgs(func, scopeArgs, scope); | ||
} else { | ||
@@ -53,3 +74,3 @@ return func; | ||
var scopeArgs = args.concat(Array.prototype.slice.call(arguments)); | ||
return method.apply(scope, scopeArgs); | ||
return spreadArgs(method, scopeArgs, scope); | ||
}; | ||
@@ -89,3 +110,3 @@ } | ||
scopeArgs = args.concat(scopeArgs); | ||
return func.apply(scope, scopeArgs); | ||
return spreadArgs(func, scopeArgs, scope); | ||
} else { | ||
@@ -99,7 +120,6 @@ return func; | ||
scopeArgs = args.concat(scopeArgs); | ||
return method.apply(scope, scopeArgs); | ||
return spreadArgs(method, scopeArgs, scope); | ||
}; | ||
} | ||
} | ||
; | ||
@@ -143,3 +163,3 @@ | ||
if (isFunction(func)) { | ||
return func.apply(scope, args); | ||
return spreadArgs(func, args, scope); | ||
} else { | ||
@@ -151,3 +171,3 @@ return func; | ||
return function () { | ||
return method.apply(scope, args); | ||
return spreadArgs(method, args, scope); | ||
}; | ||
@@ -217,3 +237,3 @@ } | ||
var scopeArgs = args.concat(Array.prototype.slice.call(arguments)); | ||
return func.apply(this, scopeArgs); | ||
return spreadArgs(func, scopeArgs, this) | ||
} else { | ||
@@ -226,3 +246,3 @@ return func; | ||
var scopeArgs = args.concat(Array.prototype.slice.call(arguments)); | ||
return method.apply(this, scopeArgs); | ||
return spreadArgs(method, scopeArgs, this) | ||
}; | ||
@@ -237,3 +257,3 @@ } | ||
return execute ? f.apply(this, arguments) : function (arg) { | ||
return f.apply(this, args.concat(Array.prototype.slice.call(arguments))); | ||
return spreadArgs(f, args.concat(Array.prototype.slice.call(arguments)), this); | ||
}; | ||
@@ -282,2 +302,76 @@ }; | ||
/** | ||
* Binds all methods or a specified array of function to the scope of the object | ||
* @example | ||
* var f = { | ||
* a: function(){ | ||
* return "a"; | ||
* }, | ||
* b: function(){ | ||
* return "b"; | ||
* } | ||
* } | ||
* | ||
* comb.hitchAll(f, "a", "b"); | ||
* | ||
* //or | ||
* | ||
* comb.hitchAll(f); | ||
* | ||
* @static | ||
* @memberOf comb | ||
* @param {Object} scope the object to bind methods on | ||
* @param {...} funcs varargs of methods to bind | ||
* | ||
* @returns {Object} the originally scoped object. | ||
* */ | ||
function hitchAll(scope) { | ||
var funcs = Array.prototype.slice.call(arguments, 1); | ||
if (!isObject(scope) && !isFunction(scope)) { | ||
throw new TypeError("scope must be an object"); | ||
} | ||
if (funcs.length === 1 && isArray(funcs[0])) { | ||
funcs = funcs[0]; | ||
} | ||
if (!funcs.length) { | ||
funcs = []; | ||
for (var k in scope) { | ||
if (scope.hasOwnProperty(k) && isFunction(scope[k])) { | ||
funcs.push(k); | ||
} | ||
} | ||
} | ||
for (var i = 0, l = funcs.length; i < l; i++) { | ||
scope[funcs[i]] = hitch(scope, scope[funcs[i]]); | ||
} | ||
return scope; | ||
} | ||
/** | ||
* Binds all methods or a specified array of function to the scope of the object | ||
* @example | ||
* var f = { | ||
* a: function(){ | ||
* return "a"; | ||
* }, | ||
* b: function(){ | ||
* return "b"; | ||
* } | ||
* } | ||
* | ||
* comb.bindAll(f, "a", "b"); | ||
* | ||
* //or | ||
* | ||
* comb.bindAll(f); | ||
* | ||
* @static | ||
* @memberOf comb | ||
* @param {Object} scope the object to bind methods on | ||
* @param {...} funcs varargs of methods to bind | ||
* | ||
* @returns {Object} the originally scoped object. | ||
* */ | ||
exports.bindAll = hitchAll; | ||
exports.hitchAll = hitchAll; | ||
exports.isFunction = isFunction; | ||
@@ -290,2 +384,3 @@ exports.hitch = hitch; | ||
exports.curry = curry; | ||
exports.__spreadArgs = spreadArgs; | ||
@@ -8,3 +8,5 @@ /** | ||
isHash = _base.isHash, | ||
isArray = _base.isArray; | ||
isArray = _base.isArray, | ||
SUPER_REGEXP = /(super)/ig, | ||
invoke = _base.__spreadArgs; | ||
@@ -23,3 +25,3 @@ | ||
superMeta.pos = 1 + pos; | ||
return m.apply(this, args); | ||
return invoke(m, args, this); | ||
} | ||
@@ -59,3 +61,3 @@ } while (l > ++pos); | ||
superMeta.pos = 1 + pos; | ||
return m.apply(this, arguments); | ||
return invoke(m, arguments, this); | ||
} | ||
@@ -69,12 +71,17 @@ } while (l > ++pos); | ||
function functionWrapper(f, name) { | ||
var wrapper = function () { | ||
var ret, meta = this.__meta || {}; | ||
var orig = meta.superMeta; | ||
meta.superMeta = {f: f, pos: 0, name: name}; | ||
ret = f.apply(this, arguments); | ||
meta.superMeta = orig; | ||
return ret; | ||
}; | ||
wrapper._f = f; | ||
return wrapper; | ||
if (f.toString().match(SUPER_REGEXP)) { | ||
var wrapper = function () { | ||
var ret, meta = this.__meta || {}; | ||
var orig = meta.superMeta; | ||
meta.superMeta = {f: f, pos: 0, name: name}; | ||
ret = invoke(f, arguments, this); | ||
meta.superMeta = orig; | ||
return ret; | ||
}; | ||
wrapper._f = f; | ||
return wrapper; | ||
} else { | ||
f._f = f; | ||
return f; | ||
} | ||
} | ||
@@ -266,3 +273,3 @@ | ||
if (supers.length) { | ||
mixin.apply(child, supers); | ||
invoke(mixin, supers, child); | ||
} | ||
@@ -276,3 +283,4 @@ childProto._super = child._super = callSuper; | ||
function defineConstructor() { | ||
this.constructor.apply(this, arguments); | ||
invoke(this.constructor, arguments, this); | ||
sup = proto = null; | ||
} | ||
@@ -289,3 +297,3 @@ | ||
if (!retInstance) { | ||
this.constructor.apply(this, arguments); | ||
invoke(this.constructor, arguments, this); | ||
retInstance = this; | ||
@@ -292,0 +300,0 @@ } |
@@ -1,4 +0,7 @@ | ||
var hitch = require("./base/functions").hitch, | ||
define = require("./define").define, | ||
var define = require("./define").define, | ||
base = require("./base"), | ||
isObject = base.isObject, | ||
hitch = base.hitch, | ||
hitchAll = base.hitchAll, | ||
partial = base.partial, | ||
argsToArray = base.argsToArray, | ||
@@ -13,3 +16,4 @@ array = base.array, | ||
bindIgnore = base.bindIgnore, | ||
isInstanceOf = base.isInstanceOf; | ||
isInstanceOf = base.isInstanceOf, | ||
spreadArgs = base.__spreadArgs; | ||
@@ -25,2 +29,5 @@ var nextTick; | ||
} | ||
function reject(e) { | ||
return new Promise().errback(e); | ||
} | ||
@@ -72,2 +79,4 @@ | ||
this.__cbs = []; | ||
this.callback = hitch(this, this.callback); | ||
this.errback = hitch(this, this.errback); | ||
}, | ||
@@ -78,10 +87,16 @@ /** | ||
__resolve: function () { | ||
if (!this.__fired) { | ||
this.__fired = true; | ||
var cbs = this.__error ? this.__errorCbs : this.__cbs, | ||
len = cbs.length, i, | ||
results = this.__error || this.__results; | ||
for (i = 0; i < len; i++) { | ||
this.__callNextTick(cbs[i], results); | ||
} | ||
var self = this; | ||
if (!self.__fired) { | ||
self.__fired = true; | ||
nextTick(function () { | ||
var cbs = self.__error ? self.__errorCbs : self.__cbs, | ||
res = self.__error || self.__results, | ||
i = -1, l = cbs.length; | ||
while (++i < l) { | ||
spreadArgs(cbs[i], res); | ||
} | ||
self.__errorCbs.length = self.__cbs.length = 0; | ||
self = null; | ||
}); | ||
} | ||
@@ -91,6 +106,6 @@ }, | ||
__callNextTick: function (cb, results) { | ||
nextTick(hitch(this, function () { | ||
cb.apply(this, results); | ||
results = null; | ||
})); | ||
nextTick(function () { | ||
spreadArgs(cb, results); | ||
cb = results = null; | ||
}); | ||
}, | ||
@@ -108,4 +123,4 @@ | ||
if (cb) { | ||
if (exports.isPromiseLike(cb)) { | ||
cb = hitch(cb, "callback"); | ||
if (isPromise(cb)) { | ||
cb = cb.callback; | ||
} | ||
@@ -131,4 +146,4 @@ if (this.__fired && this.__results) { | ||
if (cb) { | ||
if (exports.isPromiseLike(cb)) { | ||
cb = hitch(cb, "errback"); | ||
if (isPromise(cb)) { | ||
cb = cb.errback; | ||
} | ||
@@ -155,4 +170,4 @@ if (this.__fired && this.__error) { | ||
this.addCallback(cb); | ||
if (exports.isPromiseLike(cb)) { | ||
this.addErrback(hitch(cb, "callback")); | ||
if (isPromise(cb)) { | ||
this.addErrback(cb.callback); | ||
} else { | ||
@@ -215,3 +230,3 @@ this.addErrback(cb); | ||
} else { | ||
this.callback.apply(this, argsToArray(arguments, 1)); | ||
spreadArgs(this.callback, argsToArray(arguments, 1), this); | ||
} | ||
@@ -230,9 +245,9 @@ return this; | ||
then: function (callback, errback) { | ||
if (exports.isPromiseLike(callback)) { | ||
this.addCallback(callback); | ||
this.addErrback(callback); | ||
} else { | ||
this.addCallback(callback); | ||
this.addErrback(errback); | ||
if (isPromise(callback)) { | ||
errback = callback.errback; | ||
callback = callback.callback; | ||
} | ||
this.addCallback(callback); | ||
this.addErrback(errback); | ||
return this; | ||
@@ -263,8 +278,4 @@ }, | ||
if ("function" === typeof cb) { | ||
this.addErrback(function (err) { | ||
cb(err); | ||
}); | ||
this.addCallback(function () { | ||
cb.apply(this, [null].concat(argsToArray(arguments))); | ||
}); | ||
this.addErrback(cb); | ||
this.addCallback(partial(cb, null)); | ||
} | ||
@@ -327,39 +338,29 @@ return this; | ||
chain: function (callback, errback) { | ||
var promise = new Promise(), | ||
self = this; | ||
var promise = new Promise(); | ||
function errorHandler() { | ||
var args = arguments; | ||
if (errback) { | ||
nextTick(function () { | ||
try { | ||
when(isFunction(errback) ? errback.apply(this, args) : errback).then(promise); | ||
} catch (e) { | ||
promise.errback(e); | ||
} | ||
promise = args = null; | ||
}); | ||
function _errback(e) { | ||
if (isFunction(errback)) { | ||
try { | ||
var res = spreadArgs(errback, [e]); | ||
isPromiseLike(res) ? res.then(promise.callback, promise.errback) : promise.callback(res); | ||
} catch (e) { | ||
promise.errback(e); | ||
} | ||
} else { | ||
promise.errback.apply(promise, args); | ||
promise = args = null; | ||
promise.errback(e); | ||
} | ||
} | ||
this.addCallback(function () { | ||
var args = arguments; | ||
nextTick(function () { | ||
try { | ||
when(isFunction(callback) ? callback.apply(this, args) : callback).then(function () { | ||
promise.callback.apply(promise, arguments); | ||
promise = args = null; | ||
}, errorHandler); | ||
} catch (e) { | ||
errorHandler(e); | ||
} | ||
}); | ||
}); | ||
function _callback() { | ||
try { | ||
var res = isFunction(callback) ? spreadArgs(callback, arguments) : callback; | ||
isPromiseLike(res) ? res.then(promise.callback, _errback) : promise.callback(res); | ||
} catch (e) { | ||
return _errback(e); | ||
} | ||
} | ||
// If no errback passed, then invoke our promise's errback to pass | ||
// on to next link in the chain. | ||
this.addErrback(errorHandler); | ||
self.addCallback(_callback); | ||
self.addErrback(_errback); | ||
return promise.promise(); | ||
@@ -421,3 +422,3 @@ }, | ||
ret[action] = function () { | ||
this[action].apply(this, arguments); | ||
spreadArgs(this[action], arguments, this); | ||
return ret; | ||
@@ -508,12 +509,16 @@ }.bind(this); | ||
__addPromise: function (promise, i) { | ||
promise.addCallback(hitch(this, function () { | ||
var args = argsToArray(arguments); | ||
args.unshift(i); | ||
this.callback.apply(this, args); | ||
})); | ||
promise.addErrback(hitch(this, function () { | ||
var args = argsToArray(arguments); | ||
args.unshift(i); | ||
this.errback.apply(this, args); | ||
})); | ||
var self = this; | ||
promise.then( | ||
function () { | ||
var args = argsToArray(arguments); | ||
args.unshift(i); | ||
spreadArgs(self.callback, args, self); | ||
promise = i = self = null; | ||
}, | ||
function () { | ||
var args = argsToArray(arguments); | ||
args.unshift(i); | ||
spreadArgs(self.errback, args, self); | ||
promise = i = self = null; | ||
}); | ||
}, | ||
@@ -528,8 +533,11 @@ | ||
this.__fired = true; | ||
var cbs = this.__errors.length ? this.__errorCbs : this.__cbs, | ||
len = cbs.length, i, | ||
results = this.__errors.length ? this.__errors : this.__results; | ||
for (i = 0; i < len; i++) { | ||
this.__callNextTick(cbs[i], results); | ||
} | ||
var self = this; | ||
nextTick(function () { | ||
var cbs = self.__errors.length ? self.__errorCbs : self.__cbs, | ||
len = cbs.length, i, | ||
results = self.__errors.length ? self.__errors : self.__results; | ||
for (i = 0; i < len; i++) { | ||
spreadArgs(cbs[i], [results]); | ||
} | ||
}); | ||
} | ||
@@ -539,5 +547,6 @@ }, | ||
__callNextTick: function (cb, results) { | ||
nextTick(hitch(this, function () { | ||
cb.apply(this, [results]); | ||
})); | ||
nextTick(function () { | ||
spreadArgs(cb, [results]); | ||
cb = results = null; | ||
}); | ||
}, | ||
@@ -547,4 +556,4 @@ | ||
if (cb) { | ||
if (exports.isPromiseLike(cb)) { | ||
cb = hitch(cb, "callback"); | ||
if (isPromise(cb)) { | ||
cb = cb.callback; | ||
} | ||
@@ -562,4 +571,4 @@ if (this.__fired && !this.__errors.length) { | ||
if (cb) { | ||
if (exports.isPromiseLike(cb)) { | ||
cb = hitch(cb, "errback"); | ||
if (isPromise(cb)) { | ||
cb = cb.errback; | ||
} | ||
@@ -642,6 +651,9 @@ if (this.__fired && this.__errors.length) { | ||
function isPromiseLike(obj) { | ||
return !isUndefinedOrNull(obj) && (isInstanceOf(obj, Promise) || (isFunction(obj.then) | ||
&& isFunction(obj.addCallback) && isFunction(obj.addErrback))); | ||
return isObject(obj, Promise) && typeof obj.then === "function"; | ||
} | ||
function isPromise(obj) { | ||
return obj instanceof Promise; | ||
} | ||
/** | ||
@@ -684,3 +696,3 @@ * Waits for promise and non promise values to resolve and fires callback or errback appropriately. If you pass in an array of promises | ||
p = new PromiseList(args.map(function (a) { | ||
return exports.isPromiseLike(a) ? a : new Promise().callback(a); | ||
return isPromiseLike(a) ? a : new Promise().callback(a); | ||
}), true); | ||
@@ -687,0 +699,0 @@ } |
{ | ||
"name": "comb", | ||
"description": "A framework for node", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"keywords": ["OO", "Object Oriented", "Collections", "Tree", "HashTable", "Pool", "Logging", "Promise", "Promises", "Proxy"], | ||
@@ -6,0 +6,0 @@ "repository": { |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2860737
83
11523