Skip to content

fix(modal): closeCallback(...) no side effects on multiple calls #1349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion nativescript-angular/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@ export function throwIfAlreadyLoaded(
parentModule: any,
moduleName: string,
) {
if ( parentModule ) {
if (parentModule) {
throw new Error(`${moduleName} has already been loaded. Import ${moduleName} in the AppModule only.`);
}
}

export function once(fn: Function) {
let wasCalled = false;

return function wrapper() {
if (wasCalled) {
return;
}

wasCalled = true;
fn.apply(null, arguments);
};
}
11 changes: 5 additions & 6 deletions nativescript-angular/directives/dialogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ProxyViewContainer } from "tns-core-modules/ui/proxy-view-container/pro
import { AppHostView } from "../app-host-view";
import { DetachedLoader } from "../common/detached-loader";
import { PageFactory, PAGE_FACTORY } from "../platform-providers";
import { once } from "../common/utils";

export interface ModalDialogOptions {
context?: any;
Expand Down Expand Up @@ -106,19 +107,17 @@ export class ModalDialogService {
let componentView: View;
let detachedLoaderRef: ComponentRef<DetachedLoader>;

const closeCallback = (...args) => {
const closeCallback = once((...args) => {
doneCallback.apply(undefined, args);
if (componentView && !this.location._isModalClosing) {
if (componentView) {
this.location._beginCloseModalNavigation();

componentView.closeModal();
} else if (this.location._isModalClosing) {
this.location.back();
this.location._finishCloseModalNavigation();
detachedLoaderRef.instance.detectChanges();
detachedLoaderRef.destroy();
}
};
});

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

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