From d38e99cabd5dee8496a8de4da015579ae2a035bc Mon Sep 17 00:00:00 2001 From: Panayot Cankov Date: Mon, 13 Mar 2017 09:49:41 +0200 Subject: [PATCH] Expose hasLaunched() on the application, style-scope will load css-es if app hasLaunched --- .../application/application-common.ts | 18 +++++++++++++----- tns-core-modules/application/application.d.ts | 10 ++++++++++ tns-core-modules/ui/styling/style-scope.ts | 11 ++++++++++- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/tns-core-modules/application/application-common.ts b/tns-core-modules/application/application-common.ts index b55b33096..ef4f4c3fe 100644 --- a/tns-core-modules/application/application-common.ts +++ b/tns-core-modules/application/application-common.ts @@ -4,8 +4,16 @@ require("globals"); import { Observable, EventData } from "data/observable"; const events = new Observable(); -// First merge all functions from events into application-common so that later appModule.on will be defined. -global.moduleMerge(events, exports); +let launched = false; +function setLaunched() { + launched = true; + events.off("launch", setLaunched); +} +events.on("launch", setLaunched); + +export function hasLaunched(): boolean { + return launched; +} export { Observable }; @@ -34,9 +42,9 @@ export function setResources(res: any) { export let android = undefined; export let ios = undefined; -export function notify(args: EventData): void { - events.notify(args); -} +export const on: typeof events.on = events.on.bind(events); +export const off: typeof events.off = events.off.bind(events); +export const notify: typeof events.notify = events.notify.bind(events); let app: iOSApplication | AndroidApplication; export function setApplication(instance: iOSApplication | AndroidApplication): void { diff --git a/tns-core-modules/application/application.d.ts b/tns-core-modules/application/application.d.ts index e6f4702bb..8dc7af34d 100644 --- a/tns-core-modules/application/application.d.ts +++ b/tns-core-modules/application/application.d.ts @@ -148,6 +148,11 @@ export function on(event: "cssChanged", callback: (args: CssChangedEventData) => */ export function on(event: "livesync", callback: (args: EventData) => void); +/** + * Removes listener for the specified event name. + */ +export function off(eventNames: string, callback?: any, thisArg?: any); + /** * Call this method to start the application. Important: All code after this method call will not be executed! */ @@ -531,3 +536,8 @@ export interface RootViewControllerImpl { } export function getNativeApplication(): any; + +/** + * Indicates if the application is allready launched. See also the `application.on("launch", handler)` event. + */ +export function hasLaunched(): boolean; \ No newline at end of file diff --git a/tns-core-modules/ui/styling/style-scope.ts b/tns-core-modules/ui/styling/style-scope.ts index e5c30f56c..c70d9fc70 100644 --- a/tns-core-modules/ui/styling/style-scope.ts +++ b/tns-core-modules/ui/styling/style-scope.ts @@ -74,7 +74,16 @@ function loadCss(cssFile?: string): RuleSet[] { application.on("cssChanged", onCssChanged); application.on("livesync", onLiveSync); -application.on("launch", () => loadCss(application.getCssFileName())); + +function loadCssOnLaunch() { + loadCss(application.getCssFileName()); + application.off("launch", loadCssOnLaunch); +} +if (application.hasLaunched()) { + loadCssOnLaunch(); +} else { + application.on("launch", loadCssOnLaunch); +} let pattern: RegExp = /('|")(.*?)\1/;