/
pushScheduler.ts
77 lines (68 loc) · 2.26 KB
/
pushScheduler.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
/**
* Scheduler component to schedule message sending.
*/
import {DirectPushScheduler as DefaultScheduler} from '../pushSchedulers/direct';
import { getLogger } from 'pinus-logger';
import { Application } from '../application';
import { IComponent } from '../interfaces/IComponent';
import { IPushScheduler, ScheduleOptions, IPushSchedulerOrCtor, IPushSchedulerOptions, MultiPushSchedulerOptions } from '../interfaces/IPushScheduler';
import { MultiPushScheduler } from '../pushSchedulers/multi';
import { SID } from '../util/constants';
let logger = getLogger('pinus', __filename);
export class PushSchedulerComponent implements IComponent
{
scheduler : IPushScheduler;
constructor(private app : Application, opts ?: IPushSchedulerOptions)
{
opts = opts || {};
this.scheduler = getScheduler(this, app, opts);
};
name = '__pushScheduler__';
/**
* Component lifecycle callback
*
* @param {Function} cb
* @return {Void}
*/
afterStart(cb : ()=>void)
{
this.scheduler.start().then(cb);
};
/**
* Component lifecycle callback
*
* @param {Function} cb
* @return {Void}
*/
stop(force : boolean, cb : ()=>void)
{
this.scheduler.stop().then(cb);
};
/**
* 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
* @param {Function} cb
*/
schedule(reqId : number, route : string, msg : any, recvs : SID[], opts : ScheduleOptions, cb : (err?:Error)=>void)
{
this.scheduler.schedule(reqId, route, msg, recvs, opts, cb);
};
}
let getScheduler = function (pushSchedulerComp : PushSchedulerComponent, app : Application, opts : IPushSchedulerOptions) : IPushScheduler
{
let scheduler = opts.scheduler || DefaultScheduler;
if (typeof scheduler === 'function')
{
return new scheduler(app, opts);
}
if (Array.isArray(scheduler))
{
return new MultiPushScheduler(app , opts as MultiPushSchedulerOptions);
}
return scheduler as IPushScheduler;
};