流星方法是如何执行的?

How Meteor Methods are executed?

接下来 http://www.angular-meteor.com/tutorials/socially/angular2/meteor-methods

拥有:

Meteor.methods({
    invite: function(partyId: string, userId: string) {
        check(partyId, String);
        check(userId, String);

        let party = Parties.findOne(partyId);

        if (!party)
            throw new Meteor.Error('404', 'No such party!');

        if (party.public)
            throw new Meteor.Error('400', 'That party is public. No need to invite people.');

        if (party.owner !== this.userId)
            throw new Meteor.Error('403', 'No permissions!');

        if (userId !== party.owner && (party.invited || []).indexOf(userId) == -1) {
            Parties.update(partyId, { $addToSet: { invited: userId } });

            let from = getContactEmail(Meteor.users.findOne(this.userId));
            let to = getContactEmail(Meteor.users.findOne(userId));

            if (Meteor.isServer && to) {
                Email.send({
                    from: 'noreply@socially.com',
                    to: to,
                    replyTo: from || undefined,
                    subject: 'PARTY: ' + party.name,
                    text: `Hi, I just invited you to ${party.name} on Socially.
                        \n\nCome check it out: ${Meteor.absoluteUrl()}\n`
                });
            }
        }
    }
})

然后在派对中-Detail.ts我们有

invite(user:Meteor.User) {
        this.call('invite', this.party._id, user._id, (error) => {
            if (error) {
                alert(`Failed to invite due to ${error}`);
                return;
            }

            alert('User successfully invited.');
        });

    }

当用户点击Invite时代码是如何执行的?

同时在客户端和服务器端?

假设您的 Meteor.methods() 在您的 /lib 文件夹中,那么当调用一个方法时:

  1. 它将首先在客户端上执行,并且 return 结果是异步的,不会引起任何网络延迟。这是一个称为 模拟.
  2. 的过程
  3. 服务器随后将异步执行相同的代码。
  4. 如果服务器上的结果与客户端上的结果不同,服务器将指示客户端更改其结果以匹配。 (服务器总是赢)。

整体效果称为延迟补偿。客户端得到即时结果的错觉(模拟),而服务器有时间在后台赶上。