mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
refactor(HMR): Refactor livesync propagaton
This commit is contained in:
@@ -37,18 +37,25 @@ function ensureAnimationModule() {
|
||||
}
|
||||
}
|
||||
|
||||
export enum ModuleType {
|
||||
markup = "markup",
|
||||
script = "script",
|
||||
style = "style"
|
||||
}
|
||||
|
||||
export function CSSType(type: string): ClassDecorator {
|
||||
return (cls) => {
|
||||
cls.prototype.cssType = type;
|
||||
};
|
||||
}
|
||||
|
||||
export function viewMatchesModuleContext(
|
||||
view: ViewDefinition,
|
||||
context: ModuleContext,
|
||||
types: ModuleType[]): boolean {
|
||||
|
||||
return context &&
|
||||
view._moduleName &&
|
||||
context.type &&
|
||||
types.some(type => type === context.type) &&
|
||||
context.path &&
|
||||
context.path.includes(view._moduleName);
|
||||
}
|
||||
|
||||
export function PseudoClassHandler(...pseudoClasses: string[]): MethodDecorator {
|
||||
const stateEventNames = pseudoClasses.map(s => ":" + s);
|
||||
const listeners = Symbol("listeners");
|
||||
@@ -147,62 +154,36 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
|
||||
traceWrite(`${this}._onLivesync(${JSON.stringify(context)})`, traceCategories.Livesync);
|
||||
}
|
||||
|
||||
_rootModalViews.forEach(v => v.closeModal());
|
||||
_rootModalViews.length = 0;
|
||||
|
||||
if (context && context.type && context.path) {
|
||||
// Handle local styles
|
||||
if (context.type === ModuleType.style) {
|
||||
return this._changeLocalStyles(context.path);
|
||||
}
|
||||
// Handle module markup and script changes
|
||||
else {
|
||||
return this.changeModule(context);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public _changeLocalStyles(contextPath: string): boolean {
|
||||
if (!this.changeStyles(this, contextPath)) {
|
||||
eachDescendant(this, (child: ViewBase) => {
|
||||
this.changeStyles(child, contextPath);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
// Do not reset activity/window content for local styles changes
|
||||
return true;
|
||||
}
|
||||
|
||||
private changeStyles(view: ViewBase, contextPath: string): boolean {
|
||||
if (traceEnabled()) {
|
||||
traceWrite(`${view}.${view._moduleName}`, traceCategories.Livesync);
|
||||
}
|
||||
|
||||
if (view._moduleName && contextPath.includes(view._moduleName)) {
|
||||
(<this>view).changeCssFile(contextPath);
|
||||
if (this._handleLivesync(context)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private changeModule(context: ModuleContext): boolean {
|
||||
eachDescendant(this, (child: ViewBase) => {
|
||||
if (traceEnabled()) {
|
||||
traceWrite(`${child}.${child._moduleName}`, traceCategories.Livesync);
|
||||
let handled = false;
|
||||
this.eachChildView((child) => {
|
||||
if (child._onLivesync(context)) {
|
||||
handled = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Handle changes in module's Page
|
||||
if (child._moduleName && context.path.includes(child._moduleName) && child.page) {
|
||||
child.page._onLivesync(context);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return handled;
|
||||
}
|
||||
|
||||
// Do not reset activity/window content for module changes
|
||||
return true;
|
||||
public _handleLivesync(context?: ModuleContext): boolean {
|
||||
if (traceEnabled()) {
|
||||
traceWrite(`${this}._handleLivesync(${JSON.stringify(context)})`, traceCategories.Livesync);
|
||||
}
|
||||
|
||||
// Handle local CSS
|
||||
if (viewMatchesModuleContext(this, context, ["style"])) {
|
||||
if (traceEnabled()) {
|
||||
traceWrite(`Change Handled: Changing CSS for ${this}`, traceCategories.Livesync);
|
||||
}
|
||||
|
||||
this.changeCssFile(context.path);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
_setupAsRootView(context: any): void {
|
||||
|
||||
13
tns-core-modules/ui/core/view/view.d.ts
vendored
13
tns-core-modules/ui/core/view/view.d.ts
vendored
@@ -32,6 +32,17 @@ export function PseudoClassHandler(...pseudoClasses: string[]): MethodDecorator;
|
||||
*/
|
||||
export function CSSType(type: string): ClassDecorator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param view The view
|
||||
* @param context The ModuleType
|
||||
* @param type Type of the ModuleType to be matched
|
||||
*/
|
||||
export function viewMatchesModuleContext(
|
||||
view: View,
|
||||
context: ModuleContext,
|
||||
type: ModuleType[]): boolean;
|
||||
|
||||
/**
|
||||
* Denotes a length number that is in device independent pixel units.
|
||||
*/
|
||||
@@ -686,7 +697,7 @@ export abstract class View extends ViewBase {
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_changeLocalStyles(contextPath: string): boolean;
|
||||
_handleLivesync(context?: { type: string, path: string }): boolean;
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user