Skip to content

Commit 45a38ca

Browse files
committed
Make optimize synchronous
Ref #1015 Looks like `sax` is synchronous and we do not need to listen "end" event. This allows to avoid all callbacks and make `optimize` method synchronous.
1 parent b85d7f9 commit 45a38ca

File tree

8 files changed

+75
-113
lines changed

8 files changed

+75
-113
lines changed

examples/test.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,18 @@ FS.readFile(filepath, 'utf8', function(err, data) {
8282
throw err;
8383
}
8484

85-
svgo.optimize(data, {path: filepath}).then(function(result) {
85+
const result = svgo.optimize(data, {path: filepath});
8686

87-
console.log(result);
87+
console.log(result);
8888

89-
// {
90-
// // optimized SVG data string
91-
// data: '<svg width="10" height="20">test</svg>'
92-
// // additional info such as width/height
93-
// info: {
94-
// width: '10',
95-
// height: '20'
96-
// }
97-
// }
98-
99-
});
89+
// {
90+
// // optimized SVG data string
91+
// data: '<svg width="10" height="20">test</svg>'
92+
// // additional info such as width/height
93+
// info: {
94+
// width: '10',
95+
// height: '20'
96+
// }
97+
// }
10098

10199
});

lib/svgo.js

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,56 +21,37 @@ var SVGO = function(config) {
2121
this.config = CONFIG(config);
2222
};
2323

24-
SVGO.prototype.optimize = function(svgstr, info) {
25-
info = info || {};
26-
return new Promise((resolve, reject) => {
27-
if (this.config.error) {
28-
reject(this.config.error);
29-
return;
24+
SVGO.prototype.optimize = function(svgstr, info = {}) {
25+
const config = this.config;
26+
if (config.error) {
27+
throw Error(config.error);
28+
}
29+
const maxPassCount = config.multipass ? 10 : 1;
30+
let prevResultSize = Number.POSITIVE_INFINITY;
31+
let svgjs = null;
32+
for (let i = 0; i < maxPassCount; i += 1) {
33+
svgjs = SVG2JS(svgstr);
34+
if (svgjs.error == null) {
35+
svgjs = PLUGINS(svgjs, info, config.plugins);
3036
}
31-
32-
var config = this.config,
33-
maxPassCount = config.multipass ? 10 : 1,
34-
counter = 0,
35-
prevResultSize = Number.POSITIVE_INFINITY,
36-
optimizeOnceCallback = (svgjs) => {
37-
if (svgjs.error) {
38-
reject(svgjs.error);
39-
return;
40-
}
41-
42-
info.multipassCount = counter;
43-
if (++counter < maxPassCount && svgjs.data.length < prevResultSize) {
44-
prevResultSize = svgjs.data.length;
45-
this._optimizeOnce(svgjs.data, info, optimizeOnceCallback);
46-
} else {
47-
if (config.datauri) {
48-
svgjs.data = encodeSVGDatauri(svgjs.data, config.datauri);
49-
}
50-
if (info && info.path) {
51-
svgjs.path = info.path;
52-
}
53-
resolve(svgjs);
54-
}
55-
};
56-
57-
this._optimizeOnce(svgstr, info, optimizeOnceCallback);
58-
});
59-
};
60-
61-
SVGO.prototype._optimizeOnce = function(svgstr, info, callback) {
62-
var config = this.config;
63-
64-
SVG2JS(svgstr, function(svgjs) {
37+
svgjs = JS2SVG(svgjs, config.js2svg);
6538
if (svgjs.error) {
66-
callback(svgjs);
67-
return;
39+
throw Error(svgjs.error);
6840
}
69-
70-
svgjs = PLUGINS(svgjs, info, config.plugins);
71-
72-
callback(JS2SVG(svgjs, config.js2svg));
73-
});
41+
info.multipassCount = i;
42+
if (svgjs.data.length < prevResultSize) {
43+
prevResultSize = svgjs.data.length
44+
} else {
45+
if (config.datauri) {
46+
svgjs.data = encodeSVGDatauri(svgjs.data, config.datauri);
47+
}
48+
if (info && info.path) {
49+
svgjs.path = info.path;
50+
}
51+
return svgjs;
52+
}
53+
}
54+
return svgjs;
7455
};
7556

7657
/**

lib/svgo/coa.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -382,24 +382,23 @@ function processSVGData(config, info, data, output, input) {
382382
var startTime = Date.now(),
383383
prevFileSize = Buffer.byteLength(data, 'utf8');
384384

385-
return svgo.optimize(data, info).then(function(result) {
386-
if (config.datauri) {
387-
result.data = encodeSVGDatauri(result.data, config.datauri);
388-
}
389-
var resultFileSize = Buffer.byteLength(result.data, 'utf8'),
390-
processingTime = Date.now() - startTime;
385+
const result = svgo.optimize(data, info);
386+
if (config.datauri) {
387+
result.data = encodeSVGDatauri(result.data, config.datauri);
388+
}
389+
var resultFileSize = Buffer.byteLength(result.data, 'utf8'),
390+
processingTime = Date.now() - startTime;
391391

392-
return writeOutput(input, output, result.data).then(function() {
393-
if (!config.quiet && output != '-') {
394-
if (input) {
395-
console.log(`\n${PATH.basename(input)}:`);
396-
}
397-
printTimeInfo(processingTime);
398-
printProfitInfo(prevFileSize, resultFileSize);
392+
return writeOutput(input, output, result.data).then(function() {
393+
if (!config.quiet && output != '-') {
394+
if (input) {
395+
console.log(`\n${PATH.basename(input)}:`);
399396
}
400-
},
401-
error => Promise.reject(new Error(error.code === 'ENOTDIR' ? `Error: output '${output}' is not a directory.` : error)));
402-
});
397+
printTimeInfo(processingTime);
398+
printProfitInfo(prevFileSize, resultFileSize);
399+
}
400+
},
401+
error => Promise.reject(new Error(error.code === 'ENOTDIR' ? `Error: output '${output}' is not a directory.` : error)));
403402
}
404403

405404
/**

lib/svgo/svg2js.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@ var config = {
1919
* Convert SVG (XML) string to SVG-as-JS object.
2020
*
2121
* @param {String} data input data
22-
* @param {Function} callback
2322
*/
24-
module.exports = function(data, callback) {
23+
module.exports = function(data) {
2524

2625
var sax = SAX.parser(config.strict, config),
2726
root = new JSAPI({ elem: '#document', content: [] }),
2827
current = root,
2928
stack = [root],
30-
textContext = null,
31-
parsingError = false;
29+
textContext = null;
3230

3331
function pushToContent(content) {
3432

@@ -163,23 +161,12 @@ module.exports = function(data, callback) {
163161

164162
};
165163

166-
sax.onend = function() {
167-
168-
if (!this.error) {
169-
callback(root);
170-
} else {
171-
callback({ error: this.error.message });
172-
}
173-
174-
};
175-
176164
try {
177-
sax.write(data);
165+
sax.write(data).close();
166+
return root;
178167
} catch (e) {
179-
callback({ error: e.message });
180-
parsingError = true;
168+
return { error: e.message };
181169
}
182-
if (!parsingError) sax.close();
183170

184171
function trim(elem) {
185172
if (!elem.content) return elem;

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
"name": "Lev Solntsev",
2828
"email": "lev.sun@ya.ru",
2929
"url": "http://github.com/GreLI"
30+
},
31+
{
32+
"name": "Bogdan Chadkin",
33+
"email": "trysound@yandex.ru",
34+
"url": "http://github.com/TrySound"
3035
}
3136
],
3237
"repository": {

test/plugins/_index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ describe('plugins tests', function() {
4444
js2svg : { pretty: true }
4545
});
4646

47-
return svgo.optimize(orig, {path: file}).then(function(result) {
48-
//FIXME: results.data has a '\n' at the end while it should not
49-
normalize(result.data).should.be.equal(should);
50-
});
47+
const result = svgo.optimize(orig, {path: file});
48+
//FIXME: results.data has a '\n' at the end while it should not
49+
normalize(result.data).should.be.equal(should);
5150
});
5251

5352
});
@@ -69,4 +68,4 @@ function readFile(file) {
6968
resolve(data);
7069
});
7170
});
72-
}
71+
}

test/svg2js/_index.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ describe('svg2js', function() {
3030
throw err;
3131
}
3232

33-
SVG2JS(data, function(result) {
34-
root = result;
35-
done();
36-
});
33+
root = SVG2JS(data)
34+
done();
3735
});
3836

3937
});
@@ -423,9 +421,7 @@ describe('svg2js', function() {
423421
}
424422

425423
try {
426-
SVG2JS(data, function(result) {
427-
root = result;
428-
});
424+
root = SVG2JS(data)
429425
} catch (e) {
430426
error = e;
431427
}
@@ -473,9 +469,7 @@ describe('svg2js', function() {
473469
FS.readFile(filepath, 'utf8', function(err, data) {
474470
if (err) throw err;
475471

476-
SVG2JS(data, function(result) {
477-
root = result;
478-
});
472+
root = SVG2JS(data);
479473
done();
480474
});
481475
});

test/svgo/_index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ describe('indentation', function() {
3030
js2svg : { pretty: true, indent: 2 }
3131
});
3232

33-
svgo.optimize(orig, {path: filepath}).then(function(result) {
34-
normalize(result.data).should.be.equal(should);
35-
done();
36-
});
33+
const result = svgo.optimize(orig, {path: filepath});
34+
normalize(result.data).should.be.equal(should);
35+
done();
3736

3837
});
3938

0 commit comments

Comments
 (0)