Skip to content

A platform for community discussion. Free, open, simple.

DEV: Make outletArgs available as regular arguments (#32819)

David Taylor3867c879ab80
diff --git a/app/assets/javascripts/discourse/app/components/header/contents.gjs b/app/assets/javascripts/discourse/app/components/header/contents.gjs
index ef81f4704b2..27f45838f6d 100644
--- a/app/assets/javascripts/discourse/app/components/header/contents.gjs
+++ b/app/assets/javascripts/discourse/app/components/header/contents.gjs
@@ -1,5 +1,4 @@
import Component from "@glimmer/component";
-import { hash } from "@ember/helper";
import { service } from "@ember/service";
import { and } from "truth-helpers";
import deprecatedOutletArgument from "discourse/helpers/deprecated-outlet-argument";
@@ -70,7 +69,7 @@ export default class Contents extends Component {
topicInfo=@topicInfo
topicInfoVisible=@topicInfoVisible
}}
- @deprecatedArgs={{hash
+ @deprecatedArgs={{lazyHash
topic=(deprecatedOutletArgument
value=this.header.topic
message="The argument 'topic' is deprecated on the outlet 'header-contents__before', use 'topicInfo' or 'topicInfoVisible' instead"
@@ -122,7 +121,7 @@ export default class Contents extends Component {
topicInfo=@topicInfo
topicInfoVisible=@topicInfoVisible
}}
- @deprecatedArgs={{hash
+ @deprecatedArgs={{lazyHash
topic=(deprecatedOutletArgument
value=this.header.topic
message="The argument 'topic' is deprecated on the outlet 'before-header-panel', use 'topicInfo' or 'topicInfoVisible' instead"
@@ -142,7 +141,7 @@ export default class Contents extends Component {
topicInfo=@topicInfo
topicInfoVisible=@topicInfoVisible
}}
- @deprecatedArgs={{hash
+ @deprecatedArgs={{lazyHash
topic=(deprecatedOutletArgument
value=this.header.topic
message="The argument 'topic' is deprecated on the outlet 'after-header-panel', use 'topicInfo' or 'topicInfoVisible' instead"
@@ -160,7 +159,7 @@ export default class Contents extends Component {
topicInfo=@topicInfo
topicInfoVisible=@topicInfoVisible
}}
- @deprecatedArgs={{hash
+ @deprecatedArgs={{lazyHash
topic=(deprecatedOutletArgument
value=this.header.topic
message="The argument 'topic' is deprecated on the outlet 'header-contents__after', use 'topicInfo' or 'topicInfoVisible' instead"
diff --git a/app/assets/javascripts/discourse/app/components/plugin-outlet.gjs b/app/assets/javascripts/discourse/app/components/plugin-outlet.gjs
index ea2b3ec1342..a47b5f1bff3 100644
--- a/app/assets/javascripts/discourse/app/components/plugin-outlet.gjs
+++ b/app/assets/javascripts/discourse/app/components/plugin-outlet.gjs
@@ -1,9 +1,11 @@
import Component from "@glimmer/component";
import { cached } from "@glimmer/tracking";
+import ClassicComponent from "@ember/component";
import { concat } from "@ember/helper";
import { get } from "@ember/object";
import { getOwner } from "@ember/owner";
import { service } from "@ember/service";
+import curryComponent from "ember-curry-component";
import { or } from "truth-helpers";
import PluginConnector from "discourse/components/plugin-connector";
import PluginOutlet from "discourse/components/plugin-outlet";
@@ -124,6 +126,29 @@ export default class PluginOutletComponent extends Component {
);
}
+ @bind
+ safeCurryComponent(component, args) {
+ if (component.prototype instanceof ClassicComponent) {
+ for (const arg of Object.keys(args)) {
+ if (component.prototype.hasOwnProperty(arg)) {
+ deprecated(
+ `Unable to set @${arg} on connector for ${this.args.name}, because a property on the component class clashes with the argument name. Resolve the clash, or convert to a glimmer component.`,
+ {
+ id: "discourse.plugin-outlet-classic-args-clash",
+ }
+ );
+
+ // Build a clone of `args`, without the offending key, while preserving getters
+ const descriptors = Object.getOwnPropertyDescriptors(args);
+ delete descriptors[arg];
+ args = Object.defineProperties({}, descriptors);
+ }
+ }
+ }
+
+ return curryComponent(component, args, getOwner(this));
+ }
+
<template>
{{~#if (this.connectorsExist hasBlock=(has-block))~}}
{{~#if (has-block)~}}
@@ -135,9 +160,16 @@ export default class PluginOutletComponent extends Component {
{{~#each (this.getConnectors hasBlock=(has-block)) as |c|~}}
{{~#if c.componentClass~}}
- <c.componentClass
- @outletArgs={{this.outletArgsWithDeprecations}}
- >{{yield}}</c.componentClass>
+ {{~#let
+ (this.safeCurryComponent
+ c.componentClass this.outletArgsWithDeprecations
+ )
+ as |CurriedComponent|
+ ~}}
+ <CurriedComponent
+ @outletArgs={{this.outletArgsWithDeprecations}}
+ >{{yield}}</CurriedComponent>
+ {{~/let~}}
{{~else if @defaultGlimmer~}}
<c.templateOnly
@outletArgs={{this.outletArgsWithDeprecations}}
diff --git a/app/assets/javascripts/discourse/app/lib/plugin-connectors.js b/app/assets/javascripts/discourse/app/lib/plugin-connectors.js
index 3d92b0bf71d..468df1217e5 100644
--- a/app/assets/javascripts/discourse/app/lib/plugin-connectors.js
+++ b/app/assets/javascripts/discourse/app/lib/plugin-connectors.js
@@ -237,7 +237,11 @@ export function buildArgsWithDeprecations(args, deprecatedArgs, opts = {}) {
if (args) {
Object.keys(args).forEach((key) => {
- Object.defineProperty(output, key, { value: args[key] });
+ Object.defineProperty(output, key, {
+ get() {
+ return args[key];
+ },
+ });
});
}
diff --git a/app/assets/javascripts/discourse/app/services/deprecation-warning-handler.js b/app/assets/javascripts/discourse/app/services/deprecation-warning-handler.js
index b6a3d7195cb..221e9825699 100644
--- a/app/assets/javascripts/discourse/app/services/deprecation-warning-handler.js
+++ b/app/assets/javascripts/discourse/app/services/deprecation-warning-handler.js
@@ -31,6 +31,7 @@ export const CRITICAL_DEPRECATIONS = [
"discourse.mobile-view",
"discourse.mobile-templates",
"discourse.component-template-overrides",
+ "discourse.plugin-outlet-classic-args-clash",
];
if (DEBUG) {
diff --git a/app/assets/javascripts/discourse/app/static/dev-tools/plugin-outlet-debug/args-table.gjs b/app/assets/javascripts/discourse/app/static/dev-tools/plugin-outlet-debug/args-table.gjs
index 2963a975380..6599d44048d 100644
--- a/app/assets/javascripts/discourse/app/static/dev-tools/plugin-outlet-debug/args-table.gjs
+++ b/app/assets/javascripts/discourse/app/static/dev-tools/plugin-outlet-debug/args-table.gjs
@@ -43,7 +43,7 @@ export default class ArgsTable extends Component {
window[`arg${globalI}`] = value;
/* eslint-disable no-console */
console.log(
- `[plugin outlet debug] \`@outletArgs.${key}\` saved to global \`arg${globalI}\`, and logged below:`
+ `[plugin outlet debug] \`@${key}\` saved to global \`arg${globalI}\`, and logged below:`
);
console.log(value);
/* eslint-enable no-console */
@@ -53,7 +53,7 @@ export default class ArgsTable extends Component {
<template>
{{#each this.renderArgs as |arg|}}
- <div class="key"><span class="fw">{{arg.key}}</span>:</div>
+ <div class="key"><span class="fw">@{{arg.key}}</span>:</div>
<div class="value">
<span class="fw">{{arg.value}}</span>
<a
diff --git a/app/assets/javascripts/discourse/tests/integration/components/plugin-outlet-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/plugin-outlet-test.gjs
index 190f1b1e27e..5b37eaac422 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/plugin-outlet-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/plugin-outlet-test.gjs
@@ -1,6 +1,7 @@
import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
+import ClassicComponent from "@ember/component";
import templateOnly from "@ember/component/template-only";
-import { hash } from "@ember/helper";
import { getOwner } from "@ember/owner";
import { click, find, render, settled } from "@ember/test-helpers";
import hbs from "htmlbars-inline-precompile";
@@ -464,7 +465,7 @@ module("Integration | Component | plugin-outlet", function (hooks) {
<template>
<PluginOutlet
@name="test-name"
- @deprecatedArgs={{hash
+ @deprecatedArgs={{lazyHash
shouldDisplay=(deprecatedOutletArgument value=true)
}}
/>
@@ -492,7 +493,7 @@ module("Integration | Component | plugin-outlet", function (hooks) {
<template>
<PluginOutlet
@name="test-name"
- @deprecatedArgs={{hash
+ @deprecatedArgs={{lazyHash
shouldDisplay=(deprecatedOutletArgument
value=true
message="The 'shouldDisplay' is deprecated on this test"
@@ -555,7 +556,7 @@ module("Integration | Component | plugin-outlet", function (hooks) {
<template>
<PluginOutlet
@name="test-name"
- @deprecatedArgs={{hash
+ @deprecatedArgs={{lazyHash
shouldDisplay=(deprecatedOutletArgument
value=deprecatedData.display
silence="discourse.deprecation-that-should-not-be-logged"
@@ -597,7 +598,7 @@ module("Integration | Component | plugin-outlet", function (hooks) {
<PluginOutlet
@name="test-name"
@outletArgs={{lazyHash shouldDisplay=true}}
- @deprecatedArgs={{hash
+ @deprecatedArgs={{lazyHash
argNotUsed=(deprecatedOutletArgument value=true)
}}
/>
@@ -788,7 +789,7 @@ module(
test("using classic PluginConnector by default", async function (assert) {
await render(hbs`
- <PluginOutlet @name="test-name" @deprecatedArgs={{hash hello=(deprecated-outlet-argument value="world")}} />
+ <PluginOutlet @name="test-name" @deprecatedArgs={{lazyHash hello=(deprecated-outlet-argument value="world")}} />
`);
// deprecated argument still works
@@ -818,7 +819,7 @@ module(
await render(hbs`
<PluginOutlet
@name="test-name"
- @deprecatedArgs={{hash hello=(deprecated-outlet-argument value="world")}}
+ @deprecatedArgs={{lazyHash hello=(deprecated-outlet-argument value="world")}}
@defaultGlimmer={{true}}
/>
`);
@@ -858,7 +859,7 @@ module(
});
await render(hbs`
- <PluginOutlet @name="test-name" @deprecatedArgs={{hash hello=(deprecated-outlet-argument value="world")}} />
+ <PluginOutlet @name="test-name" @deprecatedArgs={{lazyHash hello=(deprecated-outlet-argument value="world")}} />
`);
// deprecated argument still works
@@ -895,7 +896,7 @@ module(
);
await render(hbs`
- <PluginOutlet @name="test-name" @deprecatedArgs={{hash hello=(deprecated-outlet-argument value="world")}} />
+ <PluginOutlet @name="test-name" @deprecatedArgs={{lazyHash hello=(deprecated-outlet-argument value="world")}} />
`);
// deprecated argument still works
@@ -928,7 +929,7 @@ module(
);
await render(hbs`
- <PluginOutlet @name="test-name" @deprecatedArgs={{hash hello=(deprecated-outlet-argument value="world")}} />
+ <PluginOutlet @name="test-name" @deprecatedArgs={{lazyHash hello=(deprecated-outlet-argument value="world")}} />
`);
// deprecated argument still works
@@ -950,7 +951,7 @@ module(
test("unused arguments", async function (assert) {
await render(hbs`
- <PluginOutlet @name="test-name" @outletArgs={{lazyHash hello="world"}} @deprecatedArgs={{hash argNotUsed=(deprecated-outlet-argument value="not used")}} />
+ <PluginOutlet @name="test-name" @outletArgs={{lazyHash hello="world"}} @deprecatedArgs={{lazyHash argNotUsed=(deprecated-outlet-argument value="not used")}} />
`);
// deprecated argument still works
@@ -1058,6 +1059,126 @@ module(
}
);
+module(
+ "Integration | Component | plugin-outlet | argument currying",
+ function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("makes arguments available at top level", async function (assert) {
+ extraConnectorComponent(
+ "test-name",
+ <template>
+ <span class="glimmer-test">{{@arg1}} from glimmer</span>
+ </template>
+ );
+
+ await render(
+ <template>
+ <PluginOutlet
+ @name="test-name"
+ @outletArgs={{lazyHash arg1="Hello world"}}
+ />
+ </template>
+ );
+ assert.dom(".glimmer-test").hasText("Hello world from glimmer");
+ });
+
+ test("doesn't completely rerender when arguments change", async function (assert) {
+ const events = [];
+
+ extraConnectorComponent(
+ "test-name",
+ class extends Component {
+ constructor() {
+ super(...arguments);
+ events.push("constructor");
+ }
+
+ willDestroy() {
+ super.willDestroy(...arguments);
+ events.push("willDestroy");
+ }
+
+ <template>
+ <span class="glimmer-test">{{@arg1}} from glimmer</span>
+ </template>
+ }
+ );
+
+ const testState = new (class {
+ @tracked hello = "Hello world";
+ })();
+
+ await render(
+ <template>
+ <PluginOutlet
+ @name="test-name"
+ @outletArgs={{lazyHash arg1=testState.hello}}
+ />
+ </template>
+ );
+ assert.dom(".glimmer-test").hasText("Hello world from glimmer");
+ assert.deepEqual(events, ["constructor"], "constructor called once");
+
+ testState.hello = "changed";
+ await settled();
+ assert.dom(".glimmer-test").hasText("changed from glimmer");
+ assert.deepEqual(events, ["constructor"], "constructor not called again");
+ });
+
+ test("makes arguments available at top level in classic components", async function (assert) {
+ extraConnectorComponent(
+ "test-name",
+ class extends ClassicComponent {
+ <template>
+ <span class="classic-test">{{this.arg1}} from classic</span>
+ </template>
+ }
+ );
+
+ await render(
+ <template>
+ <PluginOutlet
+ @name="test-name"
+ @outletArgs={{lazyHash arg1="Hello world"}}
+ />
+ </template>
+ );
+ assert.dom(".classic-test").hasText("Hello world from classic");
+ });
+
+ test("guards against name clashes in classic components", async function (assert) {
+ extraConnectorComponent(
+ "test-name",
+ class extends ClassicComponent {
+ get arg1() {
+ return "overridden";
+ }
+
+ <template>
+ <span class="classic-test">{{this.arg1}} from classic</span>
+ </template>
+ }
+ );
+
+ await withSilencedDeprecationsAsync(
+ "discourse.plugin-outlet-classic-args-clash",
+ async () => {
+ await render(
+ <template>
+ <PluginOutlet
+ @name="test-name"
+ @outletArgs={{lazyHash arg1="Hello world"}}
+ />
+ </template>
+ );
+ }
+ );
+ assert.dom(".classic-test").hasText("overridden from classic");
+ });
+ }
+);
+
module(
"Integration | Component | plugin-outlet | whitespace",
function (hooks) {