refactor(HMR): apply changes in application styles at runtime

This commit is contained in:
Vasil Chimev
2018-12-05 14:24:24 +02:00
parent 42a1491e6e
commit 790bcfb470
7 changed files with 49 additions and 57 deletions

View File

@@ -32,7 +32,14 @@ export function hasLaunched(): boolean {
export { Observable };
import { UnhandledErrorEventData, iOSApplication, AndroidApplication, CssChangedEventData, LoadAppCSSEventData } from ".";
import {
AndroidApplication,
CssChangedEventData,
getRootView,
iOSApplication,
LoadAppCSSEventData,
UnhandledErrorEventData
} from "./application";
export { UnhandledErrorEventData, CssChangedEventData, LoadAppCSSEventData };
@@ -73,8 +80,19 @@ export function setApplication(instance: iOSApplication | AndroidApplication): v
export function livesync(context?: HmrContext) {
events.notify(<EventData>{ eventName: "livesync", object: app });
const liveSyncCore = global.__onLiveSyncCore;
if (liveSyncCore) {
liveSyncCore(context);
let reapplyAppCss = false
if (context) {
const fullFileName = getCssFileName();
const fileName = fullFileName.substring(0, fullFileName.lastIndexOf(".") + 1);
const extensions = ["css", "scss"];
reapplyAppCss = extensions.some(ext => context.module === fileName.concat(ext));
}
if (reapplyAppCss) {
getRootView()._onCssStateChange();
} else if (liveSyncCore) {
liveSyncCore();
}
}

View File

@@ -18,7 +18,6 @@ export * from "./application-common";
import { createViewFromEntry } from "../ui/builder";
import { ios as iosView, View } from "../ui/core/view";
import { Frame, NavigationEntry } from "../ui/frame";
import { loadCss } from "../ui/styling/style-scope";
import * as utils from "../utils/utils";
import { profile, level as profilingLevel, Level } from "../profiling";
@@ -162,7 +161,7 @@ class IOSApplication implements IOSApplicationDefinition {
this.setWindowContent(args.root);
} else {
this._window = UIApplication.sharedApplication.delegate.window;
}
}
}
@profile
@@ -226,21 +225,10 @@ class IOSApplication implements IOSApplicationDefinition {
}
}
public _onLivesync(context?: HmrContext): void {
let executeLivesync = true;
// HMR has context, livesync does not
if (context) {
if (context.module === getCssFileName()) {
loadCss(context.module);
this._rootView._onCssStateChange();
executeLivesync = false;
}
}
if (executeLivesync) {
// If view can't handle livesync set window controller.
if (!this._rootView._onLivesync()) {
this.setWindowContent();
}
public _onLivesync(): void {
// If view can't handle livesync set window controller.
if (!this._rootView._onLivesync()) {
this.setWindowContent();
}
}
@@ -276,8 +264,8 @@ exports.ios = iosApp;
setApplication(iosApp);
// attach on global, so it can be overwritten in NativeScript Angular
(<any>global).__onLiveSyncCore = function __onLiveSyncCore(context?: HmrContext) {
iosApp._onLivesync(context);
(<any>global).__onLiveSyncCore = function () {
iosApp._onLivesync();
}
let mainEntry: NavigationEntry;
@@ -391,4 +379,4 @@ global.__onLiveSync = function __onLiveSync(context?: HmrContext) {
}
livesync(context);
}
}