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 };
|
export { Observable };
|
||||||
|
|
||||||
import { UnhandledErrorEventData, iOSApplication, AndroidApplication, CssChangedEventData, LoadAppCSSEventData } from ".";
|
import {
|
||||||
|
AndroidApplication,
|
||||||
|
CssChangedEventData,
|
||||||
|
getRootView,
|
||||||
|
iOSApplication,
|
||||||
|
LoadAppCSSEventData,
|
||||||
|
UnhandledErrorEventData
|
||||||
|
} from "./application";
|
||||||
|
|
||||||
export { UnhandledErrorEventData, CssChangedEventData, LoadAppCSSEventData };
|
export { UnhandledErrorEventData, CssChangedEventData, LoadAppCSSEventData };
|
||||||
|
|
||||||
@@ -73,8 +80,19 @@ export function setApplication(instance: iOSApplication | AndroidApplication): v
|
|||||||
export function livesync(context?: HmrContext) {
|
export function livesync(context?: HmrContext) {
|
||||||
events.notify(<EventData>{ eventName: "livesync", object: app });
|
events.notify(<EventData>{ eventName: "livesync", object: app });
|
||||||
const liveSyncCore = global.__onLiveSyncCore;
|
const liveSyncCore = global.__onLiveSyncCore;
|
||||||
if (liveSyncCore) {
|
let reapplyAppCss = false
|
||||||
liveSyncCore(context);
|
|
||||||
|
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 { createViewFromEntry } from "../ui/builder";
|
||||||
import { ios as iosView, View } from "../ui/core/view";
|
import { ios as iosView, View } from "../ui/core/view";
|
||||||
import { Frame, NavigationEntry } from "../ui/frame";
|
import { Frame, NavigationEntry } from "../ui/frame";
|
||||||
import { loadCss } from "../ui/styling/style-scope";
|
|
||||||
import * as utils from "../utils/utils";
|
import * as utils from "../utils/utils";
|
||||||
import { profile, level as profilingLevel, Level } from "../profiling";
|
import { profile, level as profilingLevel, Level } from "../profiling";
|
||||||
|
|
||||||
@@ -226,23 +225,12 @@ class IOSApplication implements IOSApplicationDefinition {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public _onLivesync(context?: HmrContext): void {
|
public _onLivesync(): 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 view can't handle livesync set window controller.
|
||||||
if (!this._rootView._onLivesync()) {
|
if (!this._rootView._onLivesync()) {
|
||||||
this.setWindowContent();
|
this.setWindowContent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public setWindowContent(view?: View): void {
|
public setWindowContent(view?: View): void {
|
||||||
if (this._rootView) {
|
if (this._rootView) {
|
||||||
@@ -276,8 +264,8 @@ exports.ios = iosApp;
|
|||||||
setApplication(iosApp);
|
setApplication(iosApp);
|
||||||
|
|
||||||
// attach on global, so it can be overwritten in NativeScript Angular
|
// attach on global, so it can be overwritten in NativeScript Angular
|
||||||
(<any>global).__onLiveSyncCore = function __onLiveSyncCore(context?: HmrContext) {
|
(<any>global).__onLiveSyncCore = function () {
|
||||||
iosApp._onLivesync(context);
|
iosApp._onLivesync();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mainEntry: NavigationEntry;
|
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;
|
__inspector?: any;
|
||||||
__extends: any;
|
__extends: any;
|
||||||
__onLiveSync: (context?: { type: string, module: string }) => void;
|
__onLiveSync: (context?: { type: string, module: string }) => void;
|
||||||
__onLiveSyncCore: (context?: { type: string, module: string }) => void;
|
__onLiveSyncCore: () => void;
|
||||||
__onUncaughtError: (error: NativeScriptError) => void;
|
__onUncaughtError: (error: NativeScriptError) => void;
|
||||||
TNS_WEBPACK?: boolean;
|
TNS_WEBPACK?: boolean;
|
||||||
__requireOverride?: (name: string, dir: string) => any;
|
__requireOverride?: (name: string, dir: string) => any;
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ import {
|
|||||||
_updateTransitions, _reverseTransitions, _clearEntry, _clearFragment, AnimationType
|
_updateTransitions, _reverseTransitions, _clearEntry, _clearFragment, AnimationType
|
||||||
} from "./fragment.transitions";
|
} from "./fragment.transitions";
|
||||||
|
|
||||||
import { loadCss } from "../styling/style-scope";
|
|
||||||
import { profile } from "../../profiling";
|
import { profile } from "../../profiling";
|
||||||
|
|
||||||
// TODO: Remove this and get it from global to decouple builder for angular
|
// TODO: Remove this and get it from global to decouple builder for angular
|
||||||
@@ -83,26 +82,15 @@ function getAttachListener(): android.view.View.OnAttachStateChangeListener {
|
|||||||
return attachStateChangeListener;
|
return attachStateChangeListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function reloadPage(context?: HmrContext): void {
|
export function reloadPage(): void {
|
||||||
const activity = application.android.foregroundActivity;
|
const activity = application.android.foregroundActivity;
|
||||||
const callbacks: AndroidActivityCallbacks = activity[CALLBACKS];
|
const callbacks: AndroidActivityCallbacks = activity[CALLBACKS];
|
||||||
const rootView: View = callbacks.getRootView();
|
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()) {
|
if (!rootView || !rootView._onLivesync()) {
|
||||||
callbacks.resetActivityContent(activity);
|
callbacks.resetActivityContent(activity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// attach on global, so it can be overwritten in NativeScript Angular
|
// attach on global, so it can be overwritten in NativeScript Angular
|
||||||
(<any>global).__onLiveSyncCore = reloadPage;
|
(<any>global).__onLiveSyncCore = reloadPage;
|
||||||
|
|||||||
@@ -103,9 +103,12 @@ export class PageBase extends ContentView implements PageDefinition {
|
|||||||
public onNavigatingTo(context: any, isBackNavigation: boolean, bindingContext?: any) {
|
public onNavigatingTo(context: any, isBackNavigation: boolean, bindingContext?: any) {
|
||||||
this._navigationContext = context;
|
this._navigationContext = context;
|
||||||
|
|
||||||
|
if (isBackNavigation && this._styleScope) {
|
||||||
|
this._styleScope.ensureSelectors();
|
||||||
if (!this._cssState.isSelectorsLatestVersionApplied()) {
|
if (!this._cssState.isSelectorsLatestVersionApplied()) {
|
||||||
this._onCssStateChange();
|
this._onCssStateChange();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//https://github.com/NativeScript/NativeScript/issues/731
|
//https://github.com/NativeScript/NativeScript/issues/731
|
||||||
if (!isBackNavigation && bindingContext !== undefined && bindingContext !== null) {
|
if (!isBackNavigation && bindingContext !== undefined && bindingContext !== null) {
|
||||||
|
|||||||
@@ -307,7 +307,7 @@ function onLiveSync(args: applicationCommon.CssChangedEventData): void {
|
|||||||
loadCss(applicationCommon.getCssFileName());
|
loadCss(applicationCommon.getCssFileName());
|
||||||
}
|
}
|
||||||
|
|
||||||
export const loadCss = profile(`"style-scope".loadCss`, (cssFile: string) => {
|
const loadCss = profile(`"style-scope".loadCss`, (cssFile: string) => {
|
||||||
if (!cssFile) {
|
if (!cssFile) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@@ -369,12 +369,7 @@ export class CssState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public isSelectorsLatestVersionApplied(): boolean {
|
public isSelectorsLatestVersionApplied(): boolean {
|
||||||
if (this._appliedSelectorsVersion && this.view._styleScope) {
|
|
||||||
this.view._styleScope.ensureSelectors();
|
|
||||||
return this.view._styleScope._getSelectorsVersion() === this._appliedSelectorsVersion;
|
return this.view._styleScope._getSelectorsVersion() === this._appliedSelectorsVersion;
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public onLoaded(): void {
|
public onLoaded(): void {
|
||||||
|
|||||||
Reference in New Issue
Block a user