meteor-reactive-publish icon indicating copy to clipboard operation
meteor-reactive-publish copied to clipboard

Publish re-run on client collections

Open outaTiME opened this issue 10 years ago • 1 comments

Hi pal,

Im getting something wired here, the re-run executes twice the publish added method (which is logical) but in the re-run the record already exist and we need to execute the changed method to update the client record ... im right ?? but how can intercept that ??

If you give me some advice ... it will be welcome :+1:

PD, im in meteor world few weeks ago (maybe im doing something wrong?)

Here the code:

Meteor.reactivePublish('stats', function (sod) {
  check(sod, Number);
  var self = this;
  var userId = this.userId;
  if (userId) {
    var user = Meteor.users.findOne({
      _id: this.userId
    }, {
      reactive: true
    });
    var stats = {
      count: 0,
    };
    var initializing = true;
    var items = user.items;
    if (items) {
      var query = Items.find({
        _id: {$in: items},
        timestamp: {$gt: sod},
      }, {
        sort: { timestamp: -1 }
      });
      var handle = query.observe({
        added: function (document) {
          stats.count += 1;
          if (!initializing) {
            // update record
            self.changed('stats', userId, status);
          }
        }
      });
      // create initial record
      self.added('stats', userId, stats);
      // done
      initializing = false;
      self.ready();
      self.onStop(function () {
        handle.stop();
      });
    } else {
      // pass
      self.ready();
    }
  } else {
    // pass
    self.ready();
  }
});

thks !!

outaTiME avatar Mar 24 '15 05:03 outaTiME

Probably the reactivePublish method must be removed the records added before the re-run.

btw, im using the old school rules (plain publish method with two observers) and all goes flawlessly (but with a lot of code :open_mouth: )

outaTiME avatar Mar 24 '15 18:03 outaTiME