Comparing version 0.0.10 to 0.0.11
20
API.md
@@ -19,3 +19,3 @@ ## Modules | ||
<dd></dd> | ||
<dt><a href="#page">page(source, dest)</a></dt> | ||
<dt><a href="#page">page(source, cb)</a></dt> | ||
<dd></dd> | ||
@@ -25,3 +25,5 @@ <dt><a href="#sequence">sequence(factory, [noTracking], [cb])</a> ⇒ <code>Promise</code></dt> | ||
<dt><a href="#stream">stream(source, dest)</a></dt> | ||
<dd></dd> | ||
<dd><p>Acquires promise objects dynamically from the source function, resolves them, | ||
and passes the result into the destination function.</p> | ||
</dd> | ||
<dt><a href="#throttle">throttle(values)</a></dt> | ||
@@ -53,3 +55,3 @@ <dd></dd> | ||
**Summary**: Attempts to resolve every value in the input array. | ||
**Returns**: <code>Promise</code> - Result for the entire batch, which resolves when every promise in the input array has been resolved, and rejects when one or more promise objects in the array rejected: - resolves with an array of individual resolved results, the same as `promise.all`; - rejects with an array of objects `{success, result}`: - `success`: `true/false`, indicates whether the corresponding value in the input array was resolved. - `result`: resolved data, if `success=true`, or else the rejection reason. The array comes extended with function `getErrors`, which returns the list of just errors, with support for nested batch results. Calling `getErrors()[0]`, for example, will get the same result as the rejection reason that `promise.all` would provide. In both cases the output array is always the same size as the input one, providing index mapping between input and output values. | ||
**Returns**: <code>Promise</code> - Result for the entire batch, which resolves when every promise in the input array has been resolved, and rejects when one or more promise objects in the array rejected: - resolves with an array of individual resolved results, the same as `promise.all`; The array comes extended with property `duration` - number of milliseconds taken to resolve all the data. - rejects with an array of objects `{success, result}`: - `success`: `true/false`, indicates whether the corresponding value in the input array was resolved. - `result`: resolved data, if `success=true`, or else the rejection reason. The array comes extended with function `getErrors`, which returns the list of just errors, with support for nested batch results. Calling `getErrors()[0]`, for example, will get the same result as the rejection reason that `promise.all` would provide. In both cases the output array is always the same size as the input one, providing index mapping between input and output values. | ||
<table> | ||
@@ -83,2 +85,3 @@ <thead> | ||
**Kind**: global function | ||
**Summary**: Sequentially resolves a chain of dependent dynamic promises. | ||
<table> | ||
@@ -116,4 +119,5 @@ <thead> | ||
<a name="page"></a> | ||
## page(source, dest) | ||
## page(source, cb) | ||
**Kind**: global function | ||
**Summary**: Resolves dynamic arrays of promises page-by-page, till no data left; | ||
<table> | ||
@@ -129,3 +133,3 @@ <thead> | ||
</tr><tr> | ||
<td>dest</td> | ||
<td>cb</td> | ||
</tr> </tbody> | ||
@@ -137,3 +141,3 @@ </table> | ||
**Kind**: global function | ||
**Summary**: Sequentially resolves dynamic promises returned by a promise factory. | ||
**Summary**: Sequentially resolves a chain of independent dynamic promises. | ||
**Returns**: <code>Promise</code> - Result of the sequence, depending on `noTracking`: - resolves with an array of resolved data, if `noTracking = false`; - resolves with an integer - total number of resolved requests, if `noTracking = true`; - rejects with the reason when the factory function throws an error or returns a rejected promise. | ||
@@ -164,3 +168,6 @@ <table> | ||
## stream(source, dest) | ||
Acquires promise objects dynamically from the source function, resolves them, and passes the result into the destination function. | ||
**Kind**: global function | ||
**Summary**: Resolves promises one-by-one from source to destination. | ||
<table> | ||
@@ -183,2 +190,3 @@ <thead> | ||
**Kind**: global function | ||
**Summary**: Not yet formulated. | ||
<table> | ||
@@ -185,0 +193,0 @@ <thead> |
@@ -32,2 +32,6 @@ 'use strict'; | ||
* - resolves with an array of individual resolved results, the same as `promise.all`; | ||
* | ||
* The array comes extended with property `duration` - number of milliseconds | ||
* taken to resolve all the data. | ||
* | ||
* - rejects with an array of objects `{success, result}`: | ||
@@ -54,5 +58,7 @@ * - `success`: `true/false`, indicates whether the corresponding value | ||
if (!values.length) { | ||
return $p.resolve([]); | ||
var empty = []; | ||
utils.extend(empty, 'duration', 0); | ||
return $p.resolve(empty); | ||
} | ||
var self = this; | ||
var self = this, start = Date.now(); | ||
return $p(function (resolve, reject) { | ||
@@ -119,3 +125,3 @@ var errors = [], sorted = false, remaining = values.length, | ||
} | ||
result.getErrors = function () { | ||
utils.extend(result, 'getErrors', function () { | ||
if (!sorted) { | ||
@@ -133,5 +139,6 @@ errors.sort(); | ||
return err; | ||
}; | ||
}); | ||
reject(result); | ||
} else { | ||
utils.extend(result, 'duration', Date.now() - start); | ||
resolve(result); | ||
@@ -138,0 +145,0 @@ } |
@@ -11,2 +11,3 @@ 'use strict'; | ||
* @method cascade | ||
* @summary Sequentially resolves a chain of dependent dynamic promises. | ||
* @param route | ||
@@ -13,0 +14,0 @@ * @param context |
@@ -6,14 +6,23 @@ 'use strict'; | ||
// same as stream, except without destination; | ||
// plus, it will pass in the page index; | ||
// and it will resolve with {pages, total} | ||
// Resolves with {duration, pages, total}; | ||
// Acquires next page via page index + THIS context; | ||
// Provides pages tracking: cb(pageIdx, data) | ||
// unlike sequence, it cannot track anything; | ||
// Stops when: | ||
// a. A non-array returned or an empty array (resolves); | ||
// b. source or cb throw an error (rejects); | ||
// Normally rejects with? Should be like batch, except replacing | ||
// whole page of data with the error context; | ||
// Perhaps it will make more sense, if the implementation uses | ||
// method batch. | ||
/** | ||
* @method page | ||
* @summary Resolves dynamic arrays of promises page-by-page, till no data left; | ||
* @param source | ||
* @param dest | ||
* @param cb | ||
*/ | ||
function page(source, dest) { | ||
function page(source, cb) { | ||
} | ||
@@ -20,0 +29,0 @@ |
@@ -8,3 +8,3 @@ 'use strict'; | ||
* @method sequence | ||
* @summary Sequentially resolves dynamic promises returned by a promise factory. | ||
* @summary Sequentially resolves a chain of independent dynamic promises. | ||
* @param {Function} factory - a callback function `(idx, t)` to create and return a new promise, | ||
@@ -11,0 +11,0 @@ * based on the request index passed. When the value is anything other than a function, an error |
@@ -21,7 +21,10 @@ 'use strict'; | ||
// Q: What would be a method to do exactly the same, but deal with arrays also? | ||
// A: page, channel, throttle? | ||
// Once finished, resolves with an integer - total number of promises resolved; | ||
/** | ||
* @method stream | ||
* @summary Resolves promises one-by-one from source to destination. | ||
* @description | ||
* Acquires promise objects dynamically from the source function, resolves them, | ||
* and passes the result into the destination function. | ||
* @param source | ||
@@ -28,0 +31,0 @@ * @param dest |
@@ -6,11 +6,13 @@ 'use strict'; | ||
// It must involve timeouts between data requests and responses, possibly a configurator; | ||
/** | ||
* @method throttle | ||
* @summary Not yet formulated. | ||
* @param values | ||
*/ | ||
function throttle(values) { | ||
// TODO: Check the posts I did for Bluebird: https://github.com/petkaantonov/bluebird/issues/570 | ||
// | ||
} | ||
// TODO: Check the posts I did for Bluebird: https://github.com/petkaantonov/bluebird/issues/570 | ||
// Throttling strategies: page, cascade, channel | ||
@@ -17,0 +19,0 @@ // Check also: https://github.com/petkaantonov/bluebird/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aclosed+throttle |
'use strict'; | ||
var utils = require('./utils'); | ||
/* | ||
Summary for the methods: | ||
stream: one-by-one, from source and into the destination; | ||
page: take page-by-page, and just resolve them till no data left; | ||
channel: the combination of stream+page - pumping pages from source, resolving and passing into the destination; | ||
throttle: must involve timeouts between data requests and responses, possibly a configurator; | ||
cascade - like sequence, except in order to get the next promise, resolve data is required | ||
from the previous resolve, i.e. sequence is independent, while cascade is strictly dependent; | ||
(this could be called "chain") | ||
*/ | ||
/* | ||
List of methods presented in this library: | ||
1. Resolve an independent sequence of promises (sequence); | ||
2. Resolve a dependent sequence of promises (cascade); | ||
3. Stream promises one-by-one from source to destination (stream); | ||
4. Resolve pages of promises (page); | ||
5. Resolve an existing array of promises (batch); | ||
* */ | ||
// TODO: Provide standard stat for all operations, once they resolve: | ||
// {duration, total, data} | ||
/** | ||
@@ -56,9 +72,6 @@ * Specialized Promise Extensions | ||
function addMethod(obj, name, promise) { | ||
Object.defineProperty(obj, name, { | ||
value: require('./ext/' + name)(promise), | ||
enumerable: true, | ||
writable: false | ||
}); | ||
var method = require('./ext/' + name)(promise); | ||
utils.extend(obj, name, method); | ||
} | ||
module.exports = main; |
@@ -15,5 +15,17 @@ 'use strict'; | ||
//////////////////////////////////////// | ||
// Extends an object with an enumerable | ||
// read-only method or property. | ||
function extend(obj, name, value) { | ||
Object.defineProperty(obj, name, { | ||
value: value, | ||
enumerable: true, | ||
writable: false | ||
}); | ||
} | ||
module.exports = { | ||
isNull: isNull, | ||
isPromise: isPromise | ||
isPromise: isPromise, | ||
extend: extend | ||
}; |
{ | ||
"name": "spex", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"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
25555
445