Skip to content

Commit bffbbc2

Browse files
authored
fix(modal): closeCallback(...) should not have side effects when called multiple times (#1349)
1 parent cc74946 commit bffbbc2

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

Diff for: nativescript-angular/common/utils.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@ export function throwIfAlreadyLoaded(
22
parentModule: any,
33
moduleName: string,
44
) {
5-
if ( parentModule ) {
5+
if (parentModule) {
66
throw new Error(`${moduleName} has already been loaded. Import ${moduleName} in the AppModule only.`);
77
}
88
}
9+
10+
export function once(fn: Function) {
11+
let wasCalled = false;
12+
13+
return function wrapper() {
14+
if (wasCalled) {
15+
return;
16+
}
17+
18+
wasCalled = true;
19+
fn.apply(null, arguments);
20+
};
21+
}

Diff for: nativescript-angular/directives/dialogs.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { ProxyViewContainer } from "tns-core-modules/ui/proxy-view-container/pro
1616
import { AppHostView } from "../app-host-view";
1717
import { DetachedLoader } from "../common/detached-loader";
1818
import { PageFactory, PAGE_FACTORY } from "../platform-providers";
19+
import { once } from "../common/utils";
1920

2021
export interface ModalDialogOptions {
2122
context?: any;
@@ -106,19 +107,17 @@ export class ModalDialogService {
106107
let componentView: View;
107108
let detachedLoaderRef: ComponentRef<DetachedLoader>;
108109

109-
const closeCallback = (...args) => {
110+
const closeCallback = once((...args) => {
110111
doneCallback.apply(undefined, args);
111-
if (componentView && !this.location._isModalClosing) {
112+
if (componentView) {
112113
this.location._beginCloseModalNavigation();
113-
114114
componentView.closeModal();
115-
} else if (this.location._isModalClosing) {
116115
this.location.back();
117116
this.location._finishCloseModalNavigation();
118117
detachedLoaderRef.instance.detectChanges();
119118
detachedLoaderRef.destroy();
120119
}
121-
};
120+
});
122121

123122
const modalParams = new ModalDialogParams(context, closeCallback);
124123
const providers = ReflectiveInjector.resolve([
@@ -141,7 +140,7 @@ export class ModalDialogService {
141140
}
142141

143142
// TODO: remove <any> cast after https://github.com/NativeScript/NativeScript/pull/5734
144-
// is in a published version of tns-core-modules.
143+
// is in a published version of tns-core-modules.
145144
(<any>parentView).showModal(componentView, context, closeCallback, fullscreen, animated, stretched);
146145
});
147146
}

0 commit comments

Comments
 (0)