forked from emeryao/typed-we-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runtime.ts
401 lines (354 loc) · 11.9 KB
/
Runtime.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/// <reference path="../we-app.d.ts" />
import {Reflect} from "./Reflect"
//========================================================
// ==================Application==========================
//========================================================
export abstract class WeChatApplication {
@MapToIgnore()
private __temporary__globalData: { [key: string]: any } = {};
get globalData(): { [key: string]: any } {
if (this.__runtime__initialized) {
return this.__runtime__app.globalData;
}
return this.__temporary__globalData;
}
set globalData(value) {
if (this.__runtime__initialized) {
this.__runtime__app.setData(value);
return;
}
this.__temporary__globalData = value;
}
@MapToIgnore()
protected __runtime__app: any;
@MapToIgnore()
private __runtime__initialized: boolean = false;
@MapToIgnore()
public __runtime__initialize(deep: any) {
if (this.__runtime__initialized) {
throw new Error(`type already inialized`);
}
this.__runtime__app = deep;
this.__runtime__initialized = true;
WeChatApplication.__runtime__static_current_app = this; // set currentApplication
}
/** Start you application
* @param app instance of WechatApplction or a constructor
*/
static bootstrap<T extends WeChatApplication>(app: WeChatApplication | ApplicationConstructor<T>) {
let appConfig: WeChatApplication;
if (app instanceof WeChatApplication) {
appConfig = app;
}
else if (typeof (app) === "function") {
appConfig = new app();
}
else {
throw new TypeError(`typeof app : must be a instance of WeChatApplication or a constructor of WebChatApplication`)
}
App(applicationConfigConvert(appConfig))
}
private static __runtime__static_current_app:WeChatApplication|null = null;
/**
* a helper method for replace wechat's @see GetGlobalApp()
*/
static GetCurrentApp<T=WeChatApplication>():T{
if(this.__runtime__static_current_app != null){
return this.__runtime__static_current_app as any; // improve code experience
}
console.warn(`You call WechatApplication.GetCurrentApp() so early and your will get a null! `)
return null as any;
}
}
export function applicationConfigConvert(app: WeChatApplication): WeApp.AppParam {
let config: WeApp.AppParam = {};
config.data = config.data;
config.onLaunch = function (...args) {
let that = this;
app.__runtime__initialize(that);
if(typeof(app["onLaunch"]) === "function"){
app["onLaunch"].apply(app,args);
}
}
for (let name in app) {
let has = Reflect.hasMetadata("ignore", app, name);
if (has) {
continue;
}
if (name === "onLaunch") {
continue;
}
let f = app[name];
if (typeof (f) === "function") {
config[name] = function (...args: any[]) {
return (<Function>f).apply(app, args);
}
}
else {
// undefined,null,string, symbol,number, boolean,object
// if(name == "data"){
// config[name] = page[name];
// continue;
// }
// else
if (f != null) {
//string, symbol,number, boolean,object
Object.defineProperty(config, name, {
get: () => {
return app[name];
},
set: (value) => {
app[name] = value;
},
enumerable: true,
configurable: true
})
}
}
}
console.log(config);
return config;
}
export interface ApplicationConstructor<T extends WeChatApplication> {
new(...args: any[]): T
}
//========================================================
// ==================Application.Lifetime==========================
//========================================================
export interface OnApplicationLaunch{
onLaunch(info:WeApp.LaunchData):void;
}
export interface OnApplicationShow{
onShow():void;///////////////////////////////////////////// Note here
}
export interface OnApplicationHide{
onHide():void;
}
export interface OnApplicationError{
onError(args?:any):void; ///////////////////////////////////////////// Note here
}
//========================================================
//=====================Page===============================
//========================================================
export abstract class WeChatPage {
protected __runtime__page: any;
private __runtime__initialized: boolean = false;
public __runtime__initialize(deep: any) {
if (this.__runtime__initialized) {
throw new Error(`type already inialized`);
}
this.__runtime__page = deep;
this.__runtime__initialized = true;
}
@MapToIgnore()
private __temporary__data: { [key: string]: any } = {};
get data(): { [key: string]: any } {
if (this.__runtime__initialized) {
return this.__runtime__page.data;
}
return this.__temporary__data;
}
set data(value) {
if (this.__runtime__initialized) {
this.__runtime__page.setData(value);
return;
}
this.__temporary__data = value;
}
@MapToIgnore()
setData(partialData: { [key: string]: any }) {
this.__runtime__page.setData(partialData);
}
static initPage<T extends WeChatPage>(page: WeChatPage | PageConstructor<T>) {
let pageConfig: WeChatPage;
if (page instanceof WeChatPage) {
pageConfig = page;
}
else if (typeof (page) === "function") {
pageConfig = new page();
}
else {
throw new TypeError(`typeof app : must be a instance of WeChatApplication or a constructor of WebChatApplication`)
}
Page(pageConfigConvert(pageConfig))
}
}
export function pageConfigConvert(page: WeChatPage): WeApp.PageParam {
let config: WeApp.PageParam = {};
config.data = config.data;
config.onLoad = function (...args) {
let that = this;
page.__runtime__initialize(that);
if(typeof(page["onLoad"]) === "function"){
page["onLoad"].apply(page,args);
}
}
for (let name in page) {
let has = Reflect.hasMetadata("ignore", page, name);
if (has) {
continue;
}
if (name === "onLoad") {
continue;
}
let f = page[name];
if (typeof (f) === "function") {
config[name] = function (...args: any[]) {
return (<Function>f).apply(page, args);
}
}
else {
// undefined,null,string, symbol,number, boolean,object
if (f != null) {
//string, symbol,number, boolean,object
Object.defineProperty(config, name, {
get: () => {
return page[name];
},
set: (value) => {
page[name] = value;
},
enumerable: true,
configurable: true
})
}
}
}
console.log(config);
return config;
}
export interface PageConstructor<T extends WeChatPage> {
new(...args: any[]): T
}
//========================================================
// ================Page.Lifetime==========================
//========================================================
export interface OnPageLoad {
onLoad(args: Map<string, any>): void | Promise<any>
}
export interface OnPageReady{
onReady():void;
}
export interface OnPageShow{
onShow():void;
}
export interface OnPageHide{
onHide():void;
}
export interface OnPageUnload {
onUnload(): void | Promise<any>
}
export interface OnPagePullDownRefresh{
onPullDownRefresh():void;
}
export interface OnPageReachBottom{
onReachBottom():void;
}
export interface OnPageShareAppMessage{
onShareAppMessage(options:{ from: string, target: WeApp.Target }):void;
}
export interface OnPageScroll{
onPageScroll():void;///////////////////////////////////////////// Note here
}
//========================================================
// ================Component==============================
//========================================================
/** wasn't ready for use */
export abstract class WechatComponent{
protected __runtime__component: any;
private __runtime__initialized: boolean = false;
public __runtime__initialize(deep: any) {
if (this.__runtime__initialized) {
throw new Error(`type already inialized`);
}
this.__runtime__component = deep;
this.__runtime__initialized = true;
}
@MapToIgnore()
private __temporary__data: { [key: string]: any } = {};
get data(): { [key: string]: any } {
if (this.__runtime__initialized) {
return this.__runtime__component.data;
}
return this.__temporary__data;
}
set data(value) {
if (this.__runtime__initialized) {
this.__runtime__component.setData(value);
return;
}
this.__temporary__data = value;
}
@MapToIgnore()
setData(partialData: { [key: string]: any }) {
this.__runtime__component.setData(partialData);
}
static initComponent<T extends WechatComponent>(component: WechatComponent | ComponentConstructor<T>) {
let componentInstance: WechatComponent;
if (component instanceof WechatComponent) {
componentInstance = component;
}
else if (typeof (component) === "function") {
componentInstance = new component();
}
else {
throw new TypeError(`typeof app : must be a instance of WeChatApplication or a constructor of WebChatApplication`)
}
Component(componentConfigConvert(componentInstance))
}
}
export interface ComponentConstructor<T>{
new(...args:any[]):T
}
export function componentConfigConvert(component:WechatComponent):WeApp.ComponentParam{
throw new Error(`not implement`);
}
//========================================================
// ================Component.Lifetime=====================
//========================================================
export interface OnComponentCreated{
created():void;
}
export interface OnComponentAttached{
attached():void;
}
export interface OnComponentReady{
ready():void;
}
export interface OnComponentMoved{
moved():void;
}
export interface OnComponentDetached{
detached():void;
}
//========================================================
// ================Metadata==============================
//========================================================
/**
* Tell the runtime that this method/property/field need to use specified name
* @param targetName Specified name
*/
export function MapTo(targetName:string){
return Reflect.metadata("MapTo",targetName);
}
/**
* Tell the runtime that this method/property/field do not need map to Config Object
*/
export function MapToIgnore() {
return Reflect.metadata(`ignore`, true);
}
/**
* Tell the runtime that this property/field **must** get/set back { Config => Class }
* Warning: this decorator can only apply to filed/property
*/
export function MapBackGetSet(){
return Reflect.metadata("nomap",true);
}
export function ComponentProperty(targetName?:string){
//let a : WeApp.ComponentParam;
let target = targetName === undefined ? null : targetName; // force null instead of undefined
return Reflect.metadata("component:property",target)
}
export function ComponentNospecial(){
return Reflect.metadata("component:nospecial",true)
}