Skip to content

Commit

Permalink
Add EventEmitter for queue length changes
Browse files Browse the repository at this point in the history
Remove unnecessary params from Error handler
  • Loading branch information
lovell committed Mar 20, 2015
1 parent 711f0fe commit 81c710e
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 54 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,19 @@ for example:
output: { file: false, buffer: true, stream: true } } }
```

#### queue

An EventEmitter that emits a `change` event when a task is either:

* queued, waiting for _libuv_ to provide a worker thread
* complete

```javascript
sharp.queue.on('change', function(queueLength) {
console.log('Queue contains ' + queueLength + ' task(s)');
});
```

### Input methods

#### sharp([input])
Expand Down
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var path = require('path');
var util = require('util');
var stream = require('stream');
var events = require('events');

var semver = require('semver');
var color = require('color');
Expand Down Expand Up @@ -68,7 +69,11 @@ var Sharp = function(input) {
streamOut: false,
withMetadata: false,
tileSize: 256,
tileOverlap: 0
tileOverlap: 0,
// Function to notify of queue length changes
queueListener: function(queueLength) {
module.exports.queue.emit('change', queueLength);
}
};
if (typeof input === 'string') {
// input=file
Expand All @@ -85,6 +90,11 @@ var Sharp = function(input) {
module.exports = Sharp;
util.inherits(Sharp, stream.Duplex);

/*
EventEmitter singleton emits queue length 'change' events
*/
module.exports.queue = new events.EventEmitter();

/*
Supported image formats
*/
Expand Down
Loading

0 comments on commit 81c710e

Please sign in to comment.