From 369a488af6e6b389cb211cca60114f5faffbb7a8 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sun, 21 Jul 2019 05:50:54 +0800 Subject: [PATCH] view build flow --- js-framework/demo.ts | 4 +- js-framework/package-lock.json | 3 +- js-framework/package.json | 2 +- js-framework/src/ui/panel.ts | 70 +++++++++++++++++++++++++++++----- js-framework/src/ui/view.ts | 4 +- 5 files changed, 68 insertions(+), 15 deletions(-) diff --git a/js-framework/demo.ts b/js-framework/demo.ts index 933c0e54..cdea813b 100644 --- a/js-framework/demo.ts +++ b/js-framework/demo.ts @@ -1,4 +1,4 @@ -import { Text, Alignment, Color, VLayout, Registor, Panel, log, logw, loge } from "./index" +import { Text, Alignment, Color, VLayout, Link, Panel, log, logw, loge } from "./index" const v = new Text v.width = 20 @@ -30,7 +30,7 @@ setTimeout(() => { clearInterval(timerId) }, 5000) -@Registor(context) +@Link(context) export class MyPage extends Panel { build() { return layout diff --git a/js-framework/package-lock.json b/js-framework/package-lock.json index 408c6add..c0647c2e 100644 --- a/js-framework/package-lock.json +++ b/js-framework/package-lock.json @@ -1,8 +1,7 @@ { - "name": "hego-js-framework", + "name": "doric-js-framework", "version": "0.1.0", "lockfileVersion": 1, - "requires": true, "dependencies": { "@types/estree": { "version": "0.0.39", diff --git a/js-framework/package.json b/js-framework/package.json index c1465a51..1d0102fb 100644 --- a/js-framework/package.json +++ b/js-framework/package.json @@ -26,4 +26,4 @@ "rollup-plugin-node-resolve": "^5.2.0", "tslib": "^1.10.0" } -} \ No newline at end of file +} diff --git a/js-framework/src/ui/panel.ts b/js-framework/src/ui/panel.ts index b9b73840..62561be6 100644 --- a/js-framework/src/ui/panel.ts +++ b/js-framework/src/ui/panel.ts @@ -1,6 +1,8 @@ -import { View } from "./view"; +import { } from './../runtime/global'; +import { View, Stack, Group } from "./view"; +import { log } from 'util'; -export function Registor(context: any) { +export function Link(context: any) { return (constructor: T) => { const ret = class extends constructor { context = context @@ -10,41 +12,91 @@ export function Registor(context: any) { } } +export function NativeCall(target: Panel, propertyKey: string, descriptor: PropertyDescriptor) { + const originVal = descriptor.value + descriptor.value = function () { + const args = [] + for (let index in arguments) { + args.push(arguments[index]); + } + Reflect.apply(Reflect.get(target, '__hookBeforeNativeCall__'), this, []) + const ret = Reflect.apply(originVal, this, args) + Reflect.apply(Reflect.get(target, '__hookAfterNativeCall__'), this, []) + return ret + } + return descriptor +} + +type Frame = { width: number, height: number } export abstract class Panel { + onCreate() { } onDestory() { } onShow() { } onHidden() { } - abstract build(): View + abstract build(rootView: Group): void + private __data__: any + private __rootView__: Group = new Stack - /** - * Native Call - */ + getRootView() { + return this.__rootView__ + } + getInitData() { + return this.__data__ + } + + @NativeCall + private __init__(frame: Frame, data: any) { + this.__data__ = data + this.__rootView__.width = frame.width + this.__rootView__.height = frame.height + this.build(this.__rootView__) + } + + @NativeCall private __onCreate__() { - Reflect.defineMetadata(Symbol.for("context"), context, Reflect.getPrototypeOf(context)) this.onCreate() } + @NativeCall private __onDestory__() { this.onDestory() } + @NativeCall private __onShow__() { this.onShow() } + @NativeCall private __onHidden__(): void { this.onHidden() } - private __build__(): View { - return this.build() + @NativeCall + private __build__() { + this.build(this.__rootView__) } + @NativeCall private __responedCallback__(viewId: string, callbackId: string) { } + + private __hookBeforeNativeCall__() { + log('__hookBeforeNativeCall__') + } + + private __hookAfterNativeCall__() { + log('__hookAfterNativeCall__') + if (this.__rootView__.isDirty()) { + const model = this.__rootView__.toModel() + log('needRefresh:' + JSON.stringify(model)) + this.__rootView__.clean() + } + } + } \ No newline at end of file diff --git a/js-framework/src/ui/view.ts b/js-framework/src/ui/view.ts index a417e2d6..16f78658 100644 --- a/js-framework/src/ui/view.ts +++ b/js-framework/src/ui/view.ts @@ -125,7 +125,9 @@ export abstract class View implements Modeling { } } } - + isDirty() { + return Reflect.ownKeys(this.__dirty_props__).length === 0 + } responseCallback(id: string) { const f = this.id2Callback(id) if (f instanceof Function) {