Environment
Meteor runs most app code within Fibers, which allows keeping track of the context a function is running in. Meteor.EnvironmentVariable
works with Meteor.bindEnvironment
, promises, and many other Meteor API’s to preserve the context in async code. Some examples of how it is used in Meteor are to store the current user in methods, and record which arguments have been checked when using audit-argument-checks
.
const currentRequest = new Meteor.EnvironmentVariable();
function log(message) {
const requestId = currentRequest.get() || 'None';
console.log(`[${requestId}]`, message);
}
currentRequest.withValue('12345', () => {
log('Handling request'); // Logs: [12345] Handling request
});
new Meteor.EnvironmentVariable()
Constructor for EnvironmentVariable
Meteor.EnvironmentVariable.get()
Return value of environment variable if available
Meteor.EnvironmentVariable.withValue(value, func)
Set the environment variable to the given value while a function is run
Arguments
- value Any
-
Value the environment variable should be set to
- func Function
-
The function to run
Meteor.bindEnvironment(func, onException, _this)
Stores the current Meteor environment variables, and wraps the function to run with the environment variables restored. On the server, the function is wrapped within a fiber.
Arguments
- func Function
-
Function that is wrapped
- onException Function
- _this Object
-
Optional
this
object against which the original function will be invoked