-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathindex.ts
128 lines (108 loc) · 3.66 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { getOwner as glimmerGetOwner, setOwner as glimmerSetOwner } from '@glimmer/owner';
/**
@module @ember/application
*/
export interface LookupOptions {
singleton?: boolean;
instantiate?: boolean;
}
export interface FactoryClass {
positionalParams?: string | string[] | undefined | null;
}
export interface Factory<T, C extends FactoryClass | object = FactoryClass> {
class?: C;
name?: string;
fullName?: string;
normalizedName?: string;
create(props?: { [prop: string]: any }): T;
}
export interface EngineInstanceOptions {
mountPoint: string;
routable: boolean;
}
import EngineInstance from '@ember/engine/instance';
export interface Owner {
lookup<T>(fullName: string, options?: LookupOptions): T | undefined;
factoryFor<T, C>(fullName: string, options?: LookupOptions): Factory<T, C> | undefined;
factoryFor(fullName: string, options?: LookupOptions): Factory<any, any> | undefined;
buildChildEngineInstance(name: string, options?: EngineInstanceOptions): EngineInstance;
register<T, C>(fullName: string, factory: Factory<T, C>, options?: object): void;
hasRegistration(name: string, options?: LookupOptions): boolean;
mountPoint?: string;
routable?: boolean;
}
import { enumerableSymbol } from '@ember/-internals/utils';
import { deprecate } from '@ember/debug';
export const LEGACY_OWNER: unique symbol = enumerableSymbol('LEGACY_OWNER') as any;
/**
Framework objects in an Ember application (components, services, routes, etc.)
are created via a factory and dependency injection system. Each of these
objects is the responsibility of an "owner", which handled its
instantiation and manages its lifetime.
`getOwner` fetches the owner object responsible for an instance. This can
be used to lookup or resolve other class instances, or register new factories
into the owner.
For example, this component dynamically looks up a service based on the
`audioType` passed as an argument:
```app/components/play-audio.js
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { getOwner } from '@ember/application';
// Usage:
//
// <PlayAudio @audioType={{@model.audioType}} @audioFile={{@model.file}}/>
//
export default class extends Component {
get audioService() {
let owner = getOwner(this);
return owner.lookup(`service:${this.args.audioType}`);
}
@action
onPlay() {
let player = this.audioService;
player.play(this.args.audioFile);
}
}
```
@method getOwner
@static
@for @ember/application
@param {Object} object An object with an owner.
@return {Object} An owner object.
@since 2.3.0
@public
*/
export function getOwner(object: any): Owner {
let owner = glimmerGetOwner(object) as Owner;
if (owner === undefined) {
owner = object[LEGACY_OWNER];
deprecate(
`You accessed the owner using \`getOwner\` on an object, but it was not set on that object with \`setOwner\`. You must use \`setOwner\` to set the owner on all objects. You cannot use Object.assign().`,
owner === undefined,
{
id: 'owner.legacy-owner-injection',
until: '3.25.0',
for: 'ember-source',
since: {
enabled: '3.22.0',
},
}
);
}
return owner;
}
/**
`setOwner` forces a new owner on a given object instance. This is primarily
useful in some testing cases.
@method setOwner
@static
@for @ember/application
@param {Object} object An object instance.
@param {Object} object The new owner object of the object instance.
@since 2.3.0
@public
*/
export function setOwner(object: any, owner: Owner): void {
glimmerSetOwner(object, owner);
object[LEGACY_OWNER] = owner;
}