Skip to content
This repository
Browse code

Fixed tasks-and-prompts.js to conform to node 1.1.103, tweaked a few …

…words in closing paragraphs of tasks-and-prompts.markdown
  • Loading branch information...
commit da58d120f5f8e9420dae771dd5e41e3b5747b210 1 parent 58f4e63
R. S. Doiel authored August 06, 2010
4  articles/tasks-and-prompts.markdown
Source Rendered
... ...
@@ -1,7 +1,7 @@
1 1
 Title: Tasks and Prompts -- Implementing Simple Work Queues
2 2
 Author: R. S. Doiel
3 3
 Date: Sun, 28 Feb 2010 00:50:22 GMT
4  
-Node: v0.1.91
  4
+Node: v0.1.103
5 5
 
6 6
 
7 7
 Sometimes all you really need is orderly execution not blocking I/O to get the job done.  Tasks and prompts is a light weight implementation of the work queue design pattern.
@@ -27,6 +27,6 @@ A simple JavaScript array works fine as a first in first out queue (i.e. shift()
27 27
 
28 28
 ## Explanations
29 29
 
30  
-In this simple example I prompt the user to answer a question then count to three. Pretty trivial but it's an implantation of a the work queue pattern. That pattern is common in installation or management scripts.  The only problem with the example above is that it isn't setup as a node module ... wait I did that already! See [github.com/rsdoiel/nshtools](http://github.com/rsdoiel/nshtools). nshtools.js has a more elaborate implementation and includes some other features like command line option processing, mixins of sys, path and fs modules, and high level file commands like cp and mv. The github wiki and the README.md file for nshtools have some more short examples for your enjoyment.
  30
+In this simple example I prompt the user to answer a question then count to three. Pretty trivial but it's an implantation of a the work queue pattern. That pattern is common in installation or management scripts.  The only problem with the example above is that it isn't setup as a node module ... wait I did that already! See [github.com/rsdoiel/nshtools](http://github.com/rsdoiel/nshtools). nshtools.js includes a more elaborate implementation and with features like command line option processing and high level file commands like cp and mv. The github wiki and the README.md file for nshtools have some more short examples for your enjoyment.
31 31
 
32 32
 Have fun!
24  articles/tasks-and-prompts/tasks-and-prompts.js
... ...
@@ -1,5 +1,4 @@
1  
-var sys = require('sys'), /* We need the sys module */
2  
-    work_queue = []; /* This holds our first in, first out queue of prompts and tasks */
  1
+var work_queue = []; /* This holds our first in, first out queue of prompts and tasks */
3 2
 
4 3
 /* This is the method we used to put prompted items into the work queue.
5 4
     prompts will pass a response to the callback method. */
@@ -25,7 +24,7 @@ run = function() {
25 24
     while (work_queue.length > 0) {
26 25
       /* If next queued item is a prompt, show the prompt and exit taskRunner. */
27 26
       if (work_queue[0].work_type === 'prompt') {
28  
-        sys.puts(work_queue[0].text);
  27
+        process.stdout.write(work_queue[0].text + "\n");
29 28
         return;
30 29
       }
31 30
       /* If we have a task we show the text associated with the task and
@@ -33,7 +32,7 @@ run = function() {
33 32
           Work queue only insures the order of firing not anything else. */
34 33
       if (work_queue[0].work_type === 'task') {
35 34
         action = work_queue.shift();
36  
-        sys.puts(action.text);
  35
+        process.stdout.write(action.text + "\n");
37 36
         action.callback();
38 37
       }
39 38
     }
@@ -61,19 +60,12 @@ run = function() {
61 60
           taskRunner();
62 61
         }
63 62
         if (work_queue.length === 0) {
64  
-          stdin.end();
  63
+          process.exit(0);
65 64
         }
66 65
       };
67 66
       
68  
-      /* This is the callback for doing all the cleanup. */
69  
-      cleanupHandlers = function () {
70  
-        stdin.removeListener('data', inputHandler);
71  
-        stdin.removeListener('end', cleanupHandlers);
72  
-      };
73  
-      
74  
-      /* Now that we have defined out handlers, add them as listners for data and end events. */
75  
-      stdin.addListener('data', inputHandler);
76  
-      stdin.addListener('end', cleanupHandlers);
  67
+      /* Now that we have defined out handlers, add them as listeners for data and end events. */
  68
+      stdin.on('data', inputHandler);
77 69
     }());
78 70
   }
79 71
 };
@@ -84,14 +76,14 @@ run = function() {
84 76
 
85 77
 /* You might prompt to do some setup before firing your tasks */
86 78
 prompt("Are you there? ", function (data) {
87  
-  sys.puts("Answer was: " + data);
  79
+  process.stdout.write("Answer was: " + data + "\n");
88 80
 }); 
89 81
 
90 82
 /* Tasks run unattended. work_queue only ensures the order 
91 83
     the callbacks are fired not order of completion */
92 84
 task('Count to three:', function () {
93 85
   for (i = 1; i <= 3; i += 1) {
94  
-    sys.puts("count: " + i);
  86
+    process.stdout.write("count: " + i + "\n");
95 87
   }
96 88
 });
97 89
 

0 notes on commit da58d12

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