8000 Improve background setter in iOS (#3582) · Cayan/NativeScript@f539dd5 · GitHub
[go: up one dir, main page]

Skip to content

Commit f539dd5

Browse files
author
Hristo Hristov
authored
Improve background setter in iOS (NativeScript#3582)
CoercableProperty now inherits from Property VS Code typescript sdk removed ViewBase will no longer clear native view if recycleNativeView is false
1 parent 4440569 commit f539dd5

File tree

12 files changed

+464
-500
lines changed

12 files changed

+464
-500
lines changed

.vscode/settings.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/.vscode/launch.json

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,41 @@
11
{
22
"version": "0.2.0",
33
"configurations": [
4-
{
5-
"name": "Sync on iOS",
6-
"type": "nativescript",
7-
"platform": "ios",
8-
"request": "launch",
9-
"appRoot": "${workspaceRoot}",
10-
"sourceMaps": true,
11-
"diagnosticLogging": false,
12-
"emulator": false,
13-
"rebuild": false,
14-
"syncAllFiles": true,
15-
"stopOnEntry": true
16-
},
174
{
185
"name": "Launch on iOS",
196
"type": "nativescript",
20-
"platform": "ios",
217
"request": "launch",
8+
"platform": "ios",
229
"appRoot": "${workspaceRoot}",
2310
"sourceMaps": true,
24-
"diagnosticLogging": false,
25-
"emulator": false,
26-
"rebuild": true,
27-
"stopOnEntry": true
11+
"watch": true
2812
},
2913
{
3014
"name": "Attach on iOS",
3115
"type": "nativescript",
32-
"platform": "ios",
3316
"request": "attach",
17+
"platform": "ios",
3418
"appRoot": "${workspaceRoot}",
3519
"sourceMaps": true,
36-
"diagnosticLogging": false,
37-
"emulator": false,
38-
"stopOnEntry": true
39-
},
40-
{
41-
"name": "Sync on Android",
42-
"type": "nativescript",
43-
"platform": "android",
44-
"request": "launch",
45-
"appRoot": "${workspaceRoot}",
46-
"sourceMaps": true,
47-
"diagnosticLogging": false,
48-
"emulator": false,
49-
"rebuild": false,
50-
"syncAllFiles": true,
51-
"stopOnEntry": true
20+
"watch": false
5221
},
5322
{
5423
"name": "Launch on Android",
5524
"type": "nativescript",
56-
"platform": "android",
5725
"request": "launch",
26+
"platform": "android",
5827
"appRoot": "${workspaceRoot}",
5928
"sourceMaps": true,
60-
"diagnosticLogging": false,
61-
"emulator": false,
62-
"rebuild": true,
63-
"stopOnEntry": true
29+
"watch": true
6430
},
6531
{
6632
"name": "Attach on Android",
6733
"type": "nativescript",
68-
"platform": "android",
6934
"request": "attach",
35+
"platform": "android",
7036
"appRoot": "${workspaceRoot}",
7137
"sourceMaps": true,
72-
"diagnosticLogging": false,
73-
"emulator": false,
74-
"stopOnEntry": true
38+
"watch": false
7539
}
7640
]
7741
}

tests/.vscode/settings.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
"nativescript": {
77
"id": "org.nativescript.tests",
88
"tns-ios": {
9-
"version": "2.4.0"
9+
"version": "2.5.0"
1010
},
1111
"tns-android": {
12-
"version": "2.4.1"
12+
"version": "2.5.0"
1313
}
1414
},
1515
"dependencies": {
16-
"tns-core-modules": "2.4.0"
16+
"tns-core-modules": "2.5.0"
1717
},
1818
"devDependencies": {
1919
"tns-platform-declarations": "*",
< 6377 button class="Button Button--iconOnly Button--invisible ExpandableHunkHeaderDiffLine-module__expand-button-line--wZKjF ExpandableHunkHeaderDiffLine-module__expand-button-unified--Eae6C" aria-label="Expand file from line 19 to line 24" data-direction="all" aria-hidden="true" tabindex="-1">
@@ -24,4 +24,4 @@
2424
"lazy": "1.0.11",
2525
"typescript": "~2.0.10"
2626
}
27-
}
27+
}

tns-core-modules/ui/core/properties.ts

Lines changed: 50 additions & 91 deletions
F438
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const enum ValueSource {
3838
Local = 3
3939
}
4040

41-
export class Property<T extends ViewBase, U> implements PropertyDescriptor, definitions.Property<T, U> {
41+
export class Property<T extends ViewBase, U> implements TypedPropertyDescriptor<U>, definitions.Property<T, U> {
4242
private registered: boolean;
4343

4444
public readonly name: string;
@@ -174,40 +174,20 @@ export class Property<T extends ViewBase, U> implements PropertyDescriptor, defi
174174
}
175175
}
176176

177-
export class CoercibleProperty<T extends ViewBase, U> implements PropertyDescriptor, definitions.CoercibleProperty<T, U> {
178-
private registered: boolean;
179-
private readonly name: string;
180-
public readonly key: symbol;
181-
public readonly native: symbol;
182-
public readonly defaultValueKey: symbol;
183-
public readonly defaultValue: U;
184-
public readonly nativeValueChange: (owner: T, value: U) => void;
185-
186-
public readonly get: () => U;
187-
public readonly set: (value: U) => void;
188-
public readonly enumerable: boolean = true;
189-
public readonly configurable: boolean = true;
190-
177+
export class CoercibleProperty<T extends ViewBase, U> extends Property<T, U> implements definitions.CoercibleProperty<T, U> {
191178
public readonly coerce: (target: T) => void;
192179

193180
constructor(options: definitions.CoerciblePropertyOptions<T, U>) {
194-
const name = options.name;
195-
this.name = name;
196-
197-
const key = Symbol(name + ":propertyKey");
198-
this.key = key;
199-
200-
const native: symbol = Symbol(name + ":nativeKey");
201-
this.native = native;
181+
super(options);
202182

203-
const defaultValueKey = Symbol(name + ":nativeDefaultValue");
204-
this.defaultValueKey = defaultValueKey;
183+
const name = options.name;
184+
const key = this.key;
185+
const native: symbol = this.native;
186+
const defaultValueKey = this.defaultValueKey;
187+
const defaultValue: U = this.defaultValue;
205188

206189
const coerceKey = Symbol(name + ":coerceKey");
207190

208-
const defaultValue: U = options.defaultValue;
209-
this.defaultValue = defaultValue;
210-
211191
const eventName = name + "Change";
212192
const affectsLayout: boolean = options.affectsLayout;
213193
const equalityComparer = options.equalityComparer;
@@ -218,8 +198,8 @@ export class CoercibleProperty<T extends ViewBase, U> implements PropertyDescrip
218198
this.coerce = function (target: T): void {
219199
const originalValue: U = coerceKey in target ? target[coerceKey] : defaultValue;
220200
// need that to make coercing but also fire change events
221-
this.set.call(target, originalValue);
222-
};
201+
target[name] = originalValue;
202+
}
223203

224204
this.set = function (this: T, value: U): void {
225205
const reset = value === unsetValue;
@@ -283,46 +263,7 @@ export class CoercibleProperty<T extends ViewBase, U> implements PropertyDescrip
283263
this.requestLayout();
284264
}
285265
}
286-
};
287-
288-
this.get = function (): U {
289-
return key in this ? this[key] : defaultValue;
290-
};
291-
292-
this.nativeValueChange = function (owner: T, value: U): void {
293-
const currentValue = key in owner ? owner[key] : defaultValue;
294-
const changed = equalityComparer ? !equalityComparer(currentValue, value) : currentValue !== value;
295-
if (changed) {
296-
owner[key] = value;
297-
owner[coerceKey] = value;
298-
if (valueChanged) {
299-
valueChanged(owner, currentValue, value);
300-
}
301-
302-
if (owner.hasListeners(eventName)) {
303-
owner.notify({
304-
eventName: eventName,
305-
propertyName: name,
306-
object: owner,
307-
value: value
308-
});
309-
}
310-
311-
if (affectsLayout) {
312-
owner.requestLayout();
313-
}
314-
}
315-
};
316-
317-
symbolPropertyMap[key] = this;
318-
}
319-
320-
public register(cls: { prototype: T }): void {
321-
if (this.registered) {
322-
throw new Error(`Property ${this.name} already registered.`);
323266
}
324-
this.registered = true;
325-
Object.defineProperty(cls.prototype, this.name, this);
326267
}
327268
}
328269

@@ -832,7 +773,7 @@ function inheritableCssPropertyValuesOn(style: Style): Array<{ property: Inherit
832773
return array;
833774
}
834775

835-
export function applyNativeSetters(view: ViewBase): void {
776+
export function initNativeView(view: ViewBase): void {
836777
let symbols = (<any>Object).getOwnPropertySymbols(view);
837778
for (let symbol of symbols) {
838779
const property: Property<any, any> = symbolPropertyMap[symbol];
@@ -873,6 +814,44 @@ export function applyNativeSetters(view: ViewBase): void {
873814
}
874815
}
875816

817+
export function resetNativeView(view: ViewBase): void {
818+
let symbols = (<any>Object).getOwnPropertySymbols(view);
819+
for (let symbol of symbols) {
820+
const property: Property<any, any> = symbolPropertyMap[symbol];
821+
if (!property) {
822+
continue;
823+
}
824+
825+
const native = property.native;
826+
if (native in view) {
827+
view[native] = view[property.defaultValueKey];
828+
delete view[property.defaultValueKey];
829+
}
830+
831+
// This will not call propertyChange!!!
832+
delete view[property.key];
833+
}
834+
835+
const style = view.style;
836+
837+
symbols = (<any>Object).getOwnPropertySymbols(style);
838+
for (let symbol of symbols) {
839+
const property: CssProperty<any, any> = cssSymbolPropertyMap[symbol];
840+
if (!property) {
841+
continue;
842+
}
843+
844+
const native = property.native;
845+
if (native in view) {
846+
view[native] = style[property.defaultValueKey];
847+
delete style[property.defaultValueKey];
848+
}
849+
850+
// This will not call propertyChange!!!
851+
delete style[property.key];
852+
}
853+
}
854+
876855
export function clearInheritedProperties(view: ViewBase): void {
877856
for (let prop of inheritableProperties) {
878857
const sourceKey = prop.sourceKey;
@@ -902,26 +881,6 @@ export function resetCSSProperties(style: Style): void {
902881
}
903882
}
904883

905-
export function resetStyleProperties(style: Style): void {
906-
let symbols = (<any>Object).getOwnPropertySymbols(style);
907-
const view = style.view;
908-
for (let symbol of symbols) {
909-
const property: CssProperty<any, any> = symbolPropertyMap[symbol];
910-
if (!property) {
911-
continue;
912-
}
913-
914-
const native = property.native;
915-
if (native in view) {
916-
view[native] = style[property.defaultValueKey];
917-
delete style[property.defaultValueKey];
918-
}
919-
920-
// This will not call propertyChange!!!
921-
delete style[property.key];
922-
}
923-
}
924-
925884
export function propagateInheritedProperties(view: ViewBase): void {
926885
const inheritablePropertyValues = inheritablePropertyValuesOn(view);
927886
const inheritableCssPropertyValues = inheritableCssPropertyValuesOn(view.style);
@@ -966,4 +925,4 @@ export function makeParser<T>(isValid: (value: any) => boolean): (value: any) =>
966925
throw new Error("Invalid value: " + value);
967926
}
968927
};
969-
}
928+
}

tns-core-modules/ui/core/view-base.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ViewBase as ViewBaseDefinition } from "ui/core/view-base";
22
import { Observable, EventData, PropertyChangeData } from "data/observable";
3-
import { Property, InheritedProperty, Style, clearInheritedProperties, propagateInheritedProperties, resetCSSProperties, applyNativeSetters, resetStyleProperties } from "./properties";
3+
import { Property, InheritedProperty, Style, clearInheritedProperties, propagateInheritedProperties, resetCSSProperties, initNativeView, resetNativeView } from "./properties";
44
import { Binding, BindingOptions } from "ui/core/bindable";
55
import { isIOS, isAndroid } from "platform";
66
import { fromString as gestureFromString } from "ui/gestures";
@@ -15,7 +15,7 @@ import * as types from "utils/types";
1515
import * as ssm from "ui/styling/style-scope";
1616
let styleScopeModule: typeof ssm;
1717
function ensureStyleScopeModule() {
18-
if (!styleScopeModule){
18+
if (!styleScopeModule) {
1919
styleScopeModule = require("ui/styling/style-scope");
2020
}
2121
}
@@ -107,6 +107,8 @@ export function eachDescendant(view: ViewBaseDefinition, callback: (child: ViewB
107107
let viewIdCounter = 0;
108108

109109
export class ViewBase extends Observable implements ViewBaseDefinition {
110+
public recycleNativeView: boolean;
111+
110112
private _style: Style;
111113
private _isLoaded: boolean;
112114
private _registeredAnimations: Array<KeyframeAnimation>;
@@ -505,7 +507,9 @@ export class ViewBase extends Observable implements ViewBaseDefinition {
505507
}
506508

507509
public _resetNativeView(): void {
508-
//
510+
if (this.nativeView && this.recycleNativeView) {
511+
resetNativeView(this);
512+
}
509513
}
510514

511515
public _setupUI(context: android.content.Context, atIndex?: number) {
@@ -533,7 +537,7 @@ export class ViewBase extends Observable implements ViewBaseDefinition {
533537
}
534538

535539
if (this.nativeView) {
536-
applyNativeSetters(this);
540+
initNativeView(this);
537541
}
538542

539543
this.eachChild((child) => {
@@ -552,11 +556,6 @@ export class ViewBase extends Observable implements ViewBaseDefinition {
552556
return true;
553557
});
554558

555-
if (this.nativeView) {
556-
// TODO: rename and implement this as resetNativeSetters
557-
resetStyleProperties(this.style);
558-
}
559-
560559
if (this.parent) {
561560
this.parent._removeViewFromNativeVisualTree(this);
562561
}

0 commit comments

Comments
 (0)
0