public
Created

Meteor Subscription timeline

  • Download Gist
Console output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
at Subscribing to post#undefined; comments are:
at Subscribed; comments are:
at Startup; comments are:
at Subscription complete; comments are:
 
at Setting session var to 1; comments are:
at Set to 1; comments are:
at Subscribing to post#1; comments are:
at Subscribed; comments are:
at Flushed; comments are:
at Subscription complete; comments are: first post on #1 - second post on #1
 
at Setting session var to 2; comments are: first post on #1 - second post on #1
at Set; comments are: first post on #1 - second post on #1
at Subscribing to post#2; comments are: first post on #1 - second post on #1
at Subscribed; comments are: first post on #1 - second post on #1
at Flushed; comments are: first post on #1 - second post on #1
at Subscription complete; comments are: first post on #1 - second post on #1 - first post on #2 - second post on #2
 
at Later; comments are: first post on #2 - second post on #2
timeline-tester.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
Comments = new Meteor.Collection('comments');
// {post_id: '1', text: 'hi there..'}
 
if (Meteor.is_client) {
function log_comments(where) {
var cs = Comments.find({}, {reactive: false}).map(function(c) { return c.text; }).join(' - ') ;
console.log("at " + where + "; comments are: " + cs);
}
Meteor.autosubscribe(function() {
var post_id = Session.get('post_id');
log_comments("Subscribing to post#" + post_id);
Meteor.subscribe('comments', post_id, function() {
log_comments('Subscription complete');
});
log_comments("Subscribed");
})
Meteor.startup(function() {
log_comments('Startup');
// wait long enough for everything to happen
Meteor.setTimeout(function(){
console.log('');
log_comments('Setting session var to 1');
Session.set('post_id', 1);
log_comments('Set to 1');
Meteor.flush()
log_comments('Flushed');
}, 5000);
// and wait even longer
Meteor.setTimeout(function() {
console.log('');
log_comments('Setting session var to 2');
Session.set('post_id', 2);
log_comments('Set');
Meteor.flush()
log_comments('Flushed');
}, 10000)
Meteor.setTimeout(function() {
console.log('');
log_comments('Later');
}, 15000);
});
}
 
if (Meteor.is_server) {
Meteor.publish('comments', function(post_id) {
return Comments.find({post_id: post_id});
});
// add some fixture data
if (Comments.find().count() === 0) {
Comments.insert({post_id: 1, text: 'first post on #1'});
Comments.insert({post_id: 1, text: 'second post on #1'});
Comments.insert({post_id: 2, text: 'first post on #2'});
Comments.insert({post_id: 2, text: 'second post on #2'});
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.