2017-11-25 14:45:31 +02:00
|
|
|
import { check } from 'meteor/check';
|
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { Mongo } from 'meteor/mongo';
|
|
|
|
|
|
|
|
import { COUNTS_COLLECTION_CLIENT } from './constants';
|
|
|
|
|
|
|
|
// XXX: Should this persist between server restarts?
|
|
|
|
const collection = new Mongo.Collection(null);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method generates a reactive count endpoint (a method and publication) for a collection or named query.
|
|
|
|
*
|
|
|
|
* @param {String} name - Name of the query or collection
|
|
|
|
* @param {Function} getCursor - Takes in the user's session document as an argument, and turns that into a Mongo cursor.
|
|
|
|
* @param {Function} getSession - Takes the subscribe method's argument as its parameter. Should enforce any necessary security constraints. The return value of this function is stored in the session document.
|
|
|
|
*/
|
|
|
|
export default (name, { getCursor, getSession }) => {
|
|
|
|
Meteor.methods({
|
|
|
|
[name + '.count.subscribe'](paramsOrBody) {
|
|
|
|
const session = getSession.call(this, paramsOrBody);
|
2018-03-20 16:22:53 +02:00
|
|
|
const sessionId = JSON.stringify(session);
|
|
|
|
|
|
|
|
const existingSession = collection.findOne({
|
|
|
|
session: sessionId,
|
|
|
|
userId: this.userId,
|
|
|
|
});
|
2017-11-25 14:45:31 +02:00
|
|
|
|
|
|
|
// Try to reuse sessions if the user subscribes multiple times with the same data
|
|
|
|
if (existingSession) {
|
|
|
|
return existingSession._id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const token = collection.insert({
|
2018-03-20 16:22:53 +02:00
|
|
|
session: sessionId,
|
2017-11-25 14:45:31 +02:00
|
|
|
query: name,
|
|
|
|
userId: this.userId,
|
|
|
|
});
|
|
|
|
|
|
|
|
return token;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Meteor.publish(name + '.count', function(token) {
|
|
|
|
check(token, String);
|
|
|
|
const self = this;
|
|
|
|
const request = collection.findOne({ _id: token, userId: self.userId });
|
|
|
|
|
|
|
|
if (!request) {
|
2018-03-20 16:22:53 +02:00
|
|
|
throw new Error(
|
|
|
|
'no-request',
|
|
|
|
`You must acquire a request token via the "${name}.count.subscribe" method first.`
|
|
|
|
);
|
2017-11-25 14:45:31 +02:00
|
|
|
}
|
|
|
|
|
2018-03-20 16:22:53 +02:00
|
|
|
request.session = JSON.parse(request.session);
|
2017-11-25 14:45:31 +02:00
|
|
|
const cursor = getCursor.call(this, request);
|
|
|
|
|
|
|
|
// Start counting
|
|
|
|
let count = 0;
|
2018-03-20 16:22:53 +02:00
|
|
|
|
|
|
|
let isReady = false;
|
|
|
|
const handle = cursor.observe({
|
|
|
|
added() {
|
2017-11-25 14:45:31 +02:00
|
|
|
count++;
|
2018-03-20 16:22:53 +02:00
|
|
|
isReady &&
|
|
|
|
self.changed(COUNTS_COLLECTION_CLIENT, token, { count });
|
2017-11-25 14:45:31 +02:00
|
|
|
},
|
|
|
|
|
2018-03-20 16:22:53 +02:00
|
|
|
removed() {
|
2017-11-25 14:45:31 +02:00
|
|
|
count--;
|
2018-03-20 16:22:53 +02:00
|
|
|
isReady &&
|
|
|
|
self.changed(COUNTS_COLLECTION_CLIENT, token, { count });
|
2017-11-25 14:45:31 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-03-20 16:22:53 +02:00
|
|
|
isReady = true;
|
|
|
|
self.added(COUNTS_COLLECTION_CLIENT, token, { count });
|
|
|
|
|
2017-11-25 14:45:31 +02:00
|
|
|
self.onStop(() => {
|
|
|
|
handle.stop();
|
|
|
|
collection.remove(token);
|
|
|
|
});
|
2018-03-20 16:22:53 +02:00
|
|
|
|
2017-11-25 14:45:31 +02:00
|
|
|
self.ready();
|
|
|
|
});
|
|
|
|
};
|