add env.variables properly on breaking changes

This commit is contained in:
Nacho Codoñer
2024-09-12 16:21:36 +02:00
parent b15d71fbef
commit 8a2f8a9c5d
3 changed files with 50 additions and 51 deletions

View File

@@ -44,7 +44,6 @@ export default defineConfig({
{text: "Renamed Functions", link: "/api/renamed-functions"},
{text: "Removed Functions", link: "/api/removed-functions"},
{text: "Using Async Functions", link: "/api/async-functions"},
{text: "Environment Variables", link: "/api/environment-variables"},
]
},
{

View File

@@ -1,50 +0,0 @@
# Environment Variables
Meteor provides the `Meteor.EnvironmentVariable` class, which helps maintain context across different boundaries.
With Meteor 3.0, we added support for asynchronous flows and improved how context is managed. As a result, some packages began losing context data, as described [in this issue](https://github.com/meteor/meteor/issues/13258).
If your app or package uses `EnvironmentVariable`, make sure to use `EnvironmentVariable.withValue` at the top level to correctly preserve and propagate the context.
For instance, when updating publish behavior and introducing a `new EnvironmentVariable` context, you need to adjust your code as follows:
```javascript
const _publishConnectionId = new Meteor.EnvironmentVariable<
string | undefined
>();
// Before
function patchPublish(publish: typeof Meteor.publish) {
return function (this: typeof Meteor, name, func, ...args) {
return publish.call(
this,
name,
function (...args) {
return _publishConnectionId.withValue(this?.connection?.id, () =>
func.apply(this, args),
);
},
...args,
);
} as typeof Meteor.publish;
}
// After
function patchPublish(publish: typeof Meteor.publish) {
return function (this: typeof Meteor, name, func, ...args) {
return _publishConnectionId.withValue(this?.connection?.id, () => {
return publish.call(
this,
name,
function (...args) {
return func.apply(this, args);
},
...args,
);
});
} as typeof Meteor.publish;
}
```
This example demonstrates the migration applied to the [`universe:i18n` package](https://github.com/vazco/meteor-universe-i18n/pull/191).

View File

@@ -281,3 +281,53 @@ const user = Meteor.user(); // [!code error]
const user = await Meteor.userAsync(); // [!code highlight]
```
## Environment Variables
Meteor provides the `Meteor.EnvironmentVariable` class, which helps maintain context across different boundaries.
With Meteor 3.0, we added support for asynchronous flows and improved how context is managed. As a result, some packages began losing context data, as described [in this issue](https://github.com/meteor/meteor/issues/13258).
If your app or package uses `EnvironmentVariable`, make sure to use `EnvironmentVariable.withValue` at the top level to correctly preserve and propagate the context.
For instance, when updating publish behavior and introducing a `new EnvironmentVariable` context, you need to adjust your code as follows:
```javascript
const _publishConnectionId = new Meteor.EnvironmentVariable<
string | undefined
>();
// Before
function patchPublish(publish: typeof Meteor.publish) {
return function (this: typeof Meteor, name, func, ...args) {
return publish.call( // [!code error]
this,
name,
function (...args) {
return _publishConnectionId.withValue(this?.connection?.id, () =>
func.apply(this, args),
);
},
...args,
); // [!code error]
} as typeof Meteor.publish; // [!code error]
}
// After
function patchPublish(publish: typeof Meteor.publish) {
return function (this: typeof Meteor, name, func, ...args) {
return _publishConnectionId.withValue(this?.connection?.id, () => { // [!code highlight]
return publish.call(
this,
name,
function (...args) {
return func.apply(this, args);
},
...args,
);
}); // [!code highlight]
} as typeof Meteor.publish;
}
```
This example demonstrates the migration applied to the [`universe:i18n` package](https://github.com/vazco/meteor-universe-i18n/pull/191).