From 86b048b3b457d0e72eba11e678f6bf9fdce34123 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Wed, 17 Jul 2019 16:04:45 +0800 Subject: [PATCH] use global define to touch context --- js-framework/index.ts | 17 ++++++++++++++--- js-framework/src/runtime/global.ts | 6 ++++++ js-framework/src/runtime/sandbox.ts | 4 ++++ js-framework/src/ui/page.ts | 10 ++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 js-framework/src/runtime/global.ts diff --git a/js-framework/index.ts b/js-framework/index.ts index de1f9627..810399d9 100644 --- a/js-framework/index.ts +++ b/js-framework/index.ts @@ -1,5 +1,11 @@ import { Text, Alignment, VLayout, Gravity } from "./src/ui/view"; import { Color } from "./src/util/color"; +import { Page, Registor } from "./src/ui/page"; + +export * from "./src/ui/view" +export * from "./src/ui/page" +export * from "./src/util/color" + const v = new Text v.width = 20 @@ -15,6 +21,11 @@ console.log(v.toModel()) const layout = new VLayout layout.space = 10 console.log(layout.toModel()) -export * from "./src/ui/view" -export * from "./src/ui/page" -export * from "./src/util/color" + +@Registor +class MyPage extends Page { + build(): import("./src/ui/view").View { + throw new Error("Method not implemented."); + } +} +console.log('end') \ No newline at end of file diff --git a/js-framework/src/runtime/global.ts b/js-framework/src/runtime/global.ts new file mode 100644 index 00000000..7e8d3529 --- /dev/null +++ b/js-framework/src/runtime/global.ts @@ -0,0 +1,6 @@ +import { Context } from "./sandbox"; + +declare global { + const context: Context +} +export { } \ No newline at end of file diff --git a/js-framework/src/runtime/sandbox.ts b/js-framework/src/runtime/sandbox.ts index c4ab6f35..31e4385d 100644 --- a/js-framework/src/runtime/sandbox.ts +++ b/js-framework/src/runtime/sandbox.ts @@ -77,6 +77,7 @@ export function jsCallReject(contextId: string, callbackId: string, args?: any) } export class Context { + holder: any id: string callbacks: Map = new Map constructor(id: string) { @@ -93,6 +94,9 @@ export class Context { }) }) } + registor(instance: Object) { + this.holder = instance + } } diff --git a/js-framework/src/ui/page.ts b/js-framework/src/ui/page.ts index c958c9f6..9b418b05 100644 --- a/js-framework/src/ui/page.ts +++ b/js-framework/src/ui/page.ts @@ -1,4 +1,14 @@ import { View } from "./view"; +import { } from '../runtime/global' + +export function Registor(constructor: T) { + const ret = class extends constructor { + context = context + } + context.registor(new ret) + return ret +} + export abstract class Page { onCreate(): void { }