Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Counts.get() should be working for SSR #63

Closed
eXon opened this issue Aug 22, 2015 · 2 comments
Closed

Counts.get() should be working for SSR #63

eXon opened this issue Aug 22, 2015 · 2 comments

Comments

@eXon
Copy link

eXon commented Aug 22, 2015

I'm using React.JS with publish-counts and it's awesome. One problem I have however is I can't have server-rendering because Counts.get() will only work on the client.

There is probably something we could do to make it works on both the client and the server. What do you think?

@boxofrox
Copy link
Contributor

I have mixed feelings.

My first thought was we might cache the counters, but this thought grew too complex to follow. We'd have to ensure that for every subscription of every client, that every counter was uniquely addressable and did not collide. I think the Meteor pub/sub system does most of this work for us, and I'm loath to pull that functionality into publish-counts. I'm in love with its simplicity.

My second thought was perhaps we can resuse the handler with an accessor for the closure variable holding the count. Something like...

Counts.publish = function (self, name, cursor, options) {
  // code: business as usual

  return {
    get: function () {
      return count;
    },
    stop: function () {
      if (handle) {
        handle.stop();
        handle = undefined;
      }
    }
  };
}

...This pushes the implementation for managing multiple counters server-side on the user.

If you're not publishing the count, then you can use the code below server-side to achieve what publish-counts does. But this code is out of the scope of the project since the project is a library that publishes counts.

var Posts = new Mongo.Collection('posts');
var count = Posts.find({}, {fields: {_id: true}}).count();

var toLikes = function (doc) { return doc.likes || 0; };
var sum = function (a, b) { return a + (b || 0); };
var countFromField = Posts.find({}, {fields: {likes: true}}).fetch().map(toLikes).reduce(sum, 0);

var toCommentCount = function (doc) { return doc.commentCount; };
var countFromFieldLength = Posts.find({}, {
  fields: {comments: true},
  transform: function (doc) {
    return {
      commentCount: doc.comments && doc.comments.length ? doc.comments.length
                                                        : 0
    };
  }
}).fetch().map(toCommentCount).reduce(sum, 0);

I'll defer to @percolatestudio/owners on this feature.

@eXon
Copy link
Author

eXon commented Jan 3, 2016

I've made it work with react-router-ssr at least (it should be automatic if you include your package). It's quite easy when using a LocalCollection (minimongo) on the server. Then, it is easy to plug with fast-render.

Thanks for your input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants