From 2b53c2cfca4921e22f5ad6d69e523bb360a129b3 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 30 Nov 2022 15:53:54 +0000 Subject: [PATCH] DEV: Enable `@cached` decorator for themes and plugins (#19261) `ember-cached-decorator-polyfill` uses a Babel transformation to apply this polyfill in core. Adding that Babel transformation to themes and plugins will be complex, so we use this to patch it at runtime. This can be removed once `@glimmer/tracking` is updated to a version with native `@cached` support. --- .../public/assets/scripts/module-shims.js | 15 +++++++++++++++ .../components/reviewable-chat-message.js | 2 ++ 2 files changed, 17 insertions(+) diff --git a/app/assets/javascripts/discourse/public/assets/scripts/module-shims.js b/app/assets/javascripts/discourse/public/assets/scripts/module-shims.js index a8b8118e99b..16a61fe5154 100644 --- a/app/assets/javascripts/discourse/public/assets/scripts/module-shims.js +++ b/app/assets/javascripts/discourse/public/assets/scripts/module-shims.js @@ -31,3 +31,18 @@ define("ember-jquery-legacy", ["exports"], function (exports) { return e.__originalEvent || e.originalEvent; }; }); + +// ember-cached-decorator-polyfill uses a Babel transformation to apply this polyfill in core. +// Adding that Babel transformation to themes and plugins will be complex, so we use this to +// patch it at runtime. This can be removed once `@glimmer/tracking` is updated to a version +// with native `@cached` support. +const glimmerTracking = require("@glimmer/tracking"); +if (glimmerTracking.cached) { + console.error( + "@glimmer/tracking natively supports the @cached decorator. The polyfill can be removed." + ); +} else { + Object.defineProperty(glimmerTracking, "cached", { + get: () => require("ember-cached-decorator-polyfill").cached, + }); +} diff --git a/plugins/chat/assets/javascripts/discourse/components/reviewable-chat-message.js b/plugins/chat/assets/javascripts/discourse/components/reviewable-chat-message.js index c74e0236a65..1857a1b9f11 100644 --- a/plugins/chat/assets/javascripts/discourse/components/reviewable-chat-message.js +++ b/plugins/chat/assets/javascripts/discourse/components/reviewable-chat-message.js @@ -1,9 +1,11 @@ import Component from "@glimmer/component"; import { inject as service } from "@ember/service"; +import { cached } from "@glimmer/tracking"; export default class ReviewableChatMessage extends Component { @service store; + @cached get chatChannel() { return this.store.createRecord( "chat-channel",