简体   繁体   中英

Deps autorun in Meteor JS

Decided to test out Meteor JS today to see if I would be interested in building my next project with it and decided to start out with the Deps library.

To get something up extremely quick to test this feature out, I am using the 500px API to simulate changes. After reading through the docs quickly, I thought I would have a working example of it on my local box.

The function seems to only autorun once which is not how it is suppose to be working based on my initial understanding of this feature in Meteor.

Any advice would be greatly appreciated. Thanks in advance.

if (Meteor.isClient) {
  var Api500px = {
    dep: new Deps.Dependency,
    get: function () {
      this.dep.depend();
      return Session.get('photos');
    },
    set: function (res) {
      Session.set('photos', res.data.photos);
      this.dep.changed();
    }
  };

  Deps.autorun(function () {
    Api500px.get();
    Meteor.call('fetchPhotos', function (err, res) {
      if (!err) Api500px.set(res);
      else console.log(err);
    });
  });

  Template.photos.photos = function () {
    return Api500px.get();
  };
}

if (Meteor.isServer) {
  Meteor.methods({
    fetchPhotos: function () {
      var url = 'https://api.500px.com/v1/photos';
      return HTTP.call('GET', url, {
        params: {
          consumer_key: 'my_consumer_key_here',
          feature: 'fresh_today',
          image_size: 2,
          rpp: 24
        }
      });
    }
  });
}

Welcome to Meteor! A couple of things to point out before the actual answer...

  1. Session variables have reactivity built in, so you don't need to use the Deps package to add Deps.Dependency properties when you're using them. This isn't to suggest you shouldn't roll your own reactive objects like this, but if you do so then its get and set functions should return and update a normal javascript property of the object (like value , for example), rather than a Session variable, with the reactivity being provided by the depend and changed methods of the dep property. The alternative would be to just use the Session variables directly and not bother with the Api500px object at all.

  2. It's not clear to me what you're trying to achieve reactively here - apologies if it should be. Are you intending to repeatedly run fetchPhotos in an infinite loop, such that every time a result is returned the function gets called again? If so, it's really not the best way to do things - it would be much better to subscribe to a server publication (using Meteor.subscribe and Meteor.publish ), get this publication function to run the API call with whatever the required regularity, and then publish the results to the client. That would dramatically reduce client-server communication with the same net result.

Having said all that, why would it only be running once? The two possible explanations that spring to mind would be that an error is being returned (and thus Api500px.set is never called), or the fact that a Session.set call doesn't actually fire a dependency changed event if the new value is the same as the existing value. However, in the latter case I would still expect your function to run repeatedly as you have your own depend and changed structure surrounding the Session variable, which does not implement that self-limiting logic, so having Api500px.get in the autorun should mean that it reruns when Api500px.set returns even if the Session.set inside it isn't actually doing anything. If it's not the former diagnosis then I'd just log everything in sight and the answer should present itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM