Latency Compensation

Παράρτημα 7.5

Μεταφρασμένο Ποσοστό

Σε αυτό το κεφάλαιο θα:

  • Understand latency compensation.
  • Slow your app down and see what's going on.
  • Learn how Meteor Methods call each other.
  • ////

    Without latency compensation
    Without latency compensation

    ////

    ////

    • ////
    • ////
    • ////

    ////

    Latency Compensation

    With latency compensation
    With latency compensation

    ////

    ////

    • ////
    • ////
    • ////
    • ////

    ////

    Observing Latency Compensation

    ////

    ////

    ////

    Posts = new Mongo.Collection('posts');
    
    Meteor.methods({
      postInsert: function(postAttributes) {
        check(this.userId, String);
        check(postAttributes, {
          title: String,
          url: String
        });
    
        if (Meteor.isServer) {
          postAttributes.title += "(server)";
          // wait for 5 seconds
          Meteor._sleepForMs(5000);
        } else {
          postAttributes.title += "(client)";
        }
    
        var postWithSameLink = Posts.findOne({url: postAttributes.url});
        if (postWithSameLink) {
          return {
            postExists: true,
            _id: postWithSameLink._id
          }
        }
    
        var user = Meteor.user();
        var post = _.extend(postAttributes, {
          userId: user._id, 
          author: user.username, 
          submitted: new Date()
        });
    
        var postId = Posts.insert(post);
    
        return {
          _id: postId
        };
      }
    });
    
    collections/posts.js

    ////

    ////

    Template.postSubmit.events({
      'submit form': function(e) {
        e.preventDefault();
    
        var post = {
          url: $(e.target).find('[name=url]').val(),
          title: $(e.target).find('[name=title]').val()
        };
    
        Meteor.call('postInsert', post, function(error, result) {
          // display the error to the user and abort
          if (error)
            return alert(error.reason);
    
          // show this result but route anyway
          if (result.postExists)
            alert('This link has already been posted');
    
          Router.go('postPage', {_id: result._id});  
        });
      }
    });
    
    client/templates/posts/post_submit.js

    ////

    ////

    ////

    Template.postSubmit.events({
      'submit form': function(e) {
        e.preventDefault();
    
        var post = {
          url: $(e.target).find('[name=url]').val(),
          title: $(e.target).find('[name=title]').val()
        };
    
        Meteor.call('postInsert', post, function(error, result) {
          // display the error to the user and abort
          if (error)
            return alert(error.reason);
    
          // show this result but route anyway
          if (result.postExists)
            alert('This link has already been posted');
        });
    
        Router.go('postsList');  
    
      }
    });
    
    client/templates/posts/post_submit.js

    Commit 7-5-1

    Demonstrate the order that posts appear using a sleep.

    ////

    Our post as first stored in the client collection
    Our post as first stored in the client collection

    ////

    Our post once the client receives the update from the server collection
    Our post once the client receives the update from the server collection

    Client Collection Methods

    ////

    ////

    //// ////

    Methods Calling Methods

    ////

    ////

    ////

    ////