Skip to content
This repository has been archived by the owner on Apr 16, 2019. It is now read-only.

Commit

Permalink
FSM based saver is naive. rolling back
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictarr committed Jun 16, 2011
1 parent 6309c94 commit 53744d6
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions controllers/github.js
Expand Up @@ -4,6 +4,39 @@ var log = console.log
, overwrite = require('../model/overwrite') , overwrite = require('../model/overwrite')


module.exports = function (db,Repo,config) { //I think dependency injection may be an antipattern module.exports = function (db,Repo,config) { //I think dependency injection may be an antipattern
function filter (obj,keys) {
var n = {}
Object.keys(obj).forEach(function (i){
if(!~keys.indexOf(i))
n[i] = obj[i]
})
return n
}

function save (repo) { //pull this out also.
if(repo._id && !repo.saving){
repo.saving = true

db.get('' + repo._id, function (err, doc){

var obj = filter (doc,['_events'])
obj._id = '' + repo._id
obj._rev = doc && doc._rev
obj.time = new Date()
db.save(obj,
function (err, data){
repo._rev = data._rev
repo.saving = null
if(repo.changed){
save(repo)
repo.changed = null
}
})
})
} else {
repo.changed = true
}
}


return function (req,cont){ return function (req,cont){
var payload var payload
Expand Down Expand Up @@ -34,22 +67,21 @@ module.exports = function (db,Repo,config) { //I think dependency injection may


repo.on('change',function (event){ repo.on('change',function (event){
console.log('change',event, repo._id) console.log('change',event, repo._id)
overwrite(db,repo,function (){ save(repo)
console.log("SAVED",event, repo._id)
})
}) })


repo.integrate(function (err,data){ repo.integrate(function (err,data){
//log //log
if(err){//not necessarily the right error message. if(err){//not necessarily the right error message.
return cont({statusCode: 503, error: err, reason: "could not connect to github or npm", data: data}) return cont({statusCode: 503, error: err, reason: "could not connect to github or npm", data: data})
} }
overwrite(db,repo,function (err2,data2){ //save to database. save(db,repo)
//,function (err2,data2){ //save to database.
if(err) if(err)
return cont({statusCode: 503, error: err, reason: "database error", data: data}) return cont({statusCode: 503, error: err, reason: "database error", data: data})


cont(null, data2) cont(null, {ok: true} )
}) //})
}) })
} }
} }

0 comments on commit 53744d6

Please sign in to comment.