8000 chore: added weakref precaution · NativeScript/NativeScript@4b023a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b023a6

Browse files
committed
chore: added weakref precaution
1 parent 764a153 commit 4b023a6

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

packages/core/ui/core/view-base/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,13 @@ export interface ShowModalOptions {
117117
* @param criterion - The type of ancestor view we are looking for. Could be a string containing a class name or an actual type.
118118
* Returns an instance of a view (if found), otherwise undefined.
119119
*/
120-
export function getAncestor(view: ViewBaseDefinition, criterion: string | { new () }): ViewBaseDefinition {
121-
let matcher: (view: ViewBaseDefinition) => boolean = null;
120+
export function getAncestor<T extends ViewBaseDefinition = ViewBaseDefinition>(view: T, criterion: string | { new () }): T {
121+
let matcher: (view: ViewBaseDefinition) => view is T;
122+
122123
if (typeof criterion === 'string') {
123-
matcher = (view: ViewBaseDefinition) => view.typeName === criterion;
124+
matcher = (view: ViewBaseDefinition): view is T => view.typeName === criterion;
124125
} else {
125-
matcher = (view: ViewBaseDefinition) => view instanceof criterion;
126+
matcher = (view: ViewBaseDefinition): view is T => view instanceof criterion;
126127
}
127128

128129
for (let parent = view.parent; parent != null; parent = parent.parent) {

packages/core/ui/frame/frame-common.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { NavigationData } from '.';
1717
export { NavigationType } from './frame-interfaces';
1818
export type { AndroidActivityCallbacks, AndroidFragmentCallbacks, AndroidFrame, BackstackEntry, NavigationContext, NavigationEntry, NavigationTransition, TransitionState, ViewEntry, iOSFrame } from './frame-interfaces';
1919

20+
const FRAME_ENTRY_LOADED_EVENT = '_frameEntryLoaded';
21+
2022
function buildEntryFromArgs(arg: any): NavigationEntry {
2123
let entry: NavigationEntry;
2224
if (typeof arg === 'string') {
@@ -76,13 +78,13 @@ export class FrameBase extends CustomLayoutView {
7678
return true;
7779
} else if (top) {
7880
let parentFrameCanGoBack = false;
79-
let parentFrame = <FrameBase>getAncestor(top, 'Frame');
81+
let parentFrame = getAncestor(top, 'Frame');
8082

8183
while (parentFrame && !parentFrameCanGoBack) {
8284
if (parentFrame && parentFrame.canGoBack()) {
8385
parentFrameCanGoBack = true;
8486
} else {
85-
parentFrame = <FrameBase>getAncestor(parentFrame, 'Frame');
87+
parentFrame = getAncestor(parentFrame, 'Frame');
8688
}
8789
}
8890

@@ -127,14 +129,29 @@ export class FrameBase extends CustomLayoutView {
127129
super.onLoaded();
128130

129131
if (parentFrame?.isLoadingSubviews) {
130-
parentFrame.once('frameEntryLoaded', () => {
131-
this.onFrameLoaded();
132+
const frameRef = new WeakRef(this);
133+
134+
parentFrame.once(FRAME_ENTRY_LOADED_EVENT, () => {
135+
const frame = frameRef.deref();
136+
if (frame) {
137+
frame.onFrameLoaded();
138+
}
132139
});
133140
} else {
134141
this.onFrameLoaded();
135142
}
136143
}
137144

145+
@profile
146+
public onUnloaded() {
147+
super.onUnloaded();
148+
149+
// This is a precaution in case the method is called asynchronously during the loading procedure
150+
if (this.hasListeners(FRAME_ENTRY_LOADED_EVENT)) {
151+
this.off(FRAME_ENTRY_LOADED_EVENT);
152+
}
153+
}
154+
138155
public onFrameLoaded(): void {
139156
this._processNextNavigationEntry();
140157
this._notifyFrameEntryLoaded();
@@ -338,15 +355,16 @@ export class FrameBase extends CustomLayoutView {
338355

339356
protected _notifyFrameEntryLoaded(): void {
340357
this.notify({
341-
eventName: 'frameEntryLoaded',
358+
eventName: FRAME_ENTRY_LOADED_EVENT,
342359
object: this,
343360
});
344361
}
345362

346363
private isNestedWithin(parentFrameCandidate: FrameBase): boolean {
347-
let frameAncestor: FrameBase = this;
364+
let frameAncestor = this as FrameBase;
365+
348366
while (frameAncestor) {
349-
frameAncestor = <FrameBase>getAncestor(frameAncestor, FrameBase);
367+
frameAncestor = getAncestor(frameAncestor, FrameBase);
350368
if (frameAncestor === parentFrameCandidate) {
351369
return true;
352370
}

0 commit comments

Comments
 (0)
0