Skip to content

Commit e1a21e9

Browse files
committed
refactor(renderer): fix removal from internal queue and add trace for
view util
1 parent 9ba194f commit e1a21e9

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

Diff for: nativescript-angular/trace.ts

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { write, categories, messageType } from "tns-core-modules/trace";
22

33
export const animationsTraceCategory = "ns-animations";
44
export const rendererTraceCategory = "ns-renderer";
5+
export const viewUtilCategory = "ns-view-util";
56
export const routerTraceCategory = "ns-router";
67
export const listViewTraceCategory = "ns-list-view";
78

@@ -17,6 +18,10 @@ export function rendererError(message: string): void {
1718
write(message, rendererTraceCategory, messageType.error);
1819
}
1920

21+
export function viewUtilLog(msg): void {
22+
write(msg, viewUtilCategory);
23+
}
24+
2025
export function routerLog(message: string): void {
2126
write(message, routerTraceCategory);
2227
}

Diff for: nativescript-angular/view-util.ts

+28-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from "./element-registry";
1818

1919
import { platformNames, Device } from "tns-core-modules/platform";
20-
import { rendererLog as traceLog } from "./trace";
20+
import { viewUtilLog as traceLog } from "./trace";
2121

2222
const ELEMENT_NODE_TYPE = 1;
2323
const XML_ATTRIBUTES = Object.freeze(["style", "rows", "columns", "fontAttributes"]);
@@ -94,6 +94,7 @@ export class ViewUtil {
9494
}
9595

9696
private appendToQueue(parent: NgElement, view: NgElement) {
97+
traceLog(`ViewUtil.appendToQueue parent: ${parent} view: ${view}`);
9798
if (parent.lastChild) {
9899
parent.lastChild.nextSibling = view;
99100
}
@@ -159,24 +160,46 @@ export class ViewUtil {
159160
}
160161

161162
private removeFromQueue(parent: NgLayoutBase, child: NgView, index: number) {
163+
traceLog(`ViewUtil.removeFromQueue ` +
164+
`parent: ${parent} child: ${child} index: ${index}`);
165+
166+
if (parent.firstChild === child && parent.lastChild === child) {
167+
parent.firstChild = null;
168+
parent.lastChild = null;
169+
return;
170+
}
171+
162172
if (parent.firstChild === child) {
163173
parent.firstChild = child.nextSibling;
164174
}
165175

166-
let previous = (parent.getChildAt(index - 1) || parent.firstChild) as NgElement;
176+
const previous = this.findPreviousElement(parent, child, index);
167177
if (parent.lastChild === child) {
168178
parent.lastChild = previous;
169179
}
170180

181+
if (previous) {
182+
previous.nextSibling = child.nextSibling;
183+
}
184+
}
185+
186+
private findPreviousElement(parent: NgLayoutBase, child: NgView, elementIndex: number): NgElement {
187+
const previousVisual = this.getPreviousVisualElement(parent, elementIndex);
188+
let previous = previousVisual || parent.firstChild;
189+
171190
// since detached elements are not added to the visual tree,
172191
// we need to find the actual previous sibling of the view,
173192
// which may as well be an invisible node
174-
while ( previous && previous !== child && isDetachedElement(previous.nextSibling)) {
193+
while (previous && previous !== child && previous.nextSibling !== child) {
175194
previous = previous.nextSibling;
176195
}
177196

178-
if (previous) {
179-
previous.nextSibling = child.nextSibling;
197+
return previous;
198+
}
199+
200+
private getPreviousVisualElement(parent: NgLayoutBase, elementIndex: number): NgElement {
201+
if (elementIndex > 0) {
202+
return parent.getChildAt(elementIndex - 1) as NgElement;
180203
}
181204
}
182205

0 commit comments

Comments
 (0)