Skip to content

Commit

Permalink
Pass the design doc to module init functions.
Browse files Browse the repository at this point in the history
Module cleanup functions now have a callback that they're
expected to call (if they exist) so they can do long running
cleanup *before* the new version of the module is started.
  • Loading branch information
adamgundy committed Apr 25, 2010
1 parent f3bcf4a commit 8fb2ac0
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions changes/lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ Deligation.prototype.designDocChange = function (dbname, id) {
}

if (id) {
d.cleanup(dbname, id);
request( this.baseurl+dbname+'/'+id, 'GET', {'accept':'application/json'}, function(error, doc) {
d.handleDesignDoc(d.baseurl, dbname, doc);
d.cleanup(dbname, id, function() {
request( d.baseurl+dbname+'/'+id, 'GET', {'accept':'application/json'}, function(error, doc) {
d.handleDesignDoc(d.baseurl, dbname, doc);
});
});
}
}
Expand All @@ -81,7 +82,7 @@ Deligation.prototype.handleDesignDoc = function (baseurl, dbname, doc) {
sys.puts('Cannot import changes listener from '+JSON.stringify(doc._id)+' '+JSON.stringify(error));
} else {
if (module.init) {
module.init( baseurl + dbname );
module.init( baseurl + dbname, doc );
}

if (module.listener) {
Expand All @@ -92,19 +93,27 @@ Deligation.prototype.handleDesignDoc = function (baseurl, dbname, doc) {
})
}
}
Deligation.prototype.cleanup = function (dbname, id) {
Deligation.prototype.cleanup = function (dbname, id, callback) {
var d = this;
var module = d.modules[dbname+'/'+id];
if (module) {
if (module.listener) {
d.changes[dbname].removeListener("change", module.listener)
}

function finishcleanup() {
delete module
delete d.modules[dbname+'/'+id];
callback();
}

if (module.cleanup) {
module.cleanup();
module.cleanup( finishcleanup );
} else {
finishcleanup();
}
delete module
delete d.modules[dbname+'/'+id];
} else {
callback();
}
}

Expand Down

0 comments on commit 8fb2ac0

Please sign in to comment.