From a840d29f60085fa99166b1b9de38c4c2cf419853 Mon Sep 17 00:00:00 2001 From: Emily Stark Date: Wed, 5 Jun 2013 18:45:09 -0700 Subject: [PATCH] Allow Meteor.Collections with name but no connection. Also differentiate LocalCollections based on connection, not just name. --- packages/minimongo/minimongo.js | 9 +++++++- packages/mongo-livedata/collection.js | 17 +++++++++------ .../mongo-livedata/local_collection_driver.js | 21 ++++++++++++++----- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/packages/minimongo/minimongo.js b/packages/minimongo/minimongo.js index 95392946b3..9eab9b0038 100644 --- a/packages/minimongo/minimongo.js +++ b/packages/minimongo/minimongo.js @@ -8,7 +8,8 @@ // LiveResultsSet: the return value of a live query. // @export LocalCollection -LocalCollection = function () { +LocalCollection = function (name) { + this.name = name; this.docs = {}; // _id -> document (also containing id) this._observeQueue = new Meteor._SynchronousQueue(); @@ -194,6 +195,12 @@ LocalCollection.Cursor.prototype.count = function () { return self.db_objects.length; }; +LocalCollection.Cursor.prototype._publishCursor = function (sub) { + var self = this; + var collection = self.collection.name; + return Meteor.Collection._publishCursor(self, sub, collection); +}; + LocalCollection._isOrderedChanges = function (callbacks) { if (callbacks.added && callbacks.addedBefore) throw new Error("Please specify only one of added() and addedBefore()"); diff --git a/packages/mongo-livedata/collection.js b/packages/mongo-livedata/collection.js index 5396c539ed..84f1ce8790 100644 --- a/packages/mongo-livedata/collection.js +++ b/packages/mongo-livedata/collection.js @@ -49,10 +49,15 @@ Meteor.Collection = function (name, options) { "the collection name to turn off this warning.)"); } - // note: nameless collections never have a connection - self._connection = name && (options.connection || - (Meteor.isClient ? - Meteor.default_connection : Meteor.default_server)); + if (! name || options.connection === null) + // note: nameless collections never have a connection + self._connection = null; + else if (options.connection) + self._connection = options.connection; + else if (Meteor.isClient) + self._connection = Meteor.default_connection; + else + self._connection = Meteor.default_server; if (!options._driver) { if (name && self._connection === Meteor.default_server && @@ -62,10 +67,10 @@ Meteor.Collection = function (name, options) { options._driver = Meteor._LocalCollectionDriver; } - self._collection = options._driver.open(name); + self._collection = options._driver.open(name, self._connection); self._name = name; - if (name && self._connection.registerStore) { + if (self._connection && self._connection.registerStore) { // OK, we're going to be a slave, replicating some remote // database, except possibly with some temporary divergence while // we have unacknowledged RPC's. diff --git a/packages/mongo-livedata/local_collection_driver.js b/packages/mongo-livedata/local_collection_driver.js index 550689fbcd..6437f726d7 100644 --- a/packages/mongo-livedata/local_collection_driver.js +++ b/packages/mongo-livedata/local_collection_driver.js @@ -1,17 +1,28 @@ // XXX namespacing Meteor._LocalCollectionDriver = function () { var self = this; - self.collections = {}; + self.noConnCollections = {}; +}; + +var ensureCollection = function (name, collections) { + if (!(name in collections)) + collections[name] = new LocalCollection(name); + return collections[name]; }; _.extend(Meteor._LocalCollectionDriver.prototype, { - open: function (name) { + open: function (name, conn) { var self = this; if (!name) return new LocalCollection; - if (!(name in self.collections)) - self.collections[name] = new LocalCollection; - return self.collections[name]; + if (! conn) { + return ensureCollection(name, self.noConnCollections); + } + if (! conn.collections) + conn.collections = {}; + // XXX is there a way to keep track of a connection's collections without + // dangling it off the connection object? + return ensureCollection(name, conn.collections); } });