Cscott has uploaded a new change for review.
https://gerrit.wikimedia.org/r/147180
Change subject: Use `Promise.guard` from `prfun`.
......................................................................
Use `Promise.guard` from `prfun`.
Change-Id: I4360aad9c4b23285e3a3e7f6328b8b48b87ee9d8
---
M lib/authors.js
M lib/html.js
M lib/image.js
M lib/p.js
M lib/parsoid.js
M lib/revisions.js
6 files changed, 7 insertions(+), 72 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Collection/OfflineContentGenerator/bundler
refs/changes/80/147180/1
diff --git a/lib/authors.js b/lib/authors.js
index 9bd8d6c..bd29eb1 100644
--- a/lib/authors.js
+++ b/lib/authors.js
@@ -17,8 +17,7 @@
/**
* Obtain the contributors list for a single article on a wiki.
*/
-Authors.prototype.fetchMetadata = P.guard(
- P.guard.n(AUTHORS_REQUEST_LIMIT),
+Authors.prototype.fetchMetadata = Promise.guard(AUTHORS_REQUEST_LIMIT,
function(wiki, title, status /* optional */)
{
if (status) {
diff --git a/lib/html.js b/lib/html.js
index 6224d5e..8e2c653 100644
--- a/lib/html.js
+++ b/lib/html.js
@@ -12,8 +12,8 @@
var Html = module.exports = function(wikis) {
var api = new Api(wikis);
// limit concurrency of API requests
- this.request = P.guard(
- P.guard.n(PARSER_REQUEST_LIMIT), api.request.bind(api)
+ this.request = Promise.guard(
+ PARSER_REQUEST_LIMIT, api.request.bind(api)
);
this.wikis = wikis;
};
diff --git a/lib/image.js b/lib/image.js
index 7136df5..854ac46 100644
--- a/lib/image.js
+++ b/lib/image.js
@@ -32,7 +32,7 @@
// returns a promise for the metadata for the given image
Image.prototype.fetchMetadata =
-P.guard(P.guard.n(IMAGE_REQUEST_LIMIT), function(img, status /* optional */) {
+Promise.guard(IMAGE_REQUEST_LIMIT, function(img, status /* optional */) {
if (status) {
// we're inside the guard, so these won't come all at once
status.report(null, img.short + ' [metadata]');
@@ -56,7 +56,7 @@
// returns a promise for the name of the on-disk filename for the given image
// (or null, if the image couldn't be fetched)
Image.prototype.fetch =
-P.guard(P.guard.n(IMAGE_REQUEST_LIMIT), function(img, imagedir, status
/*optional*/) {
+Promise.guard(IMAGE_REQUEST_LIMIT, function(img, imagedir, status
/*optional*/) {
var maxRes = +(img.imagesize) || 1200 ;
var deferred = Promise.defer();
diff --git a/lib/p.js b/lib/p.js
index cd3b28d..f245e27 100644
--- a/lib/p.js
+++ b/lib/p.js
@@ -58,67 +58,3 @@
return f.call(null, value, index, null);
}, p);
};
-
-// Limit the amount of parallelism.
-// Implementation borrowed from:
-// https://github.com/cujojs/when/blob/master/guard.js
-// Authors: Brian Cavalier, John Hann, Sakari Jokinen
-// docs at: https://github.com/cujojs/when/blob/master/docs/api.md#whenguard
-
-/**
- * Creates a guarded version of f that can only be entered when the supplied
- * condition allows.
- * @param {function} condition represents a critical section that may only
- * be entered when allowed by the condition
- * @param {function} f function to guard
- * @returns {function} guarded version of f
- */
-P.guard = function(condition, f) {
- return function() {
- var self, args;
-
- self = this;
- args = arguments;
-
- return Promise.resolve(condition()).then(function(exit) {
- return Promise.resolve(f.apply(self,
args)).finally(exit);
- });
- };
-};
-
-/**
- * Creates a condition that allows only n simultaneous executions
- * of a guarded function
- * @param {number} allowed number of allowed simultaneous executions
- * @returns {function} condition function which returns a promise that
- * fulfills when the critical section may be entered. The fulfillment
- * value is a function ("notifyExit") that must be called when the critical
- * section has been exited.
- */
-P.guard.n = function (allowed) {
- var count, waiting;
-
- count = 0;
- waiting = [];
-
- var exit = function() {
- if (count > 0) {
- count--;
- }
- if(waiting.length) {
- waiting.shift()(exit);
- }
- };
-
- return function enter() {
- return new Promise(function(resolve) {
- if(count < allowed) {
- resolve(exit);
- } else {
- waiting.push(resolve);
- }
- count += 1;
-
- });
- };
-};
diff --git a/lib/parsoid.js b/lib/parsoid.js
index 5e37249..745199b 100644
--- a/lib/parsoid.js
+++ b/lib/parsoid.js
@@ -127,4 +127,4 @@
};
// We limit the number of parallel fetches allowed to be 'in flight'
-Parsoid.prototype.fetch = P.guard(P.guard.n(PARSOID_REQUEST_LIMIT), fetch);
+Parsoid.prototype.fetch = Promise.guard(PARSOID_REQUEST_LIMIT, fetch);
diff --git a/lib/revisions.js b/lib/revisions.js
index 0f8d0c2..d9a1248 100644
--- a/lib/revisions.js
+++ b/lib/revisions.js
@@ -81,7 +81,7 @@
fs.createWriteStream(path.join(outdir, 'revisions-1.txt')) :
null;
- var fetchAndWriteOne = P.guard(P.guard.n(REVISION_REQUEST_LIMIT),
function(t) {
+ var fetchAndWriteOne = Promise.guard(REVISION_REQUEST_LIMIT,
function(t) {
status.report(null, wikis[t.wiki].prefix + ':' + t.title);
return fetchOne(wikis, t.wiki, t.canontitle || t.title,
t.revid).
then(function(data) {
--
To view, visit https://gerrit.wikimedia.org/r/147180
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I4360aad9c4b23285e3a3e7f6328b8b48b87ee9d8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Collection/OfflineContentGenerator/bundler
Gerrit-Branch: master
Gerrit-Owner: Cscott <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits