Permalink
Browse files

Add the rollback util

  • Loading branch information...
isaacs committed Jun 7, 2010
1 parent fee6f04 commit e39afe83952b4b69d00185bfe42e20456794e4e9
Showing with 47 additions and 0 deletions.
  1. +47 −0 lib/utils/rollback.js
View
@@ -0,0 +1,47 @@
+
+module.exports = rollback
+rollback.register = register
+
+var stack = []
+ , log = require("./log")
+ , inRollback = false
+
+function rollback (cb) {
+ inRollback = true
+ if (stack.length === 0) return log("completed", "rollback", cb)
+ var r = stack.pop()
+ , fn = r.shift()
+ r.push(function (er) {
+ if (er) return log.er(cb, "error during rollback")(er)
+ rollback(cb)
+ })
+ fn.rollback.apply(null, r)
+}
+
+function register () {
+ if (inRollback) return undefined
+ var caller = arguments.callee.caller
+ , args = Array.prototype.slice.call(caller.arguments)
+ , cb = args.pop()
+ if (!caller.rollback) return undefined
+ if (typeof cb !== "function") return log(
+ "Trying to register a rollback from a non-cb function."
+ , "warning")
+ log(caller.name || "(anonymous) ", "rollback register")
+ stack.push([caller].concat(args))
+}
+
+if (module !== process.mainModule) return undefined
+
+function foo (a, b, c, cb) { rollback.register() }
+foo.rollback = function () {
+ log([].slice.call(arguments), "rolling back foo")
+ var cb = Array.prototype.pop.call(arguments)
+ cb()
+}
+
+foo(1,2,3,function () { log("foo") })
+rollback(function (er) {
+ if (er) throw er
+ log("rolled back")
+})

0 comments on commit e39afe8

Please sign in to comment.