From 04d2d44a0156d46fd006a12d99949a8be3c28515 Mon Sep 17 00:00:00 2001 From: Aaron Thorp Date: Wed, 28 Feb 2018 13:53:35 +1100 Subject: [PATCH 1/2] added noReady function to allow in-publish aggregation similar to tmeasday:publish-counts package --- aggregate.js | 5 +++-- package.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/aggregate.js b/aggregate.js index 12fffd6..5ce2db8 100644 --- a/aggregate.js +++ b/aggregate.js @@ -9,10 +9,11 @@ const defaultOptions = ({ delay: 250, lookupCollections: {}, clientCollection: collection._name, + noReady: false, ...options }); -export const ReactiveAggregate = function (subscription, collection, pipeline = [], options = {}) { +export default ReactiveAggregate = function (subscription, collection, pipeline = [], options = {}) { // fill out default options const { observeSelector, observeOptions, delay, lookupCollections, clientCollection @@ -78,7 +79,7 @@ export const ReactiveAggregate = function (subscription, collection, pipeline = // send an initial result set to the client update(); // mark the subscription as ready - subscription.ready(); + if (!options.noReady) subscription.ready(); // stop observing the cursor when the client unsubscribes subscription.onStop(() => observerHandles.map((handle) => handle.stop())); diff --git a/package.js b/package.js index 26008e1..a3dbff0 100644 --- a/package.js +++ b/package.js @@ -1,6 +1,6 @@ Package.describe({ name: "jcbernack:reactive-aggregate", - version: "1.0.0", + version: "1.0.1", // Brief, one-line summary of the package. summary: "Reactively publish aggregations.", // URL to the Git repository containing the source code for this package. From 1448b6a51e687045fdf6387230769a7b35a5e61a Mon Sep 17 00:00:00 2001 From: Aaron Thorp Date: Thu, 1 Mar 2018 17:28:00 +1100 Subject: [PATCH 2/2] fix exports --- aggregate.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/aggregate.js b/aggregate.js index 5ce2db8..773c649 100644 --- a/aggregate.js +++ b/aggregate.js @@ -13,7 +13,7 @@ const defaultOptions = ({ ...options }); -export default ReactiveAggregate = function (subscription, collection, pipeline = [], options = {}) { +const ReactiveAggregate = function(subscription, collection, pipeline = [], options = {}) { // fill out default options const { observeSelector, observeOptions, delay, lookupCollections, clientCollection @@ -21,7 +21,7 @@ export default ReactiveAggregate = function (subscription, collection, pipeline collection, options }); - + // run, or re-run, the aggregation pipeline const throttledUpdate = _.throttle(Meteor.bindEnvironment(() => { // add and update documents on the client @@ -43,13 +43,13 @@ export default ReactiveAggregate = function (subscription, collection, pipeline subscription._iteration++; }), delay); const update = () => !initializing ? throttledUpdate() : null; - + // don't update the subscription until __after__ the initial hydrating of our collection let initializing = true; // mutate the subscription to ensure it updates as we version it subscription._ids = {}; subscription._iteration = 1; - + // create a list of collections to watch and make sure // we create a sanitized "strings-only" version of our pipeline const observerHandles = [createObserver(collection, { observeSelector, observeOptions })]; @@ -72,7 +72,7 @@ export default ReactiveAggregate = function (subscription, collection, pipeline } return stage; }); - + // observeChanges() will immediately fire an "added" event for each document in the query // these are skipped using the initializing flag initializing = false; @@ -82,12 +82,12 @@ export default ReactiveAggregate = function (subscription, collection, pipeline if (!options.noReady) subscription.ready(); // stop observing the cursor when the client unsubscribes subscription.onStop(() => observerHandles.map((handle) => handle.stop())); - + /** - * Create observer - * @param {Mongo.Collection|*} collection - * @returns {any|*|Meteor.LiveQueryHandle} Handle - */ + * Create observer + * @param {Mongo.Collection|*} collection + * @returns {any|*|Meteor.LiveQueryHandle} Handle + */ function createObserver(collection, queryOptions = {}) { const { observeSelector, observeOptions } = queryOptions; const selector = observeSelector || {}; @@ -103,3 +103,5 @@ export default ReactiveAggregate = function (subscription, collection, pipeline }); } }; + +export default ReactiveAggregate; \ No newline at end of file