Created
January 8, 2014 17:56
-
-
Save colllin/8321227 to your computer and use it in GitHub Desktop.
Meteor.js Custom Publish Function example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Meteor.publish('user-stats', function() { | |
var initializing = true; | |
var userStats = []; | |
var aYearAgo = moment().utc().subtract({years: 1}); | |
var aQuarterAgo = moment().utc().subtract({months: 3}); | |
var aMonthAgo = moment().utc().subtract({months: 1}); | |
var defaultAggregation = { | |
wins: 0, | |
yearWins: 0, | |
quarterWins: 0, | |
monthWins: 0 | |
}; | |
var addWin = _.bind(function(document) { | |
var aggregation = _(userStats).findWhere({_id: document.winner_id}); | |
var isNewAggregation = false; | |
if (!aggregation) { | |
isNewAggregation = true; | |
aggregation = { | |
_id: document.winner_id, | |
wins: 0, | |
yearWins: 0, | |
quarterWins: 0, | |
monthWins: 0 | |
}; | |
userStats.push(aggregation); | |
} | |
if (aMonthAgo.isBefore(document.date)) { | |
aggregation.monthWins++; | |
aggregation.quarterWins++; | |
aggregation.yearWins++; | |
} else if (aQuarterAgo.isBefore(document.date)) { | |
aggregation.quarterWins++; | |
aggregation.yearWins++; | |
} else if (aYearAgo.isBefore(document.date)) { | |
aggregation.yearWins++; | |
} | |
aggregation.wins++; | |
if (isNewAggregation) { | |
this.added('users', aggregation._id, aggregation); | |
} else { | |
this.changed('users', aggregation._id, aggregation); | |
} | |
}, this); | |
var subtractWin = _.bind(function(document) { | |
var existingAggregation = _(userStats).findWhere({_id: document.winner_id}); | |
if (!existingAggregation) { | |
// do nothing | |
// this probably indicates a bug if the code gets here | |
return; | |
} | |
if (aMonthAgo.isBefore(document.date)) { | |
existingAggregation.monthWins--; | |
existingAggregation.quarterWins--; | |
existingAggregation.yearWins--; | |
} else if (aQuarterAgo.isBefore(document.date)) { | |
existingAggregation.quarterWins--; | |
existingAggregation.yearWins--; | |
} else if (aYearAgo.isBefore(document.date)) { | |
existingAggregation.yearWins--; | |
} | |
existingAggregation.wins--; | |
this.changed('users', existingAggregation._id, existingAggregation); | |
}, this); | |
// wins depend on matches, so observe changes to the matches collection | |
var liveQuery = Matches.find({}).observe({ | |
added: function(document) { | |
addWin(document); | |
}, | |
changed: function(newDocument, oldDocument) { | |
var changed = function(prop) { | |
return newDocument[prop] != oldDocument[prop]; | |
}; | |
if (changed('winner_id') || changed('date')) { | |
subtractWin(oldDocument); | |
addWin(newDocument); | |
} | |
}, | |
removed: function(document) { | |
subtractWin(document); | |
} | |
}); | |
// Observe only returns after the initial added callbacks have | |
// run. Now mark the subscription as ready. | |
initializing = false; | |
this.ready(); | |
// Stop observing the cursor when client unsubs. | |
// Stopping a subscription automatically takes | |
// care of sending the client any removed messages. | |
this.onStop(function () { | |
liveQuery.stop(); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment