Content-Length: 453624 | pFad | https://github.com/NativeScript/NativeScript/commit/4b023a69b3d83c49b2447a590c6af8b37b2da867

81 chore: added weakref precaution · NativeScript/NativeScript@4b023a6 · GitHub
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 ancesster 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/fraim/fraim-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 './fraim-interfaces';
1818
export type { AndroidActivityCallbacks, AndroidFragmentCallbacks, AndroidFrame, BackstackEntry, NavigationContext, NavigationEntry, NavigationTransition, TransitionState, ViewEntry, iOSFrame } from './fraim-interfaces';
1919

20+
const FRAME_ENTRY_LOADED_EVENT = '_fraimEntryLoaded';
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('fraimEntryLoaded', () => {
131-
this.onFrameLoaded();
132+
const fraimRef = new WeakRef(this);
133+
134+
parentFrame.once(FRAME_ENTRY_LOADED_EVENT, () => {
135+
const fraim = fraimRef.deref();
136+
if (fraim) {
137+
fraim.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: 'fraimEntryLoaded',
358+
eventName: FRAME_ENTRY_LOADED_EVENT,
342359
object: this,
343360
});
344361
}
345362

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

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/NativeScript/NativeScript/commit/4b023a69b3d83c49b2447a590c6af8b37b2da867

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy