percolatestudio / publish-counts

Meteor package to help you publish the count of a cursor, in real time
https://atmospherejs.com/tmeasday/publish-counts
MIT License
200 stars 46 forks source link

Searching do not update counts #85

Closed humbertocruz closed 8 years ago

humbertocruz commented 8 years ago

Hello,

I trying to add a searching for a collection and it´s starts without any search couting all documents ( 11 ), then a add a searching by name and the count updates to 7, ok until now. Then a remove the search and the count stays showing 7 but the find brings 11 documents again.

boxofrox commented 8 years ago

I'd like to help you @humbertocruz, but your details are just too ambiguous. There are many, many, many ways to implement what you've described, and each with different solutions to this symptom.

Please provide in addition a working code example I can run that demonstrates this symptom. I then have something to analyze, can understand the cause, and provide an explicit answer. Otherwise, I'm just killing time with 20+ questions until I figure out what we're working with.

humbertocruz commented 8 years ago

Hello, ok, sorry, that is what I'm doing:

Counts.publish(this,'allUsuarios',Meteor.users.find(searching)); and searching starts with '{profile.isAdmin: false}' and its ok, later when extra fields is added to searching and IS passed to the subscription the value of count continue the same, the first one.

boxofrox commented 8 years ago

This is not a working code example [1], so let's start the 20 questions.

Let's start with what I think you said, in case I get it wrong.

  1. Your client subscribes to a subscription subA that publishes the count allUsuarios. The client provides searching through the call to Meteor.subscribe('subA', searching).
  2. The client will later subscribe to subA with a searching object with extra parameters. This is done without cancelling the previous subscription for subA or the previous count allUsuarios.

If I recall correcly, Meteor does not cancel existing subscriptions automatically when Meteor.subscribe is called a second time for the same subscription (e.g. subA). Which means two calls leave you with two subscriptions populating one collection. You have to cancel the subscriptions manually if you so choose.

So now we have a model of Meteor's subscriptions. How does publish-counts fit in?

When Meteor.subscribe('subA', searching) is called, Counts.publish will...

  1. Create an observer object on the server to count records in the query's result-set reactively.
  2. Attach to the Meteor.publish data transfer object (i.e. this) so the observer object can send count updates to the client.
  3. When the client receives count updates, publish-counts will store the new count in a single record named allUsuarios in the client-side only Counts collection.

This part is important. That single record named allUsuarios is effectively a global variable. It only holds one value at any given time, and the client now has one subscription updating it reactively from the server.

Then you call Meteor.subscribe('subA', searching) a second time. The same process repeats, and now you have two subscriptions updating a record named allUsuarios. Again the record only holds one value, so which value of the two subscriptions is stored? Apparently the first subscription wins.

How to fix this? You pretty much have only one option here. Cancel the previous Meteor subscription.

// client.js
var handle = Meteor.subscribe('subA', searching);

// later
handle.stop();
handle = Meteor.subscribe('subA', searching);

I hope I understood what your code does and my answer solves your problem. This is more than I care to write on a whim, so if it doesn't fix the problem, then I'm bowing out, and someone else can continue playing 20 questions.

Cheers

[1] http://sscce.org/