/
injected_property.ts
82 lines (70 loc) · 2.33 KB
/
injected_property.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
import { descriptorFor } from '@ember/-internals/meta';
import { getOwner } from '@ember/-internals/owner';
import { EMBER_MODULE_UNIFICATION } from '@ember/canary-features';
import { assert } from '@ember/debug';
import { ComputedProperty } from './computed';
import { defineProperty } from './properties';
export interface InjectedPropertyOptions {
source: string;
}
/**
@module ember
@private
*/
/**
Read-only property that returns the result of a container lookup.
@class InjectedProperty
@namespace Ember
@constructor
@param {String} type The container type the property will lookup
@param {String} name (optional) The name the property will lookup, defaults
to the property's name
@private
*/
export default class InjectedProperty extends ComputedProperty {
readonly type: string;
readonly name: string;
readonly source: string | undefined;
readonly namespace: string | undefined;
constructor(type: string, name: string, options?: InjectedPropertyOptions) {
super(injectedPropertyDesc);
this.type = type;
this.name = name;
if (EMBER_MODULE_UNIFICATION) {
this.source = options ? options.source : undefined;
this.namespace = undefined;
if (name) {
let namespaceDelimiterOffset = name.indexOf('::');
if (namespaceDelimiterOffset === -1) {
this.name = name;
this.namespace = undefined;
} else {
this.name = name.slice(namespaceDelimiterOffset + 2);
this.namespace = name.slice(0, namespaceDelimiterOffset);
}
}
}
}
}
const injectedPropertyDesc = {
get(this: any, keyName: string): any {
let desc = descriptorFor(this, keyName);
let owner = getOwner(this) || this.container; // fallback to `container` for backwards compat
assert(
`InjectedProperties should be defined with the inject computed property macros.`,
desc && desc.type
);
assert(
`Attempting to lookup an injected property on an object without a container, ensure that the object was instantiated via a container.`,
Boolean(owner)
);
let specifier = `${desc.type}:${desc.name || keyName}`;
return owner.lookup(specifier, {
source: desc.source,
namespace: desc.namespace,
});
},
set(this: any, keyName: string, value: any) {
defineProperty(this, keyName, null, value);
}
}