UNPKG

3.24 kBTypeScriptView Raw
1declare module '@ember/owner' {
2 /**
3 Ember’s dependency injection system is built on the idea of an "owner": an
4 object responsible for managing items which can be registered and looked up
5 with the system.
6
7 This module does not provide any concrete instances of owners. Instead, it
8 defines the core type, `Owner`, which specifies the public API contract for an
9 owner. The primary concrete implementations of `Owner` are `EngineInstance`,
10 from `@ember/engine/instance`, and its `ApplicationInstance` subclass, from
11 `@ember/application/instance`.
12
13 Along with `Owner` itself, this module provides a number of supporting types
14 related to Ember's DI system:
15
16 - `Factory`, Ember's primary interface for something which can create class
17 instances registered with the DI system.
18
19 - `FactoryManager`, an interface for inspecting a `Factory`'s class.
20
21 - `Resolver`, an interface defining the contract for the object responsible
22 for mapping string names to the corresponding classes. For example, when you
23 write `@service('session')`, a resolver is responsible to map that back to
24 the `Session` service class in your codebase. Normally, this is handled for
25 you automatically with `ember-resolver`, which is the main implementor of
26 this interface.
27
28 For more details on each, see their per-item docs.
29
30 @module @ember/owner
31 @public
32 */
33 import { type default as Owner } from '@ember/-internals/owner';
34 /**
35 Framework objects in an Ember application (components, services, routes, etc.)
36 are created via a factory and dependency injection system. Each of these
37 objects is the responsibility of an "owner", which handled its
38 instantiation and manages its lifetime.
39
40 `getOwner` fetches the owner object responsible for an instance. This can
41 be used to lookup or resolve other class instances, or register new factories
42 into the owner.
43
44 For example, this component dynamically looks up a service based on the
45 `audioType` passed as an argument:
46
47 ```app/components/play-audio.js
48 import Component from '@glimmer/component';
49 import { action } from '@ember/object';
50 import { getOwner } from '@ember/owner';
51
52 // Usage:
53 //
54 // <PlayAudio @audioType={{@model.audioType}} @audioFile={{@model.file}}/>
55 //
56 export default class extends Component {
57 get audioService() {
58 return getOwner(this)?.lookup(`service:${this.args.audioType}`);
59 }
60
61 @action
62 onPlay() {
63 this.audioService?.play(this.args.audioFile);
64 }
65 }
66 ```
67
68 @method getOwner
69 @static
70 @for @ember/owner
71 @param {Object} object An object with an owner.
72 @return {Object} An owner object.
73 @since 2.3.0
74 @public
75 */
76 const getOwner: (object: object) => Owner | undefined;
77 export { getOwner };
78 export type { Owner as default };
79 export {
80 setOwner,
81 type FullName,
82 type RegisterOptions,
83 type Factory,
84 type FactoryManager,
85 type KnownForTypeResult,
86 type Resolver,
87 type DIRegistry,
88 } from '@ember/-internals/owner';
89}