Permalink
Browse files

example files for my "fits on a slide" flow control lib

  • Loading branch information...
0 parents commit b38e4a0538f6ba04d2a2613473a3c071df7e9d67 @isaacs isaacs committed Oct 26, 2010
Showing with 112 additions and 0 deletions.
  1. +16 −0 async-map.js
  2. +16 −0 bind-actor.js
  3. +20 −0 chain.js
  4. +60 −0 non-trivial-example.js
@@ -0,0 +1,16 @@
+module.exports = asyncMap
+function asyncMap (list, fn, cb_) {
+ var n = list.length
+ , results = []
+ , errState = null
+ function cb (er, data) {
+ if (errState) return
+ if (er) return cb(errState = er)
+ results.push(data)
+ if (-- n === 0)
+ return cb_(null, results)
+ }
+ list.forEach(function (l) {
+ fn(l, cb)
+ })
+}
@@ -0,0 +1,16 @@
+module.exports = bindActor
+function bindActor () {
+ var args =
+ Array.prototype.slice.call
+ (arguments) // jswtf.
+ , obj = null
+ , fn
+ if (typeof args[0] === "object") {
+ obj = args.shift()
+ fn = args.shift()
+ if (typeof fn === "string")
+ fn = obj[ fn ]
+ } else fn = args.shift()
+ return function (cb) {
+ fn.apply(obj, args.concat(cb)) }
+}
@@ -0,0 +1,20 @@
+module.exports = chain
+var bindActor = require("./bind-actor.js")
+chain.first = {} ; chain.last = {}
+function chain (things, res, cb) {
+ if (!cb) cb = res , res = []
+ ;(function LOOP (i, len) {
+ if (i >= len) return cb(null,res)
+ if (Array.isArray(things[i]))
+ things[i] = bindActor.apply(null,
+ things[i].map(function(i){
+ return (i===chain.first) ? res[0]
+ : (i===chain.last)
+ ? res[res.length - 1] : i }))
+ if (!things[i]) return LOOP(i + 1, len)
+ things[i](function (er, data) {
+ res.push(er || data)
+ if (er) return cb(er, res)
+ LOOP(i + 1, len)
+ })
+ })(0, things.length) }
@@ -0,0 +1,60 @@
+
+var chain = require("./chain")
+ , asyncMap = require("./async-map")
+ , fs = require("fs")
+ , http = require("http")
+
+function myProgram (cb) {
+ var res = []
+ chain
+ ( [ [fs, "readdir", "./the-directory"]
+ , [readFiles, "./the-directory", chain.last]
+ , [sum, chain.last]
+ , [ping, "POST", "example.com", 80, "/", chain.last]
+ , [fs, "writeFile", "result.txt", chain.last]
+ , [removeFiles, "./the-directory", chain.first]
+ ]
+ , res
+ , cb
+ )
+}
+function removeFiles (dir, files, cb) {
+ asyncMap(files.map(function (f) { return dir+"/"+f }), fs.unlink, cb)
+}
+
+function readFiles (dir, files, cb) {
+ asyncMap(files.map(function (f) { return dir+"/"+f }), fs.readFile, cb)
+}
+function sum (numbers, cb) {
+ return cb(null, numbers.reduce(function(x,y){
+ return (+x.toString()) + (+y.toString())
+ }))
+}
+function ping (method, host, port, path, data, cb) {
+ console.log([method, host, port, path, data], "pinging")
+ data = new Buffer(data.toString())
+ var req = http.createClient(+port, host)
+ .request(method, path, {host:host,"content-length":data.length})
+ req.on("response", function (resp) {
+ var b = ""
+ resp.on("data", function (c) { b += c })
+ resp.on("end", function () {
+ if (resp.statusCode !== 200) {
+ cb(new Error("Failed "+JSON.stringify(resp.headers,0,2)+"\n"+b))
+ } else {
+ cb(null, b)
+ }
+ })
+ })
+ req.end(data)
+}
+
+// run it
+myProgram(function (er, results) {
+ if (er) {
+ console.error("Error\n"+er.stack)
+ process.exit(1)
+ }
+ console.log("It worked: "+results)
+})
+

0 comments on commit b38e4a0

Please sign in to comment.