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

Expose `HmrContext` interface.
Apply changes in `app.css` instantly.
Avoid navigation on livesync when changes in `app.css` have been made.
Apply changes in `app.css` on back navigation.
This commit is contained in:
Vasil Chimev
2018-10-04 19:20:13 +03:00
parent 60957799ad
commit 42a1491e6e
9 changed files with 110 additions and 34 deletions

View File

@ -70,11 +70,11 @@ export function setApplication(instance: iOSApplication | AndroidApplication): v
app = instance;
}
export function livesync() {
export function livesync(context?: HmrContext) {
events.notify(<EventData>{ eventName: "livesync", object: app });
const liveSyncCore = global.__onLiveSyncCore;
if (liveSyncCore) {
liveSyncCore();
liveSyncCore(context);
}
}

View File

@ -212,12 +212,12 @@ export function getNativeApplication(): android.app.Application {
return nativeApp;
}
global.__onLiveSync = function () {
global.__onLiveSync = function __onLiveSync(context?: HmrContext) {
if (androidApp && androidApp.paused) {
return;
}
livesync();
livesync(context);
};
function initLifecycleCallbacks() {

View File

@ -18,6 +18,7 @@ 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";
@ -225,12 +226,23 @@ class IOSApplication implements IOSApplicationDefinition {
}
}
public _onLivesync(): void {
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 setWindowContent(view?: View): void {
if (this._rootView) {
@ -264,8 +276,8 @@ exports.ios = iosApp;
setApplication(iosApp);
// attach on global, so it can be overwritten in NativeScript Angular
(<any>global).__onLiveSyncCore = function () {
iosApp._onLivesync();
(<any>global).__onLiveSyncCore = function __onLiveSyncCore(context?: HmrContext) {
iosApp._onLivesync(context);
}
let mainEntry: NavigationEntry;
@ -373,10 +385,10 @@ function setViewControllerView(view: View): void {
}
}
global.__onLiveSync = function () {
global.__onLiveSync = function __onLiveSync(context?: HmrContext) {
if (!started) {
return;
}
livesync();
livesync(context);
}

View File

@ -51,8 +51,8 @@ declare namespace NodeJS {
__native?: any;
__inspector?: any;
__extends: any;
__onLiveSync: () => void;
__onLiveSyncCore: () => void;
__onLiveSync: (context?: { type: string, module: string }) => void;
__onLiveSyncCore: (context?: { type: string, module: string }) => void;
__onUncaughtError: (error: NativeScriptError) => void;
TNS_WEBPACK?: boolean;
__requireOverride?: (name: string, dir: string) => any;
@ -64,6 +64,27 @@ declare function clearTimeout(timeoutId: number): void;
declare function setInterval(callback: (...args: any[]) => void, ms: number, ...args: any[]): number;
declare function clearInterval(intervalId: number): void;
declare enum HmrType {
markup = "markup",
script = "script",
style = "style"
}
/**
* Define a context for Hot Module Replacement.
*/
interface HmrContext {
/**
* The type of module for replacement.
*/
type: HmrType;
/**
* The module for replacement.
*/
module: string;
}
/**
* An extended JavaScript Error which will have the nativeError property initialized in case the error is caused by executing platform-specific code.
*/

View File

@ -17,6 +17,7 @@ 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
@ -82,15 +83,26 @@ function getAttachListener(): android.view.View.OnAttachStateChangeListener {
return attachStateChangeListener;
}
export function reloadPage(): void {
export function reloadPage(context?: HmrContext): 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
(<any>global).__onLiveSyncCore = reloadPage;

View File

@ -103,6 +103,10 @@ export class PageBase extends ContentView implements PageDefinition {
public onNavigatingTo(context: any, isBackNavigation: boolean, bindingContext?: any) {
this._navigationContext = context;
if (!this._cssState.isSelectorsLatestVersionApplied()) {
this._onCssStateChange();
}
//https://github.com/NativeScript/NativeScript/issues/731
if (!isBackNavigation && bindingContext !== undefined && bindingContext !== null) {
this.bindingContext = bindingContext;

View File

@ -19,6 +19,11 @@ export class CssState {
* Gets the static selectors that match the view and the dynamic selectors that may potentially match the view.
*/
public changeMap: ChangeMap<ViewBase>;
/**
* Checks whether style scope and CSS state selectors are in sync.
*/
public isSelectorsLatestVersionApplied(): boolean
}
export class StyleScope {
@ -29,6 +34,9 @@ export class StyleScope {
public static createSelectorsFromImports(tree: SyntaxTree, keyframes: Object): RuleSet[];
public ensureSelectors(): number;
public isApplicationCssSelectorsLatestVersionApplied(): boolean;
public isLocalCssSelectorsLatestVersionApplied(): boolean;
public applySelectors(view: ViewBase): void
public query(options: Node): SelectorCore[];

View File

@ -307,7 +307,7 @@ function onLiveSync(args: applicationCommon.CssChangedEventData): void {
loadCss(applicationCommon.getCssFileName());
}
const loadCss = profile(`"style-scope".loadCss`, (cssFile: string) => {
export const loadCss = profile(`"style-scope".loadCss`, (cssFile: string) => {
if (!cssFile) {
return undefined;
}
@ -343,6 +343,7 @@ export class CssState {
_appliedChangeMap: Readonly<ChangeMap<ViewBase>>;
_appliedPropertyValues: Readonly<{}>;
_appliedAnimations: ReadonlyArray<kam.KeyframeAnimation>;
_appliedSelectorsVersion: number;
_match: SelectorsMatch<ViewBase>;
_matchInvalid: boolean;
@ -367,6 +368,15 @@ 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 {
if (this._matchInvalid) {
this.updateMatch();
@ -381,6 +391,7 @@ export class CssState {
@profile
private updateMatch() {
this._appliedSelectorsVersion = this.view._styleScope._getSelectorsVersion();
this._match = this.view._styleScope ? this.view._styleScope.matchSelectors(this.view) : CssState.emptyMatch;
this._matchInvalid = false;
}
@ -597,8 +608,8 @@ export class StyleScope {
}
public ensureSelectors(): number {
if (this._applicationCssSelectorsAppliedVersion !== applicationCssSelectorVersion ||
this._localCssSelectorVersion !== this._localCssSelectorsAppliedVersion ||
if (!this.isApplicationCssSelectorsLatestVersionApplied() ||
!this.isLocalCssSelectorsLatestVersionApplied() ||
!this._mergedCssSelectors) {
this._createSelectors();
@ -607,6 +618,14 @@ export class StyleScope {
return this._getSelectorsVersion();
}
public isApplicationCssSelectorsLatestVersionApplied(): boolean {
return this._applicationCssSelectorsAppliedVersion === applicationCssSelectorVersion;
}
public isLocalCssSelectorsLatestVersionApplied(): boolean {
return this._localCssSelectorsAppliedVersion === this._localCssSelectorVersion;
}
@profile
private _createSelectors() {
let toMerge: RuleSet[][] = [];