diff --git a/client/app.js b/client/app.js index 7deffc3bb..165bc6f59 100644 --- a/client/app.js +++ b/client/app.js @@ -56,7 +56,6 @@ STATUS_PENDING=1; STATUS_APPROVED=2; STATUS_REJECTED=3; FIND_APPROVED={$or: [{status: {$exists : false}}, {status: STATUS_APPROVED}]}; -DIGEST_PAGE_PER_PAGE = 5; var postListSubscription = function(find, options, per_page) { var handle = paginatedSubscription(per_page, 'paginatedPosts', find, options); @@ -75,21 +74,61 @@ var pendingPostsHandle = postListSubscription( 10 ); -// setupPostSubscription('digestPosts', { -// find: function() { -// var mDate = moment(Session.get('currentDate')); -// var find = { -// submitted: { -// $gte: mDate.startOf('day').valueOf(), -// $lt: mDate.endOf('day').valueOf() -// } -// }; -// find=_.extend(find, FIND_APPROVED); -// return find; -// }, -// sort: {score: -1} -// ,perPage: DIGEST_PAGE_PER_PAGE -// }); +// digest subscriptions +DIGEST_PRELOADING = 3; +var digestHandles = {} +var dateHash = function(mDate) { + return mDate.format('DD-MM-YYYY'); +} +var currentMDateForDigest = function() { + return moment(Session.get('currentDate')).startOf('day'); +} +var currentDigestHandle = function() { + return digestHandles[dateHash(currentMDateForDigest())]; +} + +// 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 ** diff --git a/client/lib/router.js b/client/lib/router.js index 8592816e2..8f1d5faa3 100644 --- a/client/lib/router.js +++ b/client/lib/router.js @@ -13,11 +13,8 @@ Session.set('currentDate', new Date(year, month-1, day)); } - // a manual version of awaitSubscription; the sub can be loading - // with a new day, but the data is already there (as the subscription is - // for three days) - if (Session.equals('initialLoad', true) || (Session.equals(PAGE_SUBS['post_digest'], false) && postsForSub.digestPosts().length === 0)) { - return 'loading' + if (currentDigestHandle().loading()) { + return 'loading'; } else { return destination; } diff --git a/client/views/posts/posts_digest.js b/client/views/posts/posts_digest.js index b99a98f86..0c6b5feef 100644 --- a/client/views/posts/posts_digest.js +++ b/client/views/posts/posts_digest.js @@ -1,9 +1,9 @@ Template.posts_digest.helpers({ posts: function(){ - return postsForSub.digestPosts(); + return currentDigestHandle().fetch(); }, hasPosts: function(){ - return !!postsForSub.digestPosts().length; + return ! currentDigestHandle().loading(); }, currentDate: function(){ return moment(Session.get('currentDate')).format("dddd, MMMM Do YYYY"); diff --git a/server/publish.js b/server/publish.js index 92c44897c..fffee4fe3 100644 --- a/server/publish.js +++ b/server/publish.js @@ -50,10 +50,11 @@ Meteor.startup(function(){ // 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) { options = options || {}; @@ -62,32 +63,6 @@ Meteor.publish('paginatedPosts', function(find, options, limit) { 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