diff --git a/packages/meteor/dynamics_browser.js b/packages/meteor/dynamics_browser.js index 13133c2776..438857314b 100644 --- a/packages/meteor/dynamics_browser.js +++ b/packages/meteor/dynamics_browser.js @@ -42,6 +42,7 @@ EVp.withValue = function (value, func) { } finally { currentValues[this.slot] = saved; } + return ret; }; diff --git a/packages/meteor/dynamics_nodejs.js b/packages/meteor/dynamics_nodejs.js index bdb147ca83..4a81bf7ba8 100644 --- a/packages/meteor/dynamics_nodejs.js +++ b/packages/meteor/dynamics_nodejs.js @@ -49,20 +49,22 @@ class EnvironmentVariableAsync { */ withValue(value, func, options = {}) { const self = this; - const slotCall = Meteor._getValueFromAslStore(SLOT_CALL_KEY); - const dynamics = - Meteor._getValueFromAslStore(UPPER_CALL_DYNAMICS_KEY_NAME) || {}; - dynamics[slotCall] = Meteor._getValueFromAslStore(CURRENT_VALUE_KEY_NAME); - self.upperCallDynamics = dynamics; + const slotCall = self.slot; + const dynamics = Object.assign( + {}, + Meteor._getValueFromAslStore(UPPER_CALL_DYNAMICS_KEY_NAME) || {} + ); + + if (slotCall) { + dynamics[slotCall] = value; + } + return Meteor._runAsync( async function () { let ret; try { - Meteor._updateAslStore( - UPPER_CALL_DYNAMICS_KEY_NAME, - this.upperCallDynamics, - ); Meteor._updateAslStore(CURRENT_VALUE_KEY_NAME, value); + Meteor._updateAslStore(UPPER_CALL_DYNAMICS_KEY_NAME, dynamics); ret = await func(); } finally { Meteor._updateAslStore(CURRENT_VALUE_KEY_NAME, undefined); @@ -80,7 +82,7 @@ class EnvironmentVariableAsync { [SLOT_CALL_KEY]: this.slot, }, options, - ) + ), ); } diff --git a/packages/meteor/dynamics_test.js b/packages/meteor/dynamics_test.js index c7fe023c85..3d5763e7d1 100644 --- a/packages/meteor/dynamics_test.js +++ b/packages/meteor/dynamics_test.js @@ -16,27 +16,52 @@ Tinytest.add("environment - dynamic variables", function (test) { test.equal(CurrentFoo.get(), undefined); }); -Tinytest.addAsync( - "environment - dynamic variables with two context", - async function (test) { - const context1 = new Meteor.EnvironmentVariable(); - const context2 = new Meteor.EnvironmentVariable(); +if (Meteor.isServer) { + Tinytest.addAsync( + "environment - dynamic variables with two context (server)", + async function (test) { + const context1 = new Meteor.EnvironmentVariable(); + const context2 = new Meteor.EnvironmentVariable(); - await context1.withValue(42, async () => { - test.equal(context2.get(), undefined); - await context2.withValue(1, async () => { - await context2.withValue(2, async () => { - test.equal(context2.get(), 2); + return context1.withValue(42, async () => { + test.equal(context2.get(), undefined); + await context2.withValue(1, async () => { + await context2.withValue(2, async () => { + test.equal(context2.get(), 2); + }); + test.equal(context1.get(), 42); + test.equal(context2.get(), 1); }); test.equal(context1.get(), 42); - test.equal(context2.get(), 1); + test.equal(context2.get(), undefined); }); - test.equal(context1.get(), 42); - test.equal(context2.get(), undefined); - }); - } -); + } + ); +} else { + // Basically the same test as the server one, but without async/await + // as we don't handle async on the client in this case + // due to the idea that we need to keep new EcmaScript features doesn't compile in older browsers + Tinytest.add( + "environment - dynamic variables with two context (client)", + function (test) { + const context1 = new Meteor.EnvironmentVariable(); + const context2 = new Meteor.EnvironmentVariable(); + context1.withValue(42, () => { + test.equal(context2.get(), undefined); + context2.withValue(1, () => { + context2.withValue(2, () => { + test.equal(context2.get(), 2); + }); + test.equal(context1.get(), 42); + test.equal(context2.get(), 1); + }); + test.equal(context1.get(), 42); + test.equal(context2.get(), undefined); + }); + } + ); +} Tinytest.addAsync("environment - bindEnvironment", async function (test) { var raised_f;