Comparing version 1.1.3 to 1.1.4
@@ -11,33 +11,41 @@ var util = require('util') | ||
self.db = client.db; | ||
self.collection = Channel.collection(self.db, client.namespace).then(function(collection) { | ||
var cursor = collection.find().sort({ $natural: -1 }).limit(1); | ||
cursor.nextObject(function(err, doc) { | ||
if (err) { | ||
throw err; | ||
} else if (!doc) { | ||
throw new Error('empty collection'); | ||
return q.ninvoke(cursor, 'nextObject').then(function(doc) { | ||
if (doc) { | ||
return doc; | ||
} else { | ||
// if we've come here, then: | ||
// - the collection exists | ||
// - it had at least a document | ||
// - and we know the _id to the most recent one | ||
var query = { | ||
_id: { $gt: (lastMessage ? mongo.ObjectID(lastMessage) : doc._id) }, | ||
channel: name, | ||
from: { $ne: client.id } | ||
}; | ||
var options = { | ||
tailable: true, | ||
numberOfRetries: -1, | ||
tailableRetryInterval: Channel.TAILABLE_RETRY_INTERVAL | ||
} | ||
self.cursor = self.collection.find(query, options).sort({ $natural: 1 }); | ||
self.cursor.nextObject(function(err, data) { | ||
self._handleNext(err, data); | ||
var deferred = q.defer(); | ||
collection.insert({ init: true }, { w: 'majority' }, function(err, docs) { | ||
if (err || !docs || !docs.length) { | ||
deferred.reject('failed to insert the first doc'); | ||
} else { | ||
deferred.resolve(docs[0]); | ||
} | ||
}); | ||
self.emit('ready', collection); | ||
return collection; | ||
return deferred.promise; | ||
} | ||
}).then(function(doc) { | ||
// if we've come here, then: | ||
// - the collection exists | ||
// - it had at least a document | ||
// - and we know the _id to the most recent one | ||
var query = { | ||
_id: { $gt: (lastMessage ? mongo.ObjectID(lastMessage) : doc._id) }, | ||
channel: name, | ||
from: { $ne: client.id } | ||
}; | ||
var options = { | ||
tailable: true, | ||
numberOfRetries: -1, | ||
tailableRetryInterval: Channel.TAILABLE_RETRY_INTERVAL | ||
} | ||
self.cursor = collection.find(query, options).sort({ $natural: 1 }); | ||
self.cursor.nextObject(function(err, data) { | ||
self._handleNext(err, data); | ||
}); | ||
self.emit('ready', collection); | ||
return collection; | ||
}); | ||
@@ -138,10 +146,3 @@ }).fail(function(err) { | ||
} else { | ||
collection.insert({ init: true }, { w: 'majority' }, function(err, bootstrap) { | ||
if (err || !bootstrap) { | ||
console.error('Channel.collection', 'failed to bootstrap namespace collection', name); | ||
d.reject(new Error('failed to create bootstrap namespace collection')); | ||
} else { | ||
d.resolve(collection); | ||
} | ||
}); | ||
d.resolve(collection); | ||
} | ||
@@ -148,0 +149,0 @@ }); |
@@ -48,3 +48,7 @@ var q = require('q') | ||
clients[client.id] = client; | ||
deferred.resolve(client); | ||
Channel.collection(db, meta.ns).then(function() { | ||
deferred.resolve(client); | ||
}).fail(function(err) { | ||
deferred.reject(err); | ||
}); | ||
} | ||
@@ -51,0 +55,0 @@ }); |
{ | ||
"name": "vubsub", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "Pub/Sub for Node.js and MongoDB", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/vivocha/vubsub", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11444
228