forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
104 lines (89 loc) · 3.87 KB
/
proxy.ts
File metadata and controls
104 lines (89 loc) · 3.87 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
import observable = require("data/observable");
import bindable = require("ui/core/bindable");
import dependencyObservable = require("ui/core/dependency-observable");
import types = require("utils/types");
import definition = require("ui/core/proxy");
export class PropertyMetadata extends dependencyObservable.PropertyMetadata implements definition.PropertyMetadata {
private _onSetNativeValue: dependencyObservable.PropertyChangedCallback;
constructor(
defaultValue: any,
options?: number,
onChanged?: dependencyObservable.PropertyChangedCallback,
onValidateValue?: dependencyObservable.PropertyValidationCallback,
onSetNativeValue?: dependencyObservable.PropertyChangedCallback) {
super(defaultValue, options, onChanged, onValidateValue);
this._onSetNativeValue = onSetNativeValue;
}
get onSetNativeValue(): dependencyObservable.PropertyChangedCallback {
return this._onSetNativeValue;
}
set onSetNativeValue(value: dependencyObservable.PropertyChangedCallback) {
this._onSetNativeValue = value;
}
}
export class ProxyObject extends bindable.Bindable implements definition.ProxyObject {
private _updatingJSPropertiesDict = {};
/**
* Gets the android-specific native instance that lies behind this proxy. Will be available if running on an Android platform.
*/
get android(): any {
return undefined;
}
/**
* Gets the ios-specific native instance that lies behind this proxy. Will be available if running on an iOS platform.
*/
get ios(): any {
return undefined;
}
public _onPropertyChanged(property: dependencyObservable.Property, oldValue: any, newValue: any) {
super._onPropertyChanged(property, oldValue, newValue);
this._trySetNativeValue(property, oldValue, newValue);
}
/**
* A property has changed on the native side directly - e.g. the user types in a TextField.
*/
public _onPropertyChangedFromNative(property: dependencyObservable.Property, newValue: any) {
if (this._updatingJSPropertiesDict[property.name]) {
return;
}
this._updatingJSPropertiesDict[property.name] = true;
this._setValue(property, newValue);
delete this._updatingJSPropertiesDict[property.name];
}
public _syncNativeProperties() {
var that = this;
var eachPropertyCallback = function (property: dependencyObservable.Property): boolean {
that._trySetNativeValue(property);
return true;
}
this._eachSetProperty(eachPropertyCallback);
}
private _trySetNativeValue(property: dependencyObservable.Property, oldValue?:any, newValue?: any) {
if (this._updatingJSPropertiesDict[property.name]) {
// This is the case when a property has changed from the native side directly and we have received the "_onPropertyChanged" event while synchronizing our local cache
return;
}
if (global.android && !this.android) {
// in android we have lazy loading and we do not have a native widget created yet, do not call the onSetNativeValue callback
// properties will be synced when the widget is created
return;
}
var metadata = property.metadata;
if (!(metadata instanceof PropertyMetadata)) {
return;
}
var proxyMetadata = <PropertyMetadata>metadata;
if (proxyMetadata.onSetNativeValue) {
if (types.isUndefined(newValue)) {
newValue = this._getValue(property);
}
proxyMetadata.onSetNativeValue({
object: this,
property: property,
eventName: observable.knownEvents.propertyChange,
newValue: newValue,
oldValue: oldValue
});
}
}
}