-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathmulti.ts
More file actions
110 lines (96 loc) · 2.85 KB
/
multi.ts
File metadata and controls
110 lines (96 loc) · 2.85 KB
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
import { IPushScheduler, ScheduleOptions, IPushSchedulerOrCtor, MultiPushSchedulerOptions, IPushSelector } from "../interfaces/IPushScheduler";
import { Application } from "../application";
import { isFunction } from "util";
import { getLogger } from 'pinus-logger';
import { SID } from "../util/constants";
let logger = getLogger('pinus', __filename);
export class MultiPushScheduler implements IPushScheduler
{
app: Application;
selector : IPushSelector;
scheduler :{[id:number]:IPushScheduler};
constructor(app : Application, opts : MultiPushSchedulerOptions)
{
opts = opts || {};
let scheduler = opts.scheduler;
if (Array.isArray(scheduler))
{
this.scheduler = {};
for(let sch of scheduler)
{
if (typeof sch.scheduler === 'function')
{
this.scheduler[sch.id] = new sch.scheduler(app, sch.options);
} else
{
this.scheduler[sch.id] = sch.scheduler;
}
}
if(!isFunction(opts.selector))
{
throw new Error("MultiPushScheduler必须提供selector参数");
}
this.selector = opts.selector;
}
else
{
throw new Error("MultiPushScheduler必须提供scheduler参数");
}
this.app = app;
};
/**
* Component lifecycle callback
*
* @param {Function} cb
* @return {Void}
*/
async start()
{
for (let k in this.scheduler)
{
let sch = this.scheduler[k];
if (typeof sch.start === 'function')
{
await sch.start();
}
}
};
/**
* Component lifecycle callback
*
* @param {Function} cb
* @return {Void}
*/
async stop()
{
for (let k in this.scheduler)
{
let sch = this.scheduler[k];
if (typeof sch.stop === 'function')
{
await sch.stop();
}
}
};
/**
* Schedule how the message to send.
*
* @param {Number} reqId request id
* @param {String} route route string of the message
* @param {Object} msg message content after encoded
* @param {Array} recvs array of receiver's session id
* @param {Object} opts options
*/
schedule(reqId : number, route : string, msg : any, recvs : SID[], opts : ScheduleOptions, cb : (err?:Error)=>void)
{
let self = this;
let id = self.selector(reqId, route, msg, recvs, opts);
if (self.scheduler[id])
{
self.scheduler[id].schedule(reqId, route, msg, recvs, opts, cb);
} else
{
logger.error('invalid pushScheduler id, id: %j', id);
}
};
}