Comparing version 0.0.25 to 0.0.26
@@ -78,3 +78,3 @@ 'use strict'; | ||
try { | ||
cb.call(self, pass, data); | ||
cb.call(self, idx, pass, data); | ||
} catch (e) { | ||
@@ -81,0 +81,0 @@ var r = pass ? {success: false} : result[idx]; |
@@ -7,23 +7,15 @@ 'use strict'; | ||
// Resolves with {duration, pages, total}; | ||
// Acquires next page via page index + THIS context; | ||
// Provides pages tracking: dest(pageIdx, data) | ||
// RESOLVES: | ||
// {duration, pages, total}, when `undefined` or `[]` returned from the source function; | ||
// Stops when: | ||
// a. An `undefined` or an empty array (resolves); | ||
// b. source or dest throw an error (rejects); | ||
// c. rejects with `invalid data` when something else returns from the source; | ||
// REJECTS: | ||
// 1. {index, error} - source returned invalid value, error="Invalid value!" | ||
// 2. {index, error, source} - source rejected or returned an error; | ||
// 3. {index, data} - data page rejected; | ||
// 4. {index, error, dest} - destination function rejected or threw an error; | ||
// When source or dest throw an error, or dest rejects - rejects with that error {pageIdx, error}; | ||
// When a regular promise rejects, we can still reject with that error? But we need the page also {pageIdx, error}; | ||
// Source format: (index, data); | ||
// Destination format: (index, data); | ||
// + THIS context; | ||
// We need to indicate clearly the nature if failure: | ||
// 1. error or reject from the source {index, error;}; | ||
// 2. page rejected (batch returned reject) {index, data=batch reject data}; | ||
// 3. dest rejected or threw an error {index, error, dest=resolved data passed in}; | ||
// IMPORTANT: source can return a promise to resolve with array or `undefined`; | ||
// NEED: generic resolver for such things everywhere! | ||
/** | ||
@@ -45,11 +37,12 @@ * @method page | ||
var self = this, result = [], start = Date.now(); | ||
/* | ||
maxSize = (parseInt(maxSize) === maxSize && maxSize > 0) ? maxSize : 0; | ||
var self = this, request, start = Date.now(), total = 0; | ||
return $p(function (resolve, reject) { | ||
function loop(idx) { | ||
$utils.resolve.call(self, source, [idx], function (value, delayed) { | ||
$utils.resolve.call(self, source, [idx, request], function (value) { | ||
if (value === undefined) { | ||
// no more pages left; | ||
finish(); | ||
finish(); // no more pages left; | ||
} else { | ||
@@ -60,11 +53,40 @@ if (value instanceof Array) { | ||
} else { | ||
// carry one; | ||
$utils.batch(value) | ||
.then(function (data) { | ||
request = data; | ||
total += data.length; | ||
// batch was successful, notify; | ||
if (dest) { | ||
var destResult; | ||
try { | ||
destResult = dest.call(self, idx, data); | ||
} catch (err) { | ||
reject({ | ||
index: idx, | ||
error: err, | ||
dest: data | ||
}); | ||
return; | ||
} | ||
if ($utils.isPromise(destResult)) { | ||
destResult | ||
.then(function () { | ||
next(); | ||
}, function (reason) { | ||
reject({ | ||
index: idx, | ||
error: reason, | ||
dest: data | ||
}); | ||
}); | ||
} else { | ||
next(); | ||
} | ||
} else { | ||
next(); | ||
} | ||
}, function (reason) { | ||
reject({ | ||
error: reason, | ||
index: idx, | ||
data: value | ||
data: reason | ||
}); | ||
@@ -80,57 +102,16 @@ }); | ||
} | ||
// sequence continues; | ||
if (dest) { | ||
var destResult; | ||
try { | ||
destResult = dest.call(self, idx, value); | ||
} catch (e) { | ||
reject({ | ||
dest: data, | ||
index: idx, | ||
error: e | ||
}); | ||
return; | ||
} | ||
if ($utils.isPromise(destResult)) { | ||
destResult | ||
.then(function () { | ||
next(true); | ||
}, function (reason) { | ||
reject({ | ||
dest: data, | ||
index: idx, | ||
error: reason | ||
}); | ||
}); | ||
} else { | ||
next(delayed); | ||
} | ||
} else { | ||
next(delayed); | ||
} | ||
}, function (reason) { | ||
reject({ | ||
source: data, | ||
index: idx, | ||
error: reason | ||
error: reason, | ||
source: request | ||
}); | ||
}); | ||
function next(delayed) { | ||
function next() { | ||
idx++; | ||
if (maxSize && maxSize === idx) { | ||
if (maxSize === idx) { | ||
finish(); | ||
} else { | ||
if (delayed) { | ||
loop(idx); | ||
} else { | ||
$p.resolve() | ||
.then(function () { | ||
loop(idx); | ||
}); | ||
} | ||
loop(idx); | ||
} | ||
@@ -140,12 +121,7 @@ } | ||
function finish() { | ||
var length = Date.now() - start; | ||
if (track) { | ||
$utils.extend(result, 'duration', length); | ||
} else { | ||
result = { | ||
total: idx, | ||
duration: length | ||
} | ||
} | ||
resolve(result); | ||
resolve({ | ||
pages: idx, | ||
total: total, | ||
duration: Date.now() - start | ||
}); | ||
} | ||
@@ -156,3 +132,3 @@ } | ||
}); | ||
*/ | ||
} | ||
@@ -159,0 +135,0 @@ |
@@ -45,2 +45,4 @@ 'use strict'; | ||
maxSize = (parseInt(maxSize) === maxSize && maxSize > 0) ? maxSize : 0; | ||
var self = this, data, result = [], start = Date.now(); | ||
@@ -101,3 +103,3 @@ | ||
idx++; | ||
if (maxSize && maxSize === idx) { | ||
if (maxSize === idx) { | ||
finish(); | ||
@@ -104,0 +106,0 @@ } else { |
{ | ||
"name": "spex", | ||
"version": "0.0.25", | ||
"version": "0.0.26", | ||
"description": "Specialized Promise Extensions", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
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
30005
595