Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

noReady functionality for publish #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ const defaultOptions = ({
delay: 250,
lookupCollections: {},
clientCollection: collection._name,
noReady: false,
...options
});

export const ReactiveAggregate = function (subscription, collection, pipeline = [], options = {}) {
const ReactiveAggregate = function(subscription, collection, pipeline = [], options = {}) {
// fill out default options
const {
observeSelector, observeOptions, delay, lookupCollections, clientCollection
} = defaultOptions({
collection,
options
});

// run, or re-run, the aggregation pipeline
const throttledUpdate = _.throttle(Meteor.bindEnvironment(() => {
// add and update documents on the client
Expand All @@ -42,13 +43,13 @@ export const 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 })];
Expand All @@ -71,22 +72,22 @@ export const 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;
// 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()));

/**
* 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 || {};
Expand All @@ -102,3 +103,5 @@ export const ReactiveAggregate = function (subscription, collection, pipeline =
});
}
};

export default ReactiveAggregate;
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down