Skip to content

Commit

Permalink
[fix] register unique events, fixes #65
Browse files Browse the repository at this point in the history
  • Loading branch information
Swaagie committed Oct 18, 2015
1 parent 5e84bce commit d436b8d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
10 changes: 6 additions & 4 deletions lib/minimize.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var debug = require('diagnostics')('minimize')
, EventEmitter = require('events').EventEmitter
, Helpers = require('./helpers')
, html = require('htmlparser2')
, uuid = require('node-uuid')
, emits = require('emits')
, async = require('async')
, util = require('util');
Expand Down Expand Up @@ -56,12 +57,13 @@ util.inherits(Minimize, EventEmitter);
*/
Minimize.prototype.parse = function parse(content, callback) {
if (typeof callback !== 'function') throw new Error('No callback provided');
var id = uuid.v4();

//
// Listen to DOM parsing, so the htmlparser callback can trigger it.
//
this.once('read', this.minifier, this);
this.once('parsed', callback);
this.once('read', this.minifier.bind(this, id));
this.once('parsed:' + id, callback);

//
// Initiate parsing of HTML.
Expand All @@ -76,13 +78,13 @@ Minimize.prototype.parse = function parse(content, callback) {
* @param {Object} dom presented as traversable object
* @api private
*/
Minimize.prototype.minifier = function minifier(error, dom) {
Minimize.prototype.minifier = function minifier(id, error, dom) {
if (error) throw new Error('Minifier failed to parse DOM', error);

//
// DOM has been completely parsed, emit the results.
//
this.traverse(dom, '', this.emits('parsed'));
this.traverse(dom, '', this.emits('parsed:' + id));
};

/**
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"cli-color": "~1.0.0",
"diagnostics": "~1.0.1",
"emits": "~3.0.0",
"htmlparser2": "~3.8.3"
"htmlparser2": "~3.8.3",
"node-uuid": "~1.4.3"
},
"devDependencies": {
"chai": "~3.2.0",
Expand Down
25 changes: 23 additions & 2 deletions test/minimize-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('Minimize', function () {

var second = emit.getCall(1).args;
expect(second).to.be.an('array');
expect(second[0]).to.be.equal('parsed');
expect(second[0]).to.be.include('parsed');
expect(second[1]).to.be.equal(null);
expect(second[2]).to.be.equal('');

Expand Down Expand Up @@ -451,7 +451,7 @@ describe('Minimize', function () {

var result = once.getCall(1).args;
expect(result).to.be.an('array');
expect(result[0]).to.be.equal('parsed');
expect(result[0]).to.be.include('parsed');
expect(result[1]).to.be.equal(fn);
once.restore();
});
Expand All @@ -463,6 +463,27 @@ describe('Minimize', function () {
minimize.parse(html.content, function () {});
parser.restore();
});

it('can handle two calls simultaneously', function (done) {
var minimize = new Minimize
, i = 0;

function next() {
if (i++ >= 1) return done();
}

minimize.parse('<h1 class=>content</h1>', function (error, result) {
expect(error).to.equal(null);
expect(result).to.equal('<h1>content</h1>');
next();
});

minimize.parse('<h1 class=>should be different from above</h1>', function (error, result) {
expect(error).to.equal(null);
expect(result).to.equal('<h1>should be different from above</h1>');
next();
});
});
});

describe('#use', function () {
Expand Down

0 comments on commit d436b8d

Please sign in to comment.