Comparing version 1.1.1 to 1.1.2
@@ -14,39 +14,39 @@ var util = require('util') | ||
var cursor = collection.find().sort({ $natural: -1 }).limit(1); | ||
return q.ninvoke(cursor, 'nextObject').then(function(doc) { | ||
if (doc) { | ||
return doc; | ||
var deferred = q.defer(); | ||
cursor.nextObject(function(err, doc) { | ||
if (err) { | ||
deferred.reject(err); | ||
} else if (!doc) { | ||
deferred.reject('empty collection'); | ||
} else { | ||
var deferred = q.defer(); | ||
collection.insert({ dummy: true }, { safe: true }, function(err, docs) { | ||
if (err || !docs || !docs.length) { | ||
deferred.reject('failed to insert the first doc'); | ||
} else { | ||
deferred.resolve(docs[0]); | ||
} | ||
}); | ||
return deferred.promise; | ||
deferred.resolve(doc) | ||
} | ||
}).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 | ||
} | ||
}); | ||
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; | ||
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; | ||
}).fail(function(err) { | ||
console.error('failed to initialize channel', name, client.namespace, err); | ||
self._close(); | ||
self.emit('error', err); | ||
}); | ||
@@ -115,11 +115,22 @@ self.db.collection('channels').insert({ client: self.client.id, channel: self.name, ns: self.client.namespace, ping: new Date() }, { w: 0 }); | ||
var name = 'ns_' + namespace; | ||
function getOrCreateNamespace(cb) { | ||
db.collection(name, { strict: true }, function(err, collection) { | ||
if (err || !collection) { | ||
console.warn('Channel.collection', 'trying to create namespace collection', name); | ||
db.createCollection(name, { | ||
size: Channel.COLLECTION_SIZE, | ||
capped: true, | ||
autoIndexId: true | ||
}, cb); | ||
} else { | ||
cb(null, collection); | ||
} | ||
}); | ||
} | ||
db.collection(name, { strict: true }, function(err, collection) { | ||
getOrCreateNamespace(function(err, collection) { | ||
if (err || !collection) { | ||
console.warn('Channel.collection', 'trying to create namespace collection', name); | ||
db.createCollection(name, { | ||
size: Channel.COLLECTION_SIZE, | ||
capped: true, | ||
autoIndexId: true | ||
}, function(err, collection) { | ||
// Take two... | ||
getOrCreateNamespace(function(err, collection) { | ||
if (err) { | ||
@@ -132,3 +143,3 @@ console.error('Channel.collection', 'failed to create namespace collection', name); | ||
} else { | ||
collection.insert({ init: true }, { w: 1 }, function(err, bootstrap) { | ||
collection.insert({ init: true }, { w: 'majority' }, function(err, bootstrap) { | ||
if (err || !bootstrap) { | ||
@@ -135,0 +146,0 @@ console.error('Channel.collection', 'failed to bootstrap namespace collection', name); |
{ | ||
"name": "vubsub", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Pub/Sub for Node.js and MongoDB", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/vivocha/vubsub", |
Sorry, the diff of this file is not supported yet
11448
227