|
3 | 3 | */ |
4 | 4 |
|
5 | 5 | import EmberObject from '@ember/object'; |
| 6 | +import { schedule, join } from '@ember/runloop'; |
6 | 7 | import { RSVP } from '@ember/-internals/runtime'; |
7 | 8 | import { assert } from '@ember/debug'; |
| 9 | +import type { Container } from '@ember/-internals/container'; |
8 | 10 | import { Registry, privatize as P } from '@ember/-internals/container'; |
9 | 11 | import { guidFor } from '@ember/-internals/utils'; |
10 | 12 | import { ENGINE_PARENT, getEngineParent, setEngineParent } from './parent'; |
11 | | -import { ContainerProxyMixin, RegistryProxyMixin } from '@ember/-internals/runtime'; |
12 | | -import type { InternalOwner } from '@ember/-internals/owner'; |
| 13 | +import { RegistryProxyMixin } from '@ember/-internals/runtime'; |
| 14 | +import type { ContainerProxy, InternalOwner, RegisterOptions } from '@ember/-internals/owner'; |
13 | 15 | import type Owner from '@ember/-internals/owner'; |
14 | 16 | import { type FullName, isFactory } from '@ember/-internals/owner'; |
15 | 17 | import type Engine from '@ember/engine'; |
@@ -41,18 +43,21 @@ export interface EngineInstanceOptions { |
41 | 43 | @class EngineInstance |
42 | 44 | @extends EmberObject |
43 | 45 | @uses RegistryProxyMixin |
44 | | - @uses ContainerProxyMixin |
45 | 46 | */ |
46 | 47 |
|
| 48 | +// TODO: Update this comment |
47 | 49 | // Note on types: since `EngineInstance` uses `RegistryProxyMixin` and |
48 | 50 | // `ContainerProxyMixin`, which respectively implement the same `RegistryMixin` |
49 | 51 | // and `ContainerMixin` types used to define `InternalOwner`, this is the same |
50 | 52 | // type as `InternalOwner` from TS's POV. The point of the explicit `extends` |
51 | 53 | // clauses for `InternalOwner` and `Owner` is to keep us honest: if this stops |
52 | 54 | // type checking, we have broken part of our public API contract. Medium-term, |
53 | 55 | // the goal here is to `EngineInstance` simple be `Owner`. |
54 | | -interface EngineInstance extends RegistryProxyMixin, ContainerProxyMixin, InternalOwner, Owner {} |
55 | | -class EngineInstance extends EmberObject.extend(RegistryProxyMixin, ContainerProxyMixin) { |
| 56 | +interface EngineInstance extends RegistryProxyMixin, InternalOwner, Owner {} |
| 57 | +class EngineInstance |
| 58 | + extends EmberObject.extend(RegistryProxyMixin) |
| 59 | + implements ContainerProxy, InternalOwner, Owner |
| 60 | +{ |
56 | 61 | /** |
57 | 62 | @private |
58 | 63 | @method setupRegistry |
@@ -256,6 +261,47 @@ class EngineInstance extends EmberObject.extend(RegistryProxyMixin, ContainerPro |
256 | 261 | this.register(key, singleton, { instantiate: false }); |
257 | 262 | }); |
258 | 263 | } |
| 264 | + |
| 265 | + // Container Proxy |
| 266 | + |
| 267 | + /** |
| 268 | + The container stores state. |
| 269 | +
|
| 270 | + @private |
| 271 | + @property {Ember.Container} __container__ |
| 272 | + */ |
| 273 | + declare __container__: Container; |
| 274 | + |
| 275 | + ownerInjection() { |
| 276 | + return this.__container__.ownerInjection(); |
| 277 | + } |
| 278 | + |
| 279 | + destroy() { |
| 280 | + let container = this.__container__; |
| 281 | + |
| 282 | + if (container) { |
| 283 | + join(() => { |
| 284 | + container.destroy(); |
| 285 | + schedule('destroy', container, 'finalizeDestroy'); |
| 286 | + }); |
| 287 | + } |
| 288 | + |
| 289 | + return super.destroy(); |
| 290 | + } |
259 | 291 | } |
260 | 292 |
|
| 293 | +// MEGAHAX: This is really nasty, but if we don't define the functions this way, we need to provide types. |
| 294 | +// If we provide types, for reasons I don't understand, they somehow break the interface. |
| 295 | +// Adding the methods this way allows us to keep the types defined by the interface. |
| 296 | + |
| 297 | +// @ts-expect-error This is a huge hack to avoid type issues. |
| 298 | +EngineInstance.prototype.lookup = function lookup(fullName: FullName, options?: RegisterOptions) { |
| 299 | + return this.__container__.lookup(fullName, options); |
| 300 | +}; |
| 301 | + |
| 302 | +// @ts-expect-error This is a huge hack to avoid type issues |
| 303 | +EngineInstance.prototype.factoryFor = function factoryFor(fullName: FullName) { |
| 304 | + return this.__container__.factoryFor(fullName); |
| 305 | +}; |
| 306 | + |
261 | 307 | export default EngineInstance; |
0 commit comments