DEV: Make it possible to deprecate plugin outlet properties

This commit is contained in:
Daniel Waterworth
2019-11-20 09:21:33 +00:00
parent b28767f158
commit d541183906
5 changed files with 51 additions and 13 deletions

View File

@ -21,7 +21,8 @@ var define, requirejs;
getProperties: Ember.getProperties,
set: Ember.set,
setProperties: Ember.setProperties,
computed: Ember.computed
computed: Ember.computed,
defineProperty: Ember.defineProperty
},
"@ember/object/computed": {
alias: Ember.computed.alias,

View File

@ -1,5 +1,7 @@
import Component from "@ember/component";
import { observes } from "discourse-common/utils/decorators";
import { defineProperty, computed } from "@ember/object";
import deprecated from "discourse-common/lib/deprecated";
import { buildArgsWithDeprecations } from "discourse/lib/plugin-connectors";
export default Component.extend({
init() {
@ -9,10 +11,30 @@ export default Component.extend({
this.set("layoutName", connector.templateName);
const args = this.args || {};
Object.keys(args).forEach(key => this.set(key, args[key]));
Object.keys(args).forEach(key => {
defineProperty(
this,
key,
computed("args", () => (this.args || {})[key])
);
});
const deprecatedArgs = this.deprecatedArgs || {};
Object.keys(deprecatedArgs).forEach(key => {
defineProperty(
this,
key,
computed("deprecatedArgs", () => {
deprecated(`The ${key} property is deprecated, but is being used in ${this.layoutName}`);
return (this.deprecatedArgs || {})[key];
})
);
});
const connectorClass = this.get("connector.connectorClass");
connectorClass.setupComponent.call(this, args, this);
const merged = buildArgsWithDeprecations(args, deprecatedArgs);
connectorClass.setupComponent.call(this, merged, this);
this.set("actions", connectorClass.actions);
},
@ -24,12 +46,6 @@ export default Component.extend({
connectorClass.teardownComponent.call(this, this);
},
@observes("args")
_argsChanged() {
const args = this.args || {};
Object.keys(args).forEach(key => this.set(key, args[key]));
},
send(name, ...args) {
const connectorClass = this.get("connector.connectorClass");
const action = connectorClass.actions[name];

View File

@ -30,7 +30,7 @@ import Component from "@ember/component";
The list of disabled plugins is returned via the `Site` singleton.
**/
import { renderedConnectorsFor } from "discourse/lib/plugin-connectors";
import { renderedConnectorsFor, buildArgsWithDeprecations } from "discourse/lib/plugin-connectors";
export default Component.extend({
tagName: "span",
@ -46,7 +46,7 @@ export default Component.extend({
this._super(...arguments);
const name = this.name;
if (name) {
const args = this.args;
const args = buildArgsWithDeprecations(this.args || {}, this.deprecatedArgs || {});
this.set("connectors", renderedConnectorsFor(name, args, this));
}
}

View File

@ -1,4 +1,5 @@
import Site from "discourse/models/site";
import deprecated from "discourse-common/lib/deprecated";
let _connectorCache;
let _rawConnectorCache;
@ -109,3 +110,23 @@ export function rawConnectorsFor(outletName) {
}
return _rawConnectorCache[outletName] || [];
}
export function buildArgsWithDeprecations(args, deprecatedArgs) {
const output = {};
Object.keys(args).forEach(key => {
Object.defineProperty(output, key, { value: args[key] });
});
Object.keys(deprecatedArgs).forEach(key => {
Object.defineProperty(output, key, {
get() {
deprecated(`${key} is deprecated`);
return deprecatedArgs[key];
}
});
});
return output;
}

View File

@ -1,3 +1,3 @@
{{#each connectors as |c|}}
{{plugin-connector connector=c args=args class=c.classNames tagName=connectorTagName}}
{{plugin-connector connector=c args=args deprecatedArgs=deprecatedArgs class=c.classNames tagName=connectorTagName}}
{{/each}}