mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
refactor(HMR): apply changes in application styles at runtime
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -226,23 +225,12 @@ 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) {
|
||||
public _onLivesync(): void {
|
||||
// If view can't handle livesync set window controller.
|
||||
if (!this._rootView._onLivesync()) {
|
||||
this.setWindowContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public setWindowContent(view?: View): void {
|
||||
if (this._rootView) {
|
||||
@@ -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;
|
||||
|
||||
2
tns-core-modules/module.d.ts
vendored
2
tns-core-modules/module.d.ts
vendored
@@ -52,7 +52,7 @@ declare namespace NodeJS {
|
||||
__inspector?: any;
|
||||
__extends: any;
|
||||
__onLiveSync: (context?: { type: string, module: string }) => void;
|
||||
__onLiveSyncCore: (context?: { type: string, module: string }) => void;
|
||||
__onLiveSyncCore: () => void;
|
||||
__onUncaughtError: (error: NativeScriptError) => void;
|
||||
TNS_WEBPACK?: boolean;
|
||||
__requireOverride?: (name: string, dir: string) => any;
|
||||
|
||||
@@ -17,7 +17,6 @@ import {
|
||||
_updateTransitions, _reverseTransitions, _clearEntry, _clearFragment, AnimationType
|
||||
} from "./fragment.transitions";
|
||||
|
||||
import { loadCss } from "../styling/style-scope";
|
||||
import { profile } from "../../profiling";
|
||||
|
||||
// TODO: Remove this and get it from global to decouple builder for angular
|
||||
@@ -83,25 +82,14 @@ function getAttachListener(): android.view.View.OnAttachStateChangeListener {
|
||||
return attachStateChangeListener;
|
||||
}
|
||||
|
||||
export function reloadPage(context?: HmrContext): void {
|
||||
export function reloadPage(): void {
|
||||
const activity = application.android.foregroundActivity;
|
||||
const callbacks: AndroidActivityCallbacks = activity[CALLBACKS];
|
||||
const rootView: View = callbacks.getRootView();
|
||||
|
||||
let executeLivesync = true;
|
||||
// HMR has context, livesync does not
|
||||
if (context) {
|
||||
if (context.module === application.getCssFileName()) {
|
||||
loadCss(context.module);
|
||||
rootView._onCssStateChange();
|
||||
executeLivesync = false;
|
||||
}
|
||||
}
|
||||
if (executeLivesync) {
|
||||
if (!rootView || !rootView._onLivesync()) {
|
||||
callbacks.resetActivityContent(activity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// attach on global, so it can be overwritten in NativeScript Angular
|
||||
|
||||
@@ -103,9 +103,12 @@ export class PageBase extends ContentView implements PageDefinition {
|
||||
public onNavigatingTo(context: any, isBackNavigation: boolean, bindingContext?: any) {
|
||||
this._navigationContext = context;
|
||||
|
||||
if (isBackNavigation && this._styleScope) {
|
||||
this._styleScope.ensureSelectors();
|
||||
if (!this._cssState.isSelectorsLatestVersionApplied()) {
|
||||
this._onCssStateChange();
|
||||
}
|
||||
}
|
||||
|
||||
//https://github.com/NativeScript/NativeScript/issues/731
|
||||
if (!isBackNavigation && bindingContext !== undefined && bindingContext !== null) {
|
||||
|
||||
@@ -307,7 +307,7 @@ function onLiveSync(args: applicationCommon.CssChangedEventData): void {
|
||||
loadCss(applicationCommon.getCssFileName());
|
||||
}
|
||||
|
||||
export const loadCss = profile(`"style-scope".loadCss`, (cssFile: string) => {
|
||||
const loadCss = profile(`"style-scope".loadCss`, (cssFile: string) => {
|
||||
if (!cssFile) {
|
||||
return undefined;
|
||||
}
|
||||
@@ -369,12 +369,7 @@ export class CssState {
|
||||
}
|
||||
|
||||
public isSelectorsLatestVersionApplied(): boolean {
|
||||
if (this._appliedSelectorsVersion && this.view._styleScope) {
|
||||
this.view._styleScope.ensureSelectors();
|
||||
return this.view._styleScope._getSelectorsVersion() === this._appliedSelectorsVersion;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public onLoaded(): void {
|
||||
|
||||
Reference in New Issue
Block a user