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()
Getter for the current value of the variable, or undefined
if
called from outside a withValue
callback.
Meteor.EnvironmentVariable.withValue(value, func, [options])
takes a value and a function, calls the function with the value set for the duration of the call
Arguments
- value any
-
The value to set for the duration of the function call
- func Function
-
The function to call with the new value of the
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 Async Local Storage.
This function has two reasons:
- Return the function to be executed on the MeteorJS context, having it assigned in Async Local Storage.
- Better error handling, the error message will be more clear.
Arguments
- func Function
-
Function that is wrapped
- onException Function
- _this Object
-
Optional
this
object against which the original function will be invoked