Skip to content
This repository has been archived by the owner on Nov 25, 2021. It is now read-only.

Commit

Permalink
Fix callback issue. All callbacks work as expected. Added destroy method
Browse files Browse the repository at this point in the history
  • Loading branch information
iSuslov committed Oct 11, 2015
1 parent 84d8377 commit 70cfbba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ var jobFromFile = printer.printFile(filePath, options);
jobFromFile.cancel();

// Listen events from job
jobFromBuffer.once('sent', function() {
jobFromBuffer.on('completed', function() {
console.log('Job ' + jobFromBuffer.identifier + 'has been printed');
jobFromBuffer.removeAllListeners();
});
job.once('sent', function () {
console.log('Job ' + job.identifier + ' has been sent');
});
job.on('completed', function () {
console.log('Job ' + job.identifier + ' has been printed');
job.removeAllListeners();
printer.destroy();
});

//destroys all child processes
printer.destroy()
```

## Options map
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "node-printer",
"description": "Create and manage one or multiple printers (w/ CUPS), send file path or node buffer with support for all lp options. Get feedback on jobs you sent.",
"version": "1.0.3",
"version": "1.0.4",
"homepage": "http://github.com/iSuslov/node-printer",
"keywords": [
"printer",
Expand Down
27 changes: 19 additions & 8 deletions printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ var oOptions = {


var forIn = function (obj, callback) {
if(typeof obj !== "object"){
if (typeof obj !== "object") {
return;
}
var keys = Object.keys(obj);
Expand Down Expand Up @@ -226,7 +226,7 @@ var optionsFactory = function (options) {
}
});
if (typeof oOption.default !== 'undefined') { // if method options does not contains the value, use default
// if available
// if available
if (typeof selOOptions[oOptionId] === 'undefined') {
selOOptions[oOptionId] = oOption.default;
}
Expand Down Expand Up @@ -311,26 +311,37 @@ Printer.match = function (name) {
}).length);
};

Printer.prototype.destroy = function () {
var self = this;
self.lpq.removeAllListeners();
self.lpq.kill();
self.jobs.forEach(function(job){
job.removeAllListeners();
})
}

Printer.prototype.watch = function () {
var self = this;
var args = ['-P', this.name];

var lpq = spawn('lpq', args);
self.lpq = lpq;

lpq.stdout.on('data', function (data) {
data = parseStdout(data);
data.shift(2);
data.shift();
data.shift();

data = data.map(function (line) {
line = line.split(/[ ]{2,}/);
line = line.split(/ +/);
return {
rank: (line[0] === 'active' ? line[0] : parseInt(line[0].slice(0, -2))),
owner: line[1],
identifier: parseInt(line[2]),
files: line[3],
totalSize: line[4]
totalSize: parseInt(line[4])
};
});

self.jobs.map(function (job) {
var status = data.filter(function (status) {
if (status.identifier === job.identifier) return status;
Expand Down Expand Up @@ -362,8 +373,8 @@ Printer.prototype.printBuffer = function (data, options) {

var lp = spawn('lp', args);

lp.stdin.write(data);
lp.stdin.end();
lp.stdin.write(data)
lp.stdin.end()

var job = new Job(lp);
job.on('sent', function () {
Expand Down

0 comments on commit 70cfbba

Please sign in to comment.