1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-08 23:25:51 +01:00
verdaccio/lib/transaction.js
2013-10-26 15:43:42 +04:00

47 lines
1.1 KiB
JavaScript

var async = require('async')
//
// Function performs a certain task on a multiple uplinks
// and reverts changes if something fails
//
// uplinks - list of uplinks not counting local
// localAction, localRollback - function(cb)
// remoteAction, remoteRollback - function(uplink, cb)
//
module.exports = function(uplinks, localAction, localRollback, remoteAction, remoteRollback, callback) {
var uplink_ids = uplinks.map(function(_, i) {
return i
})
localAction(function(err) {
if (err) return callback(err)
async.map(uplink_ids, function(i, cb) {
remoteAction(uplinks[i], function(err) {
cb(null, err)
})
}, function(err, res) {
var return_err = err
// let err be first non-null element in the array
for (var i=0; i<res.length; i++) {
if (return_err) break
return_err = res[i]
}
if (!return_err) return callback()
async.map(uplink_ids, function(i, cb) {
if (res[i]) return cb()
remoteRollback(uplinks[i], function() {
cb()
})
}, function(err) {
localRollback(function() {
callback(return_err)
})
})
})
})
}