Comparing version 0.1.7 to 0.1.8
157
aa.js
// aa.js - async-await.js | ||
this.aa = function () { | ||
'use strict'; | ||
var PromiseThunk = require('promise-thunk'); | ||
@@ -47,2 +49,21 @@ var isPromise = PromiseThunk.isPromise; | ||
// Queue | ||
function Queue() { | ||
this.tail = this.head = null; | ||
} | ||
Queue.prototype.push = function push(x) { | ||
if (this.tail) | ||
this.tail = this.tail[1] = [x, null]; | ||
else | ||
this.tail = this.head = [x, null]; | ||
}; | ||
Queue.prototype.shift = function shift() { | ||
if (!this.head) return null; | ||
var x = this.head[0]; | ||
this.head = this.head[1]; | ||
if (!this.head) this.tail = null; | ||
return x; | ||
}; | ||
// nextTickDo(fn) | ||
@@ -53,7 +74,8 @@ var nextTickDo = typeof setImmediate === 'function' ? setImmediate : | ||
var tasks = []; | ||
var tasks = new Queue(); | ||
var nextTickProgress = false; | ||
// nextTick(fn, ctx, ...args) | ||
function nextTick(fn, ctx, args) { | ||
// nextTick(fn, ...args) | ||
function nextTick(fn) { | ||
if (typeof fn !== 'function') | ||
@@ -66,8 +88,8 @@ throw new TypeError('fn must be a function'); | ||
nextTickProgress = true; | ||
nextTickDo(function () { | ||
var args; | ||
while (args = tasks.shift()) { | ||
var fn = args[0], ctx = args[1]; | ||
fn.apply(ctx, slice.call(args, 2)); | ||
} | ||
while (args = tasks.shift()) | ||
args[0](args[1], args[2], args[3], args[4]); | ||
nextTickProgress = false; | ||
@@ -120,75 +142,74 @@ }); | ||
nextTick(doValue, null, ret.value, callback); | ||
nextTick(doValue, ret.value, callback, ctx, args); | ||
} | ||
function doValue(value, callback) { | ||
if (value == null || | ||
typeof value !== 'object' && | ||
typeof value !== 'function') | ||
return callback(null, value); | ||
} | ||
if (value instanceof GeneratorFunction) | ||
value = value.apply(ctx, args); | ||
if (value instanceof GeneratorFunctionPrototype || isGenerator(value)) | ||
return aa.call(ctx, value)(callback); | ||
function doValue(value, callback, ctx, args) { | ||
if (value == null || | ||
typeof value !== 'object' && | ||
typeof value !== 'function') | ||
return callback(null, value); | ||
if (value instanceof PromiseThunk) | ||
return value(callback); | ||
if (value instanceof GeneratorFunction) | ||
value = value.apply(ctx, args); | ||
if (isPromise(value)) | ||
return value.then(function (val) { callback(null, val); }, callback); | ||
if ((value && value.constructor === GeneratorFunctionPrototype) || isGenerator(value)) | ||
return aa.call(ctx, value)(callback); | ||
// function must be a thunk | ||
if (typeof value === 'function') | ||
return value(callback); | ||
// function must be a thunk | ||
if (typeof value === 'function') | ||
return value(callback); | ||
var called = false; | ||
if (isPromise(value)) | ||
return value.then(function (val) { callback(null, val); }, callback); | ||
// array | ||
if (value instanceof Array) { | ||
var n = value.length; | ||
if (n === 0) return callback(null, []); | ||
var arr = Array(n); | ||
value.forEach(function (val, i) { | ||
doValue(val, function (err, val) { | ||
if (err) { | ||
if (!called) | ||
called = true, callback(err); | ||
} | ||
else { | ||
arr[i] = val; | ||
if (--n === 0 && !called) | ||
called = true, callback(null, arr); | ||
} | ||
}); | ||
var called = false; | ||
// array | ||
if (value instanceof Array) { | ||
var n = value.length; | ||
if (n === 0) return callback(null, []); | ||
var arr = Array(n); | ||
value.forEach(function (val, i) { | ||
doValue(val, function (err, val) { | ||
if (err) { | ||
if (!called) | ||
called = true, callback(err); | ||
} | ||
else { | ||
arr[i] = val; | ||
if (--n === 0 && !called) | ||
called = true, callback(null, arr); | ||
} | ||
}); | ||
} // array | ||
}); | ||
} // array | ||
// object | ||
else if (value.constructor === Object) { | ||
var keys = Object.keys(value); | ||
var n = keys.length; | ||
if (n === 0) return callback(null, {}); | ||
var obj = {}; | ||
keys.forEach(function (key) { | ||
obj[key] = undefined; | ||
doValue(value[key], function (err, val) { | ||
if (err) { | ||
if (!called) | ||
called = true, callback(err); | ||
} | ||
else { | ||
obj[key] = val; | ||
if (--n === 0 && !called) | ||
called = true, callback(null, obj); | ||
} | ||
}); | ||
// object | ||
else if (value.constructor === Object) { | ||
var keys = Object.keys(value); | ||
var n = keys.length; | ||
if (n === 0) return callback(null, {}); | ||
var obj = {}; | ||
keys.forEach(function (key) { | ||
obj[key] = undefined; | ||
doValue(value[key], function (err, val) { | ||
if (err) { | ||
if (!called) | ||
called = true, callback(err); | ||
} | ||
else { | ||
obj[key] = val; | ||
if (--n === 0 && !called) | ||
called = true, callback(null, obj); | ||
} | ||
}); | ||
} // object | ||
}); | ||
} // object | ||
// other value | ||
else | ||
return callback(null, value); | ||
} | ||
// other value | ||
else | ||
return callback(null, value); | ||
} | ||
@@ -204,3 +225,3 @@ | ||
function isGenerator(gtor) { | ||
return gtor instanceof GeneratorFunctionPrototype || !!gtor && gtor.next === 'function'; | ||
return !!gtor && (gtor.constructor === GeneratorFunctionPrototype || gtor.next === 'function'); | ||
} | ||
@@ -207,0 +228,0 @@ |
{ | ||
"name": "aa", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "aa - Async-Await. co like library, go like channel, thunkify or promisefy wrap package", | ||
@@ -9,3 +9,7 @@ "main": "aa.js", | ||
}, | ||
"devDependencies": {}, | ||
"devDependencies": { | ||
"co": ">=4.6.0", | ||
"control-c": ">=0.0.9", | ||
"promise-light": ">=0.1.5" | ||
}, | ||
"keywords": [ | ||
@@ -12,0 +16,0 @@ "chan", |
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
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
219408
43
5290
1
213
3
2
6