Sorted out digests in a nice clean way.

This commit is contained in:
Tom Coleman 2012-12-13 18:12:08 +11:00
parent 063b7f85da
commit f36d68451c
4 changed files with 63 additions and 52 deletions

View file

@ -56,7 +56,6 @@ STATUS_PENDING=1;
STATUS_APPROVED=2; STATUS_APPROVED=2;
STATUS_REJECTED=3; STATUS_REJECTED=3;
FIND_APPROVED={$or: [{status: {$exists : false}}, {status: STATUS_APPROVED}]}; FIND_APPROVED={$or: [{status: {$exists : false}}, {status: STATUS_APPROVED}]};
DIGEST_PAGE_PER_PAGE = 5;
var postListSubscription = function(find, options, per_page) { var postListSubscription = function(find, options, per_page) {
var handle = paginatedSubscription(per_page, 'paginatedPosts', find, options); var handle = paginatedSubscription(per_page, 'paginatedPosts', find, options);
@ -75,21 +74,61 @@ var pendingPostsHandle = postListSubscription(
10 10
); );
// setupPostSubscription('digestPosts', { // digest subscriptions
// find: function() { DIGEST_PRELOADING = 3;
// var mDate = moment(Session.get('currentDate')); var digestHandles = {}
// var find = { var dateHash = function(mDate) {
// submitted: { return mDate.format('DD-MM-YYYY');
// $gte: mDate.startOf('day').valueOf(), }
// $lt: mDate.endOf('day').valueOf() var currentMDateForDigest = function() {
// } return moment(Session.get('currentDate')).startOf('day');
// }; }
// find=_.extend(find, FIND_APPROVED); var currentDigestHandle = function() {
// return find; return digestHandles[dateHash(currentMDateForDigest())];
// }, }
// sort: {score: -1}
// ,perPage: DIGEST_PAGE_PER_PAGE // we use autorun here, because we DON'T want meteor to automatically
// }); // unsubscribe for us
Meteor.autorun(function() {
var daySubscription = function(mDate) {
var find = _.extend({
submitted: {
$gte: mDate.startOf('day').valueOf(),
$lt: mDate.endOf('day').valueOf()
}
}, FIND_APPROVED);
var options = {sort: {score: -1}};
// we aren't ever going to paginate this sub, but we'll use pSub
// so we have a reactive loading() function
// (grr... https://github.com/meteor/meteor/pull/273)
return postListSubscription(find, options, 5);
};
// take it to the start of the day.
var mDate = currentMDateForDigest();
var firstDate = moment(mDate).subtract('days', DIGEST_PRELOADING);
var lastDate = moment(mDate).add('days', DIGEST_PRELOADING);
// first unsubscribe all the subscriptions that fall outside of our current range
_.each(digestHandles, function(handle, hash) {
var mDate = moment(hash, 'DD-MM-YYYY');
if (mDate < firstDate || mDate > lastDate) {
console.log('unsubscribing digest for ' + mDate.toString())
handle.stop();
delete digestHandles[dateHash(mDate)];
}
});
// set up a sub for each day for the DIGEST_PRELOADING days before and after
// but we want to be smart about it --
for (mDate = firstDate; mDate < lastDate; mDate.add('days',1 )) {
if (! digestHandles[dateHash(mDate)] && mDate < moment().add('days', 1)) {
console.log('subscribing digest for ' + mDate.toString());
digestHandles[dateHash(mDate)] = daySubscription(mDate);
}
}
});
// ** Categories ** // ** Categories **

View file

@ -13,11 +13,8 @@
Session.set('currentDate', new Date(year, month-1, day)); Session.set('currentDate', new Date(year, month-1, day));
} }
// a manual version of awaitSubscription; the sub can be loading if (currentDigestHandle().loading()) {
// with a new day, but the data is already there (as the subscription is return 'loading';
// for three days)
if (Session.equals('initialLoad', true) || (Session.equals(PAGE_SUBS['post_digest'], false) && postsForSub.digestPosts().length === 0)) {
return 'loading'
} else { } else {
return destination; return destination;
} }

View file

@ -1,9 +1,9 @@
Template.posts_digest.helpers({ Template.posts_digest.helpers({
posts: function(){ posts: function(){
return postsForSub.digestPosts(); return currentDigestHandle().fetch();
}, },
hasPosts: function(){ hasPosts: function(){
return !!postsForSub.digestPosts().length; return ! currentDigestHandle().loading();
}, },
currentDate: function(){ currentDate: function(){
return moment(Session.get('currentDate')).format("dddd, MMMM Do YYYY"); return moment(Session.get('currentDate')).format("dddd, MMMM Do YYYY");

View file

@ -50,10 +50,11 @@ Meteor.startup(function(){
// Posts // Posts
Posts = new Meteor.Collection('posts'); Posts = new Meteor.Collection('posts');
// Meteor.publish('posts', function() {
// return Posts.find({}, {sort: {score: -1}});
// });
// a single post, identified by id
Meteor.publish('post', function(id) {
return Posts.find(id);
});
Meteor.publish('paginatedPosts', function(find, options, limit) { Meteor.publish('paginatedPosts', function(find, options, limit) {
options = options || {}; options = options || {};
@ -62,32 +63,6 @@ Meteor.publish('paginatedPosts', function(find, options, limit) {
return Posts.find(find || {}, options); return Posts.find(find || {}, options);
}); });
Meteor.publish('posts', function(find, options, subName) {
var collection=Posts.find(find, options);
var collectionArray=collection.fetch();
// if this is a single post subscription but no post ID is passed, just return an empty collection
if(subName==="singlePost" && _.isEmpty(find)){
collection=null;
collectionArray=[];
}
// console.log("publishing :"+subName);
// console.log(find, options.sort, options.skip, options.limit);
// console.log('collection.fetch().length '+collectionArray.length);
// for(i=0;i<collectionArray.length;i++){
// console.log('- '+collectionArray[i].headline);
// }
// console.log('\n');
return collection;
});
// a single post, identified by id
Meteor.publish('post', function(id) {
return Posts.find(id);
});
// FIXME -- check all docs, not just the first one. // FIXME -- check all docs, not just the first one.
Meteor.startup(function(){ Meteor.startup(function(){
Posts.allow({ Posts.allow({