diff --git a/packages/@ember/-internals/container/index.ts b/packages/@ember/-internals/container/index.ts index 24100fa997f..f7e49b70623 100644 --- a/packages/@ember/-internals/container/index.ts +++ b/packages/@ember/-internals/container/index.ts @@ -11,5 +11,5 @@ export { getFactoryFor, setFactoryFor, INIT_FACTORY, - DeprecatedStoreInjection, + deprecatedStoreInjections, } from './lib/container'; diff --git a/packages/@ember/-internals/container/lib/container.ts b/packages/@ember/-internals/container/lib/container.ts index 116057e542c..ace4a755e96 100644 --- a/packages/@ember/-internals/container/lib/container.ts +++ b/packages/@ember/-internals/container/lib/container.ts @@ -49,12 +49,8 @@ if (DEBUG) { } } -export class DeprecatedStoreInjection { - store: unknown; - constructor(store: unknown) { - this.store = store; - } -} +export const deprecatedStoreInjections = + DEBUG && globalThis.WeakSet ? new globalThis.WeakSet() : undefined; export interface ContainerOptions { owner?: Owner; @@ -479,8 +475,8 @@ function injectionsFor(container: Container, fullName: string) { let result = buildInjections(container, typeInjections, injections); - if (DEBUG && type === 'route' && result.injections.store) { - result.injections.store = new DeprecatedStoreInjection(result.injections.store); + if (DEBUG && deprecatedStoreInjections && type === 'route' && result.injections.store) { + deprecatedStoreInjections.add(result.injections.store as object); } return result; } diff --git a/packages/@ember/-internals/routing/lib/system/route.ts b/packages/@ember/-internals/routing/lib/system/route.ts index 353382d9326..a18a07c26c2 100644 --- a/packages/@ember/-internals/routing/lib/system/route.ts +++ b/packages/@ember/-internals/routing/lib/system/route.ts @@ -1,4 +1,4 @@ -import { DeprecatedStoreInjection, privatize as P } from '@ember/-internals/container'; +import { deprecatedStoreInjections, privatize as P } from '@ember/-internals/container'; import { addObserver, computed, @@ -2377,7 +2377,7 @@ Route.reopen(ActionHandler, Evented, { }, set(key, value) { - if (DEBUG && value instanceof DeprecatedStoreInjection) { + if (DEBUG && deprecatedStoreInjections?.has(value)) { Object.defineProperty(this, key, { configurable: true, enumerable: false, @@ -2395,7 +2395,7 @@ Route.reopen(ActionHandler, Evented, { }, } ); - return value.store; + return value; }, }); } else { diff --git a/packages/@ember/-internals/routing/tests/system/route_test.js b/packages/@ember/-internals/routing/tests/system/route_test.js index 3c94f0a2099..870fd95656e 100644 --- a/packages/@ember/-internals/routing/tests/system/route_test.js +++ b/packages/@ember/-internals/routing/tests/system/route_test.js @@ -68,7 +68,6 @@ moduleFor( ["@test 'store' can be injected by data persistence frameworks [DEPRECATED]"](assert) { assert.expect(9); - expectDeprecation(); runDestroy(route); let owner = buildOwner(); @@ -93,6 +92,50 @@ moduleFor( route = owner.lookup('route:index'); + expectDeprecation( + `A value for the \`store\` property was injected onto a route via the owner API. Implicit injection via the owner API is now deprecated, please add an explicit injection for this value. If the injected value is a service, consider using the @service decorator.` + ); + assert.equal(route.model({ post_id: 1 }), post, '#model returns the correct post'); + assert.equal(route.findModel('post', 1), post, '#findModel returns the correct post'); + + runDestroy(owner); + } + + ["@test 'store' can be injected by data persistence frameworks but explicitly injected"]( + assert + ) { + assert.expect(9); + runDestroy(route); + + let owner = buildOwner(); + + let post = { + id: 1, + }; + + let Store = EmberObject.extend({ + find(type, value) { + assert.ok(true, 'injected model was called'); + assert.equal(type, 'post', 'correct type was called'); + assert.equal(value, 1, 'correct value was called'); + return post; + }, + }); + + owner.register( + 'route:index', + EmberRoute.extend({ + store: injectService(), + }) + ); + owner.register('service:store', Store); + + owner.inject('route', 'store', 'service:store'); + + route = owner.lookup('route:index'); + + expectNoDeprecation(); + assert.equal(route.model({ post_id: 1 }), post, '#model returns the correct post'); assert.equal(route.findModel('post', 1), post, '#findModel returns the correct post');