-
Notifications
You must be signed in to change notification settings - Fork 19
/
wxhttptask.ts
149 lines (130 loc) · 2.79 KB
/
wxhttptask.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
declare const wx;
import * as utils from "../util"
export enum WxResponseType
{
Success = 1,
Fail = 2,
Complete = 3,
}
export interface WxBaseParams
{
success?:(resp) => void;
fail?:(resp) => void;
complete?:(resp) => void;
}
interface WxLoginParams extends WxBaseParams
{
timeout?:number;
}
interface WxUserInfoParams extends WxBaseParams
{
withCredentials?:boolean;
lang?:string;
timeout?:number;
}
interface WxRequestParams extends WxBaseParams
{
url:string;
data?:any;
header?:object;
method?:string;
dataType?:string;
responseType?:string;
}
export type WxTaskParams = WxLoginParams|WxRequestParams|WxUserInfoParams;
export class WxTaskPool
{
private pools:Map<WxTaskType, WxTask[]>;
constructor()
{
this.pools = new Map();
}
get(taskType:WxTaskType)
{
const pool = this.pools.get(taskType);
if(pool && pool.length)
{
const task = pool.pop();
task.isInPool = false;
return task;
}
return null;
}
put(taskType:WxTaskType, task:WxTask)
{
if(task.isInPool)
{
// console.log("already in pool");
return;
}
let pool = this.pools.get(taskType);
if(!pool)
{
pool = [];
this.pools.set(taskType, pool);
}
task.isInPool = true;
pool.push(task);
}
}
export enum WxTaskType
{
Login = "Loin",
UserInfo = "UserInfo",
Request = "Request",
}
export abstract class WxTask
{
public isInPool:boolean;
protected params:WxTaskParams;
protected cb:utils.handler;
protected datas:any[];
private taskType:WxTaskType;
constructor(taskType:WxTaskType)
{
this.taskType = taskType;
}
init(params:WxTaskParams, cb:utils.handler, ...datas)
{
this.isInPool = false;
this.params = params;
this.cb = cb;
this.datas = datas;
}
exec()
{
this.params.success = resp => {
this.cb.exec(this.taskType, this, WxResponseType.Success, resp, ...this.datas);
};
this.params.fail = resp => {
this.cb.exec(this.taskType, this, WxResponseType.Fail, resp, ...this.datas);
};
// this.params.complete = resp => {
// this.cb.exec(this.taskType, this, WxResponseType.Complete, resp, ...this.datas);
// }
}
}
export class WxLoginTask extends WxTask
{
exec()
{
super.exec();
wx.login(this.params);
}
}
export class WxUserInfoTask extends WxTask
{
exec()
{
super.exec();
wx.getUserInfo(this.params);
}
}
export class WxRequestTask extends WxTask
{
exec()
{
super.exec();
wx.request(this.params);
}
}