/
properties.ts
239 lines (201 loc) · 7.14 KB
/
properties.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
@module @ember/object
*/
import { descriptorFor, Meta, meta as metaFor, peekMeta, UNDEFINED } from '@ember/-internals/meta';
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';
import { overrideChains } from './property_events';
export type MandatorySetterFunction = ((this: object, value: any) => void) & {
isMandatorySetter: true;
};
export type DefaultGetterFunction = (this: object) => void;
export type InheritingGetterFunction = ((this: object) => void) & {
isInheritingGetter: true;
};
// ..........................................................
// DESCRIPTOR
//
/**
Objects of this type can implement an interface to respond to requests to
get and set. The default implementation handles simple properties.
@class Descriptor
@private
*/
export abstract class Descriptor {
isDescriptor = true;
enumerable = true;
configurable = true;
setup(obj: object, keyName: string, meta: Meta): void {
Object.defineProperty(obj, keyName, {
enumerable: this.enumerable,
configurable: this.configurable,
get: DESCRIPTOR_GETTER_FUNCTION(keyName, this),
});
meta.writeDescriptors(keyName, this);
}
teardown(_obj: object, keyName: string, meta: Meta): void {
meta.removeDescriptors(keyName);
}
abstract get(obj: object, keyName: string): any | null | undefined;
abstract set(obj: object, keyName: string, value: any | null | undefined): any | null | undefined;
willWatch?(obj: object, keyName: string, meta: Meta): void;
didUnwatch?(obj: object, keyName: string, meta: Meta): void;
didChange?(obj: object, keyName: string): void;
}
interface ExtendedObject {
didDefineProperty?: (obj: object, keyName: string, value: any) => void;
}
// ..........................................................
// DEFINING PROPERTIES API
//
export function MANDATORY_SETTER_FUNCTION(name: string): MandatorySetterFunction {
function SETTER_FUNCTION(this: object, value: any | undefined | null): void {
let m = peekMeta(this)!;
if (m.isInitializing() || m.isPrototypeMeta(this)) {
m.writeValues(name, value);
} else {
assert(
`You must use set() to set the \`${name}\` property (of ${this}) to \`${value}\`.`,
false
);
}
}
return Object.assign(SETTER_FUNCTION as MandatorySetterFunction, { isMandatorySetter: true });
}
export function DEFAULT_GETTER_FUNCTION(name: string): DefaultGetterFunction {
return function GETTER_FUNCTION(this: any): void {
let meta = peekMeta(this);
if (meta !== null) {
return meta.peekValues(name);
}
};
}
export function INHERITING_GETTER_FUNCTION(name: string): InheritingGetterFunction {
function IGETTER_FUNCTION(this: any): void {
let meta = peekMeta(this);
let val;
if (meta !== null) {
val = meta.readInheritedValue('values', name);
if (val === UNDEFINED) {
let proto = Object.getPrototypeOf(this);
return proto === null ? undefined : proto[name];
}
}
return val;
}
return Object.assign(IGETTER_FUNCTION as InheritingGetterFunction, {
isInheritingGetter: true,
});
}
function DESCRIPTOR_GETTER_FUNCTION(name: string, descriptor: Descriptor): () => any {
return function CPGETTER_FUNCTION(this: object): any {
return descriptor.get(this, name);
};
}
/**
NOTE: This is a low-level method used by other parts of the API. You almost
never want to call this method directly. Instead you should use
`mixin()` to define new properties.
Defines a property on an object. This method works much like the ES5
`Object.defineProperty()` method except that it can also accept computed
properties and other special descriptors.
Normally this method takes only three parameters. However if you pass an
instance of `Descriptor` as the third param then you can pass an
optional value as the fourth parameter. This is often more efficient than
creating new descriptor hashes for each property.
## Examples
```javascript
import { defineProperty, computed } from '@ember/object';
// ES5 compatible mode
defineProperty(contact, 'firstName', {
writable: true,
configurable: false,
enumerable: true,
value: 'Charles'
});
// define a simple property
defineProperty(contact, 'lastName', undefined, 'Jolley');
// define a computed property
defineProperty(contact, 'fullName', computed('firstName', 'lastName', function() {
return this.firstName+' '+this.lastName;
}));
```
@public
@method defineProperty
@static
@for @ember/object
@param {Object} obj the object to define this property on. This may be a prototype.
@param {String} keyName the name of the property
@param {Descriptor} [desc] an instance of `Descriptor` (typically a
computed property) or an ES5 descriptor.
You must provide this or `data` but not both.
@param {*} [data] something other than a descriptor, that will
become the explicit value of this property.
*/
export function defineProperty(
obj: object,
keyName: string,
desc?: Descriptor | undefined | null,
data?: any | undefined | null,
meta?: Meta
): void {
if (meta === undefined) {
meta = metaFor(obj);
}
let watching = meta.peekWatching(keyName) > 0;
let previousDesc = descriptorFor(obj, keyName, meta);
let wasDescriptor = previousDesc !== undefined;
if (wasDescriptor) {
previousDesc.teardown(obj, keyName, meta);
}
// used to track if the the property being defined be enumerable
let enumerable = true;
// Ember.NativeArray is a normal Ember.Mixin that we mix into `Array.prototype` when prototype extensions are enabled
// mutating a native object prototype like this should _not_ result in enumerable properties being added (or we have significant
// issues with things like deep equality checks from test frameworks, or things like jQuery.extend(true, [], [])).
//
// this is a hack, and we should stop mutating the array prototype by default 😫
if (obj === Array.prototype) {
enumerable = false;
}
let value;
if (desc instanceof Descriptor) {
value = desc;
desc.setup(obj, keyName, meta);
} else if (desc === undefined || desc === null) {
value = data;
if (DEBUG && watching) {
meta.writeValues(keyName, data);
let defaultDescriptor = {
configurable: true,
enumerable,
set: MANDATORY_SETTER_FUNCTION(keyName),
get: DEFAULT_GETTER_FUNCTION(keyName),
};
Object.defineProperty(obj, keyName, defaultDescriptor);
} else if (wasDescriptor || enumerable === false) {
Object.defineProperty(obj, keyName, {
configurable: true,
enumerable,
writable: true,
value,
});
} else {
obj[keyName] = data;
}
} else {
value = desc;
// fallback to ES5
Object.defineProperty(obj, keyName, desc);
}
// if key is being watched, override chains that
// were initialized with the prototype
if (watching) {
overrideChains(obj, keyName, meta);
}
// The `value` passed to the `didDefineProperty` hook is
// either the descriptor or data, whichever was passed.
if (typeof (obj as ExtendedObject).didDefineProperty === 'function') {
(obj as ExtendedObject).didDefineProperty!(obj, keyName, value);
}
}