Permalink
Browse files

Add Job element.

  • Loading branch information...
1 parent ae140ed commit 53f85729639ad7a6dd2c30dbc88ecd289fa8a166 @armetiz committed Aug 29, 2012
Showing with 56 additions and 28 deletions.
  1. +20 −4 README.md
  2. +27 −0 job.js
  3. +9 −24 printer.js
View
@@ -6,12 +6,28 @@ A tool to print document or data. Based on "lp" binary.
## Quick Examples
```js
-var printer = require ("printer");
+var printer = require ("./src/printer");
var options = {
destination: "EPSON_SX510",
- verbose: true
};
-printer.printText("package.json", options);
-printer.printFile("package.json", options);
+var text = "package.json";
+var file = "package.json";
+
+var jobText = printer.printText(text, options, "text_demo");
+var jobFile = printer.printFile(file, options, "file_demo");
+
+var onJobEnd = function () {
+ console.log(this.identifier + ", job finish");
+};
+
+var onJobError = function (message) {
+ console.log(this.identifier + ", error: " + message);
+};
+
+jobText.on("end", onJobEnd);
+jobText.on("error", onJobError);
+
+jobFile.on("end", onJobEnd);
+jobFile.on("error", onJobError);
```
View
27 job.js
@@ -0,0 +1,27 @@
+var utils = require("util");
+var events = require("events");
+
+var Job = function (lp, identifier) {
+
+ var self = new events.EventEmitter();
+ var error;
+
+ self.identifier = identifier;
+
+ lp.stderr.on("data", function(data) {
+ error = data.slice(0, data.length - 1);
+ });
+
+ lp.on("exit", function(code) {
+ if (0 === code) {
+ self.emit("end");
+ }
+ else {
+ self.emit("error", error);
+ }
+ });
+
+ return self;
+}
+
+module.exports = Job;
View
@@ -1,3 +1,4 @@
+var Job = require ("./job");
var spawn = require("child_process").spawn;
var _ = require ("underscore");
@@ -11,7 +12,6 @@ var optionsFactory = function (options) {
defaultOptions.hostname = null;
defaultOptions.numCopies = 1;
defaultOptions.priority = 1;
- defaultOptions.verbose = false;
return _.defaults(options, defaultOptions);
};
@@ -55,42 +55,27 @@ var argsFactory = function (options) {
return args;
};
-var stdioFactory = function (options) {
- var stdio = [];
-
- if (true === options.verbose) {
- stdio.push(null);
- stdio.push(process.stdout);
- stdio.push(process.stderr);
- }
- else {
- stdio.push(null);
- stdio.push(null);
- stdio.push(null);
- }
-
- return stdio;
-};
-
-module.exports.printText = function (text, options) {
+module.exports.printText = function (text, options, identifier) {
options = optionsFactory(options);
- var stdio = stdioFactory(options);
var args = argsFactory(options);
- var lp = spawn("lp", args, {stdio: stdio});
+ var lp = spawn("lp", args);
lp.stdin.write(text);
lp.stdin.end();
+
+ return new Job(lp, identifier);
}
-module.exports.printFile = function (file, options) {
+module.exports.printFile = function (file, options, identifier) {
options = optionsFactory(options);
- var stdio = stdioFactory(options);
var args = argsFactory(options);
args.push ("--");
args.push (file);
- var lp = spawn("lp", args, {stdio: stdio});
+ var lp = spawn("lp", args);
+
+ return new Job(lp, identifier);
}

0 comments on commit 53f8572

Please sign in to comment.