fix(perf): replace async waterfall with promise chain

This commit is contained in:
Michael "Z" Goddard 2019-02-06 14:56:42 -05:00
parent 4f3d6ebaf1
commit 4c17e4e137
No known key found for this signature in database
GPG key ID: 762CD40DD5349872

View file

@ -1,8 +1,8 @@
var async = require('async'); var pify = require('pify');
var unpack = require('./lib/unpack'); var unpack = pify(require('./lib/unpack'));
var parse = require('./lib/parse'); var parse = pify(require('./lib/parse'));
var validate = require('./lib/validate'); var validate = pify(require('./lib/validate'));
/** /**
* Unpacks, parses, validates, and analyzes Scratch projects. If successful, * Unpacks, parses, validates, and analyzes Scratch projects. If successful,
@ -12,23 +12,15 @@ var validate = require('./lib/validate');
* @param {Function} callback Returns error or project data * @param {Function} callback Returns error or project data
*/ */
module.exports = function (input, isSprite, callback) { module.exports = function (input, isSprite, callback) {
// First unpack the input (need this outside of the async waterfall so that // Unpack the input and further transform the json portion by parsing and
// unpackedProject can be refered to again) // validating it.
unpack(input, isSprite, function (err, unpackedProject) { unpack(input, isSprite)
if (err) return callback(err); .then(function (unpackedProject) {
return parse(unpackedProject[0])
async.waterfall([ .then(validate.bind(null, isSprite))
function (cb) { .then(function (validatedProject) {
parse(unpackedProject[0], cb); return [validatedProject, unpackedProject[1]];
}, });
// TODO is there a better way to pass this arg })
// than partially applying this funciton? .then(callback.bind(null, null), callback);
validate.bind(null, isSprite)
], function (error, validatedInput) {
// One more callback wrapper so that we can re-package everything
// with the possible zip returned from unpack
if (error) return callback(error);
callback(null, [validatedInput, unpackedProject[1]]);
});
});
}; };