From 8a2f8a9c5d570a3ce490a136b1e5d239456bed68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nacho=20Codo=C3=B1er?= Date: Thu, 12 Sep 2024 16:21:36 +0200 Subject: [PATCH] add env.variables properly on breaking changes --- .../v3-migration-docs/.vitepress/config.mts | 1 - .../api/environment-variables.md | 50 ------------------- .../breaking-changes/index.md | 50 +++++++++++++++++++ 3 files changed, 50 insertions(+), 51 deletions(-) delete mode 100644 v3-docs/v3-migration-docs/api/environment-variables.md diff --git a/v3-docs/v3-migration-docs/.vitepress/config.mts b/v3-docs/v3-migration-docs/.vitepress/config.mts index 0901426e6b..5725965a6b 100644 --- a/v3-docs/v3-migration-docs/.vitepress/config.mts +++ b/v3-docs/v3-migration-docs/.vitepress/config.mts @@ -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"}, ] }, { diff --git a/v3-docs/v3-migration-docs/api/environment-variables.md b/v3-docs/v3-migration-docs/api/environment-variables.md deleted file mode 100644 index bf7e0b2db0..0000000000 --- a/v3-docs/v3-migration-docs/api/environment-variables.md +++ /dev/null @@ -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). diff --git a/v3-docs/v3-migration-docs/breaking-changes/index.md b/v3-docs/v3-migration-docs/breaking-changes/index.md index 90da82e95c..cfaf9d3ef1 100644 --- a/v3-docs/v3-migration-docs/breaking-changes/index.md +++ b/v3-docs/v3-migration-docs/breaking-changes/index.md @@ -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).