DEV: Rename custom getOwner to getOwnerWithFallback (#23437)

Our custom implementation of `getOwner` includes a fallback which returns an owner, even if the passed object does not have one set. This is confusing and creates a false sense of security. Generally if the fallback is used, it means there is a problem with the patterns being used.

This commit renames our custom implementation to `getOwnerWithFallback`, while maintaining the old `getOwner` export with a deprecation notice. Core code is updated to use the official `@ember/application` implementation, or the new `getOwnerWithFallback` function.

This commit updates all core uses of `{ getOwner } from discourse-common/lib/get-owner` to use `getOwnerWithFallback`. Future commits will work through and convert many of these to use the official `@ember/application` implementation
This commit is contained in:
David Taylor
2023-09-26 14:30:52 +01:00
committed by GitHub
parent 2e950eb07a
commit 8958b4f76a
99 changed files with 487 additions and 381 deletions

View File

@@ -3,7 +3,14 @@ import deprecated from "discourse-common/lib/deprecated";
let _default = {};
export function getOwner(obj) {
/**
* Works similarly to { getOwner } from `@ember/application`, but has a fallback
* when the passed object doesn't have an owner.
*
* This exists for historical reasons. Ideally, any uses of it should be updated to use
* the official `@ember/application` implementation.
*/
export function getOwnerWithFallback(obj) {
if (emberGetOwner) {
return emberGetOwner(obj || _default) || emberGetOwner(_default);
}
@@ -11,6 +18,17 @@ export function getOwner(obj) {
return obj.container;
}
/**
* @deprecated use `getOwnerWithFallback` instead
*/
export function getOwner(obj) {
deprecated(
"Importing getOwner from `discourse-common/lib/get-owner` is deprecated. Use `import { getOwner } from '@ember/application'`, or if you still need the fallback shim, use `import { getOwnerWithFallback } from 'discourse-common/lib/get-owner';`.",
{ since: "3.2", id: "discourse.get-owner-with-fallback" }
);
return getOwnerWithFallback(obj);
}
export function setDefaultOwner(container) {
setOwner(_default, container);
}
@@ -18,7 +36,7 @@ export function setDefaultOwner(container) {
// `this.container` is deprecated, but we can still build a container-like
// object for components to use
export function getRegister(obj) {
const owner = getOwner(obj);
const owner = getOwnerWithFallback(obj);
const register = {
lookup: (...args) => owner.lookup(...args),
lookupFactory: (...args) => {