Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor: When is this.isSimulation more useful than Meteor.isClient?

Tags:

meteor

Definition of this.isSimulation:

Access inside a method invocation. Boolean value, true if this invocation is a stub.

Definition of Meteor.isClient:

Boolean variable. True if running in client environment.

In what situations would this.isSimulation be useful? Why can't I always just use Meteor.isClient?

I'm using Meteor.isClient to check if a client document is synced with the server using this simple-schema property:

unsynced: {
  type: Boolean,
  autoValue: function () {
    return Meteor.isClient;
  }
}

It seems to be working, but others said use this.isSimulation, except doing that here didn't work for me.

like image 779
Eliezer Steinbock Avatar asked Oct 15 '14 10:10

Eliezer Steinbock


1 Answers

I'm taking Slava's comment and turn it into an answer ;) The idea is that isSimulation may become true anywhere, on the server or the client. It is only useful inside the Method context. Simulation may be used, as Slava said, when a server is acting as the client for another server.

isClient can be used for all kinds of code, not just inside Method. It prevents code from being executed in an environment where it might else throw an error (like using Session on the server).

For latency compensation you oftentimes run the same method on the client and on the server, but if you're inserting data into the database obviously the client can only simulate this. As such it is sometimes helpful to determine whether a simulation is running or the database is actually changed.

In your case you might want to adjust the method to check whether the server saved the document to be more robust. For that I would add an isServer block into the method to set a flag synced to true and set it to false whenever a client changes data. Ideally couple it with a successful update/upsert.

like image 97
Stephan Avatar answered Sep 28 '22 07:09

Stephan