forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdependency-observable.ts
More file actions
442 lines (369 loc) · 13.8 KB
/
dependency-observable.ts
File metadata and controls
442 lines (369 loc) · 13.8 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import definition = require("ui/core/dependency-observable");
import {Observable, WrappedValue} from "data/observable";
import types = require("utils/types");
// use private variables in the scope of the module rather than static members of the class since a member is still accessible through JavaScript and may be changed.
var propertyFromKey = {};
var propertyIdCounter = 0;
function generatePropertyKey(name: string, ownerType: string, validate?: boolean) {
if (validate) {
validateRegisterParameters(name, ownerType);
}
return ownerType + "." + name;
}
function validateRegisterParameters(name: string, ownerType: string) {
if (name == null || name.trim().length === 0) {
throw new Error("Name should not be null or empty string.");
}
if (ownerType == null || ownerType.trim().length === 0) {
throw new Error("OwnerType should not be null or empty string.");
}
}
function getPropertyByNameAndType(name: string, owner: any): Property {
var result;
var key;
var classInfo = types.getClassInfo(owner);
while (classInfo) {
key = generatePropertyKey(name, classInfo.name);
result = propertyFromKey[key];
if (result) {
break;
}
classInfo = classInfo.baseClassInfo;
}
return result;
}
export module PropertyMetadataSettings {
export var None = 0;
export var AffectsLayout = 1;
export var AffectsStyle = 1 << 1;
export var Inheritable = 1 << 2;
}
export module ValueSource {
export var Default = 0;
export var Inherited = 1;
export var Css = 2;
export var Local = 3;
export var VisualState = 4;
}
export class PropertyMetadata implements definition.PropertyMetadata {
private _defaultValue: any;
private _options: number;
private _onChanged: definition.PropertyChangedCallback;
private _onValidateValue: definition.PropertyValidationCallback;
private _equalityComparer: definition.PropertyEqualityComparer;
constructor(
defaultValue: any,
options?: number,
onChanged?: definition.PropertyChangedCallback,
onValidateValue?: definition.PropertyValidationCallback,
equalityComparer?: definition.PropertyEqualityComparer) {
this._defaultValue = defaultValue;
this._options = options;
if (types.isNullOrUndefined(this._options)) {
this._options = PropertyMetadataSettings.None;
}
this._onChanged = onChanged;
this._onValidateValue = onValidateValue;
this._equalityComparer = equalityComparer;
}
public get defaultValue(): any {
return this._defaultValue;
}
public get options(): number {
return this._options;
}
public get onValueChanged(): definition.PropertyChangedCallback {
return this._onChanged;
}
public set onValueChanged(value: definition.PropertyChangedCallback) {
this._onChanged = value;
}
public get onValidateValue(): definition.PropertyValidationCallback {
return this._onValidateValue;
}
public get equalityComparer(): definition.PropertyEqualityComparer {
return this._equalityComparer;
}
public get affectsLayout(): boolean {
return (this._options & PropertyMetadataSettings.AffectsLayout) === PropertyMetadataSettings.AffectsLayout;
}
public get affectsStyle(): boolean {
return (this._options & PropertyMetadataSettings.AffectsStyle) === PropertyMetadataSettings.AffectsStyle;
}
public get inheritable(): boolean {
return (this._options & PropertyMetadataSettings.Inheritable) === PropertyMetadataSettings.Inheritable;
}
}
export class Property implements definition.Property {
private _metadata: PropertyMetadata;
private _key: string;
private _name: string;
private _ownerType: string;
private _id: number;
private _valueConverter: (value: any) => any;
constructor(name: string, ownerType: string, metadata: PropertyMetadata, valueConverter?: (value: string) => any) {
// register key
this._key = generatePropertyKey(name, ownerType, true);
if (propertyFromKey[this._key]) {
throw new Error("Property " + name + " already registered for type " + ownerType + ".");
}
propertyFromKey[this._key] = this;
if (!metadata || !(metadata instanceof PropertyMetadata)) {
throw new Error("Expected valid PropertyMetadata instance.");
}
this._name = name;
this._ownerType = ownerType;
this._metadata = metadata;
// generate a unique numeric id for each property (faster lookup than a string key)
this._id = propertyIdCounter++;
this._valueConverter = valueConverter;
}
public defaultValueGetter: (instance: definition.DependencyObservable) => definition.NativeValueResult;
public get name(): string {
return this._name;
}
public get id(): number {
return this._id;
}
public get metadata(): PropertyMetadata {
return this._metadata;
}
public isValidValue(value: Object): boolean {
if (this.metadata.onValidateValue) {
return this.metadata.onValidateValue(value);
}
// TODO: consider type check here (e.g. passing function where object is expected)
return true;
}
public get valueConverter(): (value: string) => any {
return this._valueConverter;
}
public _getEffectiveValue(entry: PropertyEntry): any {
if (types.isDefined(entry.localValue)) {
entry.valueSource = ValueSource.Local;
return entry.localValue;
}
if (types.isDefined(entry.inheritedValue)) {
entry.valueSource = ValueSource.Inherited;
return entry.inheritedValue;
}
entry.valueSource = ValueSource.Default;
return this.metadata.defaultValue;
}
}
export class PropertyEntry implements definition.PropertyEntry {
private _property: Property;
private _valueSource: number;
private _inheritedValue: any;
private _cssValue: any;
private _localValue: any;
private _effectiveValue: any;
private _visualStateValue: any;
constructor(property: Property) {
this._property = property;
}
get property(): Property {
return this._property;
}
get effectiveValue() {
if (!this._effectiveValue) {
this._effectiveValue = this._property._getEffectiveValue(this);
}
return this._effectiveValue;
}
get valueSource(): number {
return this._valueSource;
}
set valueSource(value: number) {
this._valueSource = value;
}
get localValue(): any {
return this._localValue;
}
set localValue(value: any) {
this._localValue = value;
this._effectiveValue = undefined;
}
get inheritedValue(): any {
return this._inheritedValue;
}
set inheritedValue(value: any) {
this._inheritedValue = value;
this._effectiveValue = undefined;
}
get cssValue(): any {
return this._cssValue;
}
set cssValue(value: any) {
this._cssValue = value;
this._effectiveValue = undefined;
}
get visualStateValue(): any {
return this._visualStateValue;
}
set visualStateValue(value: any) {
this._visualStateValue = value;
this._effectiveValue = undefined;
}
public resetValue() {
this._valueSource = ValueSource.Default;
this._visualStateValue = undefined;
this._localValue = undefined;
this._cssValue = undefined;
this._inheritedValue = undefined;
this._effectiveValue = undefined;
}
}
var defaultValueForPropertyPerType: Map<string, any> = new Map<string, any>();
export class DependencyObservable extends Observable {
private _propertyEntries = {};
public set(name: string, value: any) {
var property = getPropertyByNameAndType(name, this);
if (property) {
this._setValue(property, value, ValueSource.Local);
} else {
super.set(name, value);
}
}
public get(name: string): any {
var property = getPropertyByNameAndType(name, this);
if (property) {
return this._getValue(property);
} else {
return super.get(name);
}
}
public _setValue(property: Property, value: any, source?: number) {
let realValue = WrappedValue.unwrap(value);
if (!property.isValidValue(realValue)) {
throw new Error("Invalid value " + realValue + " for property " + property.name);
}
if (types.isUndefined(source)) {
source = ValueSource.Local;
}
this._setValueInternal(property, value, source);
}
public _getValueSource(property: Property): number {
var entry: PropertyEntry = this._propertyEntries[property.id];
if (entry) {
return entry.valueSource;
}
return ValueSource.Default;
}
public _getValue(property: Property): any {
var entry: PropertyEntry = this._propertyEntries[property.id];
if (entry) {
return entry.effectiveValue;
}
else if (property.defaultValueGetter) { // we check for cached properties only for these which have 'defaultValueGetter' defined;
// When DependencyProperties are removed from Style - fix this check.
var view = (<any>this)._view || this;
let key = types.getClass(view) + "." + property.id;
let defaultValue = defaultValueForPropertyPerType.get(key);
if (types.isUndefined(defaultValue) && view._nativeView) {
let defaultValueResult = property.defaultValueGetter(this);
defaultValue = defaultValueResult.result;
if (defaultValueResult.cacheable) {
defaultValueForPropertyPerType.set(key, defaultValue);
}
}
return defaultValue;
}
return property.metadata.defaultValue;
}
public _resetValue(property: Property, source?: number) {
if (!(property.id in this._propertyEntries)) {
return;
}
if (types.isDefined(source)) {
// resetting particular modifier to undefined will remove it from the effective value composition
this._setValueInternal(property, undefined, source);
} else {
var currentValue = this._getValue(property);
delete this._propertyEntries[property.id];
var newValue = this._getValue(property);
var comparer: (x: any, y: any) => boolean = property.metadata.equalityComparer || this._defaultComparer;
if (!comparer(currentValue, newValue)) {
this._onPropertyChanged(property, currentValue, newValue);
}
}
}
public _onPropertyChanged(property: Property, oldValue: any, newValue: any) {
let realNewValue = WrappedValue.unwrap(newValue);
if (property.metadata.onValueChanged) {
property.metadata.onValueChanged({
object: this,
property: property,
eventName: Observable.propertyChangeEvent,
newValue: realNewValue,
oldValue: oldValue
});
}
if (this.hasListeners(Observable.propertyChangeEvent)) {
var changeData = super._createPropertyChangeData(property.name, newValue);
this.notify(changeData);
}
let eventName = property.name + "Change";
if (this.hasListeners(eventName)) {
var ngChangedData = {
eventName: eventName,
propertyName: property.name,
object: this,
value: realNewValue
}
this.notify(ngChangedData);
}
}
public _eachSetProperty(callback: (property: Property) => boolean) {
var i;
var key;
var entry: PropertyEntry;
var retVal: boolean;
var keys = Object.keys(this._propertyEntries);
for (i = 0; i < keys.length; i++) {
key = keys[i];
entry = this._propertyEntries[key];
retVal = callback(entry.property);
if (!retVal) {
break;
}
}
}
public toString(): string {
return this.typeName;
}
private _setValueInternal(property: Property, value: any, source: number) {
let realValue = WrappedValue.unwrap(value);
// Convert the value to the real property type in case it is coming as a string from CSS or XML.
if (types.isString(realValue) && property.valueConverter) {
realValue = property.valueConverter(realValue);
}
var entry: PropertyEntry = this._propertyEntries[property.id];
if (!entry) {
entry = new PropertyEntry(property);
this._propertyEntries[property.id] = entry;
}
var currentValue = entry.effectiveValue;
switch (source) {
case ValueSource.Css:
entry.cssValue = realValue;
break;
case ValueSource.Inherited:
entry.inheritedValue = realValue;
break;
case ValueSource.Local:
entry.localValue = realValue;
break;
case ValueSource.VisualState:
entry.visualStateValue = realValue;
break;
}
var comparer: (x: any, y: any) => boolean = property.metadata.equalityComparer || this._defaultComparer;
if ((value && value.wrapped) || !comparer(currentValue, entry.effectiveValue)) {
this._onPropertyChanged(property, currentValue, entry.effectiveValue);
}
}
private _defaultComparer(x: any, y: any): boolean {
return x === y;
}
}