meteorhacks / unblock

this.unblock inside publications :D
MIT License
87 stars 14 forks source link

Question: Always unblock? #2

Open aldeed opened 9 years ago

aldeed commented 9 years ago

I'm trying to think of a case when I would not want to unblock my publish function. The only case would be if I'm using the published data to pass as an argument (from the client) to another publish function. However, if I'm doing that, I would always have that code in an autorun, so it will rerun once the data from the first publish function arrives.

So I think it's safe to unblock all functions always, in which case, maybe you could make a version of this package that automatically unblocks all?

Am I overlooking something?

arunoda commented 9 years ago

No you are not :)

I'm unblocking most of my publications. But some of the initial meteor related publications (like hot code reload related) must run with blocking.

What kind of API we are looking for? any ideas?

aldeed commented 9 years ago

Hmm, if reload needs to block, then it might have to wait until MDG improves their pub/sub.

I was thinking I could just meteor add meteorhacks:unblock-all-publish and be done with it. :)

I feel the same way about Meteor methods. I unblock 90% of them, so shouldn't it really unblock by default and have this.block() when blocking is necessary? Oh well.

arunoda commented 9 years ago

this.block() I think that's a good way to think about this issue.

But the real issue is this won't go into core anytime sooner(may be never) how hard we speak :)

lorensr commented 9 years ago

Is the null publication one of the ones that needs blocking? Eg

Meteor.publish null, ->
  if @userId
    Meteor.users.find @userId,
      fields: 
        services: 0
        server: 0
  else
    null
arunoda commented 9 years ago

Hmm. Nope. You should not write null publications at all.

lorensr commented 9 years ago

I thought this is the universal subscription that's in every app to get user data?

http://support.kadira.io/knowledgebase/articles/379961-what-is-null-autopublish-publication

arunoda commented 9 years ago

Ah! yep. You mean about the core null publication. Yep, there are using a null publication to send user data.

On Tue, Jul 28, 2015 at 9:53 AM Loren Sands-Ramshaw < notifications@github.com> wrote:

I thought this is the universal subscription that's in every app to get user data?

http://support.kadira.io/knowledgebase/articles/379961-what-is-null-autopublish-publication

— Reply to this email directly or view it on GitHub https://github.com/meteorhacks/unblock/issues/2#issuecomment-125434546.

crapthings commented 6 years ago

for always unblock

export default {

  safeMethods(methods) {
    const _methods = {};

    _.each(methods, (method, methodName) => {
      _methods[methodName] = function () {
        return _isUserLogin(this, arguments, method);
      }
    });

    Meteor.methods(_methods);
  },

  safePublish(name, func) {
    Meteor.publish(name, function () {
      this.unblock();
      return _isUserLogin(this, arguments, func);
    });
  },

  safePublishComposite(name, func) {
    Meteor.publishComposite(name, function () {
      this.unblock();
      return _isUserLogin(this, arguments, func);
    });
  },

  safePublishTransformed(name, func) {
    Meteor.publishTransformed(name, function () {
      this.unblock();
      return _isUserLogin(this, arguments, func);
    });
  },

}

function _isUserLogin(ctx, args, fn) {
  return ctx.userId
    ? fn.bind(ctx)(...args)
    : ctx.error(new Meteor.Error('unauthorized'));
}