Use promises instead of async in font concatenation (remove async dependency)

This commit is contained in:
Petr Sloup
2017-05-10 10:22:39 +02:00
parent 5d93b1d4f9
commit bdc3d20524
5 changed files with 36 additions and 41 deletions

View File

@@ -1,7 +1,6 @@
'use strict';
var async = require('async'),
path = require('path'),
var path = require('path'),
fs = require('fs');
var clone = require('clone'),
@@ -73,47 +72,47 @@ module.exports.fixTileJSONCenter = function(tileJSON) {
}
};
module.exports.getFontsPbf = function(allowedFonts, fontPath, names, range, fallbacks, callback) {
var getFontPbf = function(allowedFonts, name, range, callback, fallbacks) {
var getFontPbf = function(allowedFonts, fontPath, name, range, fallbacks) {
return new Promise(function(resolve, reject) {
if (!allowedFonts || (allowedFonts[name] && fallbacks)) {
var filename = path.join(fontPath, name, range + '.pbf');
if (!fallbacks) {
fallbacks = clone(allowedFonts || {});
}
delete fallbacks[name];
return fs.readFile(filename, function(err, data) {
fs.readFile(filename, function(err, data) {
if (err) {
console.error('ERROR: Font not found:', name);
if (fallbacks && Object.keys(fallbacks).length) {
var fallbackName = Object.keys(fallbacks)[0];
console.error('ERROR: Trying to use', fallbackName, 'as a fallback');
delete fallbacks[fallbackName];
return getFontPbf(null, fallbackName, range, callback, fallbacks);
getFontPbf(null, fontPath, fallbackName, range, fallbacks).then(resolve, reject);
} else {
return callback(new Error('Font load error: ' + name));
reject('Font load error: ' + name);
}
} else {
return callback(null, data);
resolve(data);
}
});
} else {
return callback(new Error('Font not allowed: ' + name));
}
};
var fonts = names.split(',');
var queue = [];
fonts.forEach(function(font) {
queue.push(function(callback) {
getFontPbf(allowedFonts, font, range, callback, clone(allowedFonts || fallbacks));
});
});
return async.parallel(queue, function(err, results) {
if (err) {
callback(err, new Buffer([]));
} else {
callback(err, glyphCompose.combine(results));
reject('Font not allowed: ' + name);
}
});
};
module.exports.getFontsPbf = function(allowedFonts, fontPath, names, range, fallbacks) {
var fonts = names.split(',');
var queue = [];
fonts.forEach(function(font) {
queue.push(
getFontPbf(allowedFonts, fontPath, font, range, clone(allowedFonts || fallbacks))
);
});
return new Promise(function(resolve, reject) {
Promise.all(queue).then(function(values) {
return resolve(glyphCompose.combine(values));
}, reject);
});
};