Skip to content

Commit

Permalink
make sure there is consistent ev value across client/server
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardoventurini committed May 9, 2024
1 parent b544f3c commit 08c2d7b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ function @meteor {
}

function @test-package {
@meteor test-packages "$@"
@meteor test-packages "$@" --exclude-archs=web.browser.legacy,web.cordova
}
20 changes: 17 additions & 3 deletions packages/meteor/dynamics_browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,26 @@ EVp.getOrNullIfOutsideFiber = function () {
* @returns {any} The return value of the function
*/
EVp.withValue = function (value, func) {
var saved = currentValues[this.slot];
let ret
let isPromise
const saved = currentValues[this.slot];

try {
currentValues[this.slot] = value;
var ret = func();

ret = func();

isPromise = Meteor._isPromise(ret);

if (isPromise) {
return ret.finally(() => {
currentValues[this.slot] = saved;
});
}
} finally {
currentValues[this.slot] = saved;
if (!isPromise) {
currentValues[this.slot] = saved;
}
}

return ret;
Expand Down
10 changes: 4 additions & 6 deletions packages/meteor/dynamics_nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@ class EnvironmentVariableAsync {
}

return Meteor._runAsync(
async function () {
let ret;
function () {
Meteor._updateAslStore(CURRENT_VALUE_KEY_NAME, value);
Meteor._updateAslStore(UPPER_CALL_DYNAMICS_KEY_NAME, dynamics);
ret = await func();
return ret;
return func();
},
self,
Object.assign(
Expand Down Expand Up @@ -134,10 +132,10 @@ Meteor.EnvironmentVariable = EnvironmentVariableAsync;
/**
* @summary 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.
* function is wrapped within Async Local Storage.
*
* This function has two reasons:
* 1. Return the function to be executed on the MeteorJS context, having it assinged in the async localstorage.
* 1. Return the function to be executed on the MeteorJS context, having it assigned in Async Local Storage.
* 2. Better error handling, the error message will be more clear.
* @locus Anywhere
* @memberOf Meteor
Expand Down
14 changes: 10 additions & 4 deletions packages/meteor/dynamics_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Tinytest.addAsync("environment - bindEnvironment", async function (test) {
test.equal(raised_f, null);

test.equal(await f(true), undefined);
test.equal(raised_f, "test");
test.equal(raised_f, "test", 'raised_f should be "test"');
};

// At top level
Expand All @@ -117,9 +117,9 @@ Tinytest.addAsync("environment - bindEnvironment", async function (test) {

// Inside a withValue

await CurrentFoo.withValue(22, function () {
await CurrentFoo.withValue(22, async function () {
test.equal(CurrentFoo.get(), 22);
test_f();
await test_f();
test.equal(CurrentFoo.get(), 22);
});

Expand Down Expand Up @@ -223,4 +223,10 @@ if (Meteor.isServer) {
test.equal(val1, { name: 'test' });
test.equal(val2, { name: 'test' });
})
}
}

Tinytest.add('environment - consistent ev value', function (test) {
let ev1 = new Meteor.EnvironmentVariable();
const ret = ev1.withValue(10, () => 5);
test.equal(ret, 5);
})

0 comments on commit 08c2d7b

Please sign in to comment.