Skip to content

Chaining expressions after .changes() has been called #21

@rclai

Description

@rclai

I was made aware that after you've called .changes(), you can actually .map(), and even .filter() the changes that come in like this: .changes().filter(...). I posted an issue asking why .get().pluck() wouldn't work and they told me that a workaround would involve having to chain expressions after .changes() to get the functionality you want. This seems to be a common thing to do to work around the limitations of change-feeds.

var fields = r.expr([ 'id', 'b', 'c' ]);
r.table('cities').get(...).changes().filter(function (row) {
  return fields.contains(function (field) {
    // compare the fields we care about
    return row('new_val')(field).ne(row('old_val')(field)).default(true);
  })
});

In terms of publishing a change-feed in Meteor in a simple way, it seems like doing something like the above is going to be tough to pull-off, to make it simple in a .publish(), that is. So if you really wanted to publish something like the above, you would need to write a change-feed manually, like:

Meteor.publish('city', function (id) {
  var self = this;
  var init = true;
  var stream = Cities.get(id).changes().filter(function (row) {
    return fields.contains(function (field) {
      return row('new_val')(field).ne(row('old_val')(field)).default(true);
    })
  }).run().each(function (err, notif) {
    if (err) {
      self.error(err);
    } else if (init) {
      init = false;
      self.added(Cities.name, /* the initial value */)
      self.ready();
    } else {
      // A bunch of logic to do
      // self.added, self.changed and self.removed
    }
  });

  self.onStop(function () {
    stream && stream.close();
  });
});

I wrote this to hopefully discuss how we can make this simpler, since that's what Meteor strives to be.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions