Skip to content
Browse files

First

  • Loading branch information...
0 parents commit 0aa8783c7e91d7c495a6c17a90921862a03449ce @quiiver committed Feb 26, 2010
Showing with 55 additions and 0 deletions.
  1. +1 −0 README
  2. +54 −0 magickal.js
1 README
@@ -0,0 +1 @@
+Async-wrapper around the image magick command line tools.
54 magickal.js
@@ -0,0 +1,54 @@
+(function() {
+
+ var sys = require('sys');
+ var Magickal = function() {
+ this.available = 5;
+ this.queue = [];
+ };
+
+ Magickal.prototype = {
+ convert : function(to, from, callback) {
+ this.__run("convert", [to, from], callback);
+ },
+
+ __run : function (cmd, args, callback) {
+ sys.puts("running command: " + cmd + " - " + args.join(" "));
+ if (this.available > 0) {
+ this.available -= 1
+ var p = process.createChildProcess(cmd, args);
+ p.addListener("output", callback);
+ p.addListener("error", this.errorHandler);
+ p.addListener("exit", this.onExit(p));
+ } else {
+ this.queue.push([cmd, args, callback]);
+ }
+ },
+
+ checkQueue : function() {
+ var cmd = this.queue.pop();
+ if (cmd) {
+ this.__run.apply(this, cmd);
+ }
+ },
+
+ onExit : function(proc) {
+ var self = this;
+ return function (data) {
+ self.available += 1;
+ self.checkQueue();
+ sys.puts("Process: " + proc.pid + " finished. " + self.available + ", available");
+ }
+ },
+
+ errorHandler : function(err) {
+ if (err) sys.puts(err);
+ }
+ }
+
+ var magickal = new Magickal;
+
+ var total = 3;
+ for (var i = 0; i < total; i++)
+ magickal.convert("test.png", "converted/test-" + i + ".gif");
+
+})();

0 comments on commit 0aa8783

Please sign in to comment.
Something went wrong with that request. Please try again.