@@ -6,6 +6,16 @@ import EmberObject from '@ember/object';
66import require , { has } from 'require' ;
77import Ember from 'ember' ;
88
9+ import { FullName } from '@ember/owner' ;
10+
11+ // These shenanigans work around the fact that the import locations are not
12+ // public API and are not stable, so we jump through hoops to get the right
13+ // types and values to use.
14+ import {
15+ ContainerProxyMixin ,
16+ RegistryProxyMixin ,
17+ } from './-owner-mixin-imports' ;
18+
919/**
1020 * Adds methods that are normally only on registry to the container. This is largely to support the legacy APIs
1121 * that are not using `owner` (but are still using `this.container`).
@@ -40,12 +50,14 @@ function exposeRegistryMethodsWithoutDeprecations(container: any) {
4050 }
4151}
4252
43- const RegistryProxyMixin = ( Ember as any ) . _RegistryProxyMixin ;
44- const ContainerProxyMixin = ( Ember as any ) . _ContainerProxyMixin ;
45-
53+ // NOTE: this is the same as what `EngineInstance`/`ApplicationInstance`
54+ // implement, and is thus a superset of the `InternalOwner` contract from Ember
55+ // itself.
56+ interface Owner extends RegistryProxyMixin , ContainerProxyMixin { }
4657const Owner = EmberObject . extend ( RegistryProxyMixin , ContainerProxyMixin , {
4758 _emberTestHelpersMockOwner : true ,
4859
60+ /* eslint-disable valid-jsdoc */
4961 /**
5062 * Unregister a factory and its instance.
5163 *
@@ -57,13 +69,16 @@ const Owner = EmberObject.extend(RegistryProxyMixin, ContainerProxyMixin, {
5769 * @see {@link https://github.com/emberjs/ember.js/pull/12680 }
5870 * @see {@link https://github.com/emberjs/ember.js/blob/v4.5.0-alpha.5/packages/%40ember/engine/instance.ts#L152-L167 }
5971 */
60- unregister ( fullName : string ) {
61- // @ts -expect-error
62- this [ '__container__' ] . reset ( fullName ) ;
72+ /* eslint-enable valid-jsdoc */
73+ unregister ( this : Owner , fullName : FullName ) {
74+ // SAFETY: this is always present, but only the stable type definitions from
75+ // Ember actually preserve it, since it is private API.
76+ ( this as any ) [ '__container__' ] . reset ( fullName ) ;
6377
6478 // We overwrote this method from RegistryProxyMixin.
65- // @ts -expect-error
66- this [ '__registry__' ] . unregister ( fullName ) ;
79+ // SAFETY: this is always present, but only the stable type definitions from
80+ // Ember actually preserve it, since it is private API.
81+ ( this as any ) [ '__registry__' ] . unregister ( fullName ) ;
6782 } ,
6883} ) ;
6984
@@ -72,46 +87,62 @@ const Owner = EmberObject.extend(RegistryProxyMixin, ContainerProxyMixin, {
7287 * @param {Object } resolver the resolver to use with the registry
7388 * @returns {Object } owner, container, registry
7489 */
75- export default function ( resolver : Resolver ) {
76- let fallbackRegistry , registry , container ;
77- let namespace = EmberObject . create ( {
78- // @ts -expect-error
79- Resolver : {
80- create ( ) {
81- return resolver ;
82- } ,
90+ export default function buildRegistry ( resolver : Resolver ) {
91+ let namespace = new Application ( ) ;
92+ // @ts -ignore: this is actually the correcct type, but there was a typo in
93+ // Ember's docs for many years which meant that there was a matching problem
94+ // in the types for Ember's definition of `Engine`. Once we require at least
95+ // Ember 5.1 (in some future breaking change), this ts-ignore can be removed.
96+ namespace . Resolver = {
97+ create ( ) {
98+ return resolver ;
8399 } ,
84- } ) ;
100+ } ;
85101
86- fallbackRegistry = ( Application as any ) . buildRegistry ( namespace ) ;
102+ // @ts -ignore: this is private API.
103+ let fallbackRegistry = Application . buildRegistry ( namespace ) ;
87104 // TODO: only do this on Ember < 3.13
88- fallbackRegistry . register (
89- 'component-lookup:main' ,
90- ( Ember as any ) . ComponentLookup
91- ) ;
105+ // @ts -ignore: this is private API.
106+ fallbackRegistry . register ( 'component-lookup:main' , Ember . ComponentLookup ) ;
92107
93- registry = new ( Ember as any ) . Registry ( {
108+ // @ts -ignore: this is private API.
109+ let registry = new Ember . Registry ( {
94110 fallback : fallbackRegistry ,
95111 } ) ;
96112
97- ( ApplicationInstance as any ) . setupRegistry ( registry ) ;
113+ // @ts -ignore: this is private API.
114+ ApplicationInstance . setupRegistry ( registry ) ;
98115
99116 // these properties are set on the fallback registry by `buildRegistry`
100117 // and on the primary registry within the ApplicationInstance constructor
101118 // but we need to manually recreate them since ApplicationInstance's are not
102119 // exposed externally
120+ // @ts -ignore: this is private API.
103121 registry . normalizeFullName = fallbackRegistry . normalizeFullName ;
122+ // @ts -ignore: this is private API.
104123 registry . makeToString = fallbackRegistry . makeToString ;
124+ // @ts -ignore: this is private API.
105125 registry . describe = fallbackRegistry . describe ;
106126
107127 let owner = Owner . create ( {
108- // @ts -expect-error
128+ // @ts -ignore -- we do not have type safety for `Object.extend` so the type
129+ // of `Owner` here is just `EmberObject`, but we *do* constrain it to allow
130+ // only types from the actual class, so these fields are not accepted.
131+ // However, we can see that they are valid, based on the definition of
132+ // `Owner` above given that it fulfills the `InternalOwner` contract and
133+ // also extends it just as `EngineInstance` does internally.
134+ //
135+ // NOTE: we use an `ignore` directive rather than `expect-error` because in
136+ // *some* versions of the types, we *do* have (at least some of) this
137+ // safety, and maximal backwards compatibility means we have to account for
138+ // that.
109139 __registry__ : registry ,
110140 __container__ : null ,
111- } ) ;
141+ } ) as unknown as Owner ;
112142
113- container = registry . container ( { owner : owner } ) ;
114- // @ts -expect-error
143+ // @ts -ignore: this is private API.
144+ let container = registry . container ( { owner : owner } ) ;
145+ // @ts -ignore: this is private API.
115146 owner . __container__ = container ;
116147
117148 exposeRegistryMethodsWithoutDeprecations ( container ) ;
0 commit comments