DEV: Fix optionalService in decorator form (#29042)

…and remove unused injections
This commit is contained in:
Jarek Radosz 2024-10-01 21:12:34 +09:00 committed by GitHub
parent 6545b06e6b
commit 41c3c5745e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 71 additions and 9 deletions

View File

@ -49,7 +49,7 @@ export default class ReviewableItem extends Component {
@service siteSettings;
@service currentUser;
@service composer;
@optionalService("admin-tools") adminTools;
@optionalService adminTools;
updating = null;
editing = false;

View File

@ -2,7 +2,6 @@ import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
import { service } from "@ember/service";
import optionalService from "discourse/lib/optional-service";
import { bind } from "discourse-common/utils/decorators";
export default class TopicTimeline extends Component {
@ -13,8 +12,6 @@ export default class TopicTimeline extends Component {
@tracked docked = false;
@tracked dockedBottom = false;
adminTools = optionalService();
constructor() {
super(...arguments);

View File

@ -15,7 +15,7 @@ export default class UserController extends Controller.extend(CanCheckEmails) {
@service currentUser;
@service router;
@service dialog;
@optionalService("admin-tools") adminTools;
@optionalService adminTools;
@controller("user-notifications") userNotifications;

View File

@ -2,8 +2,16 @@ import { computed } from "@ember/object";
import { getOwner } from "@ember/owner";
import { dasherize } from "@ember/string";
export default function (name) {
return computed(function (defaultName) {
export default function (target, name, descriptor) {
name ??= target;
const decorator = computed(function (defaultName) {
return getOwner(this).lookup(`service:${name || dasherize(defaultName)}`);
});
if (descriptor) {
return decorator(target, name, descriptor);
} else {
return decorator;
}
}

View File

@ -0,0 +1,59 @@
import Component from "@ember/component";
import Service from "@ember/service";
import { render } from "@ember/test-helpers";
import { hbs } from "ember-cli-htmlbars";
import { module, test } from "qunit";
import optionalService from "discourse/lib/optional-service";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
class FooService extends Service {
name = "foo";
}
class BarService extends Service {
name = "bar";
}
const EmberObjectComponent = Component.extend({
name: "",
layout: hbs`<span class="ember-object-component">{{this.foo.name}} {{this.baz.name}}</span>`,
foo: optionalService(),
baz: optionalService("bar"),
});
class NativeComponent extends Component {
@optionalService foo;
@optionalService("bar") baz;
name = "";
layout = hbs`<span class="native-component">{{this.foo.name}} {{this.baz.name}}</span>`;
}
module("Unit | Utils | optional-service", function (hooks) {
setupRenderingTest(hooks);
hooks.beforeEach(function () {
this.registry.register("service:foo", FooService);
this.registry.register("service:bar", BarService);
});
test("optionalService works in EmberObject classes", async function (assert) {
this.registry.register(
"component:ember-object-component",
EmberObjectComponent
);
await render(hbs`<EmberObjectComponent />`);
assert.dom(".ember-object-component").hasText("foo bar");
});
test("optionalService works in native classes", async function (assert) {
this.registry.register("component:native-component", NativeComponent);
await render(hbs`<NativeComponent />`);
assert.dom(".native-component").hasText("foo bar");
});
});

View File

@ -14,7 +14,6 @@ import { modifier } from "ember-modifier";
import { eq, not } from "truth-helpers";
import DButton from "discourse/components/d-button";
import concatClass from "discourse/helpers/concat-class";
import optionalService from "discourse/lib/optional-service";
import { applyValueTransformer } from "discourse/lib/transformer";
import { updateUserStatusOnMention } from "discourse/lib/update-user-status-on-mention";
import isZoomed from "discourse/lib/zoom-check";
@ -65,7 +64,6 @@ export default class ChatMessage extends Component {
@service router;
@service toasts;
@service modal;
@optionalService adminTools;
@tracked isActive = false;