浏览代码

feat(ts) migrate strophe.ping to TS

dev0
Yash 8 个月前
父节点
当前提交
ce7ea33418
没有帐户链接到提交者的电子邮件
共有 1 个文件被更改,包括 30 次插入8 次删除
  1. 30
    8
      modules/xmpp/strophe.ping.ts

modules/xmpp/strophe.ping.js → modules/xmpp/strophe.ping.ts 查看文件

3
 
3
 
4
 import ConnectionPlugin from './ConnectionPlugin';
4
 import ConnectionPlugin from './ConnectionPlugin';
5
 
5
 
6
-
7
 const logger = getLogger('modules/xmpp/strophe.ping');
6
 const logger = getLogger('modules/xmpp/strophe.ping');
8
 
7
 
9
 /**
8
 /**
23
  */
22
  */
24
 const PING_DEFAULT_THRESHOLD = 2;
23
 const PING_DEFAULT_THRESHOLD = 2;
25
 
24
 
25
+export interface IPingOptions {
26
+    interval?: number;
27
+    threshold?: number;
28
+    timeout?: number;
29
+}
30
+
31
+export interface IPingConnectionPluginOptions {
32
+    getTimeSinceLastServerResponse: () => number;
33
+    onPingThresholdExceeded: () => void;
34
+    pingOptions?: IPingOptions;
35
+}
36
+
26
 /**
37
 /**
27
  * XEP-0199 ping plugin.
38
  * XEP-0199 ping plugin.
28
  *
39
  *
29
  * Registers "urn:xmpp:ping" namespace under Strophe.NS.PING.
40
  * Registers "urn:xmpp:ping" namespace under Strophe.NS.PING.
30
  */
41
  */
31
 export default class PingConnectionPlugin extends ConnectionPlugin {
42
 export default class PingConnectionPlugin extends ConnectionPlugin {
43
+    failedPings: number;
44
+    _onPingThresholdExceeded: () => void;
45
+    _getTimeSinceLastServerResponse: () => number;
46
+    pingInterval: number;
47
+    pingTimeout: number;
48
+    pingThreshold: number;
49
+    pingTimestampsToKeep: number;
50
+    pingExecIntervals: number[];
51
+    intervalId: number | null;
52
+    _lastServerCheck: number;
53
+
32
     /**
54
     /**
33
      * Constructs new object
55
      * Constructs new object
34
      * @param {Object} options
56
      * @param {Object} options
39
      * @param {Object} options.pingOptions - The ping options if any.
61
      * @param {Object} options.pingOptions - The ping options if any.
40
      * @constructor
62
      * @constructor
41
      */
63
      */
42
-    constructor({ getTimeSinceLastServerResponse, onPingThresholdExceeded, pingOptions = {} }) {
64
+    constructor({ getTimeSinceLastServerResponse, onPingThresholdExceeded, pingOptions = {} }: IPingConnectionPluginOptions) {
43
         super();
65
         super();
44
         this.failedPings = 0;
66
         this.failedPings = 0;
45
         this._onPingThresholdExceeded = onPingThresholdExceeded;
67
         this._onPingThresholdExceeded = onPingThresholdExceeded;
60
      * Initializes the plugin. Method called by Strophe.
82
      * Initializes the plugin. Method called by Strophe.
61
      * @param connection Strophe connection instance.
83
      * @param connection Strophe connection instance.
62
      */
84
      */
63
-    init(connection) {
85
+    init(connection: Strophe.Connection): void {
64
         super.init(connection);
86
         super.init(connection);
65
         Strophe.addNamespace('PING', 'urn:xmpp:ping');
87
         Strophe.addNamespace('PING', 'urn:xmpp:ping');
66
     }
88
     }
75
      * @param timeout ms how long are we going to wait for the response. On
97
      * @param timeout ms how long are we going to wait for the response. On
76
      * timeout <tt>error<//t> callback is called with undefined error argument.
98
      * timeout <tt>error<//t> callback is called with undefined error argument.
77
      */
99
      */
78
-    ping(jid, success, error, timeout) {
100
+    ping(jid: string, success: (result: any) => void, error: (err: any) => void, timeout: number): void {
79
         this._addPingExecutionTimestamp();
101
         this._addPingExecutionTimestamp();
80
 
102
 
81
         const iq = $iq({
103
         const iq = $iq({
96
      * must be called before starting a new one.
118
      * must be called before starting a new one.
97
      * @param remoteJid remote JID to which ping requests will be sent to.
119
      * @param remoteJid remote JID to which ping requests will be sent to.
98
      */
120
      */
99
-    startInterval(remoteJid) {
121
+    startInterval(remoteJid: string): void {
100
         clearInterval(this.intervalId);
122
         clearInterval(this.intervalId);
101
         this.intervalId = window.setInterval(() => {
123
         this.intervalId = window.setInterval(() => {
102
 
124
 
140
     /**
162
     /**
141
      * Stops current "ping"  interval task.
163
      * Stops current "ping"  interval task.
142
      */
164
      */
143
-    stopInterval() {
165
+    stopInterval(): void {
144
         if (this.intervalId) {
166
         if (this.intervalId) {
145
             window.clearInterval(this.intervalId);
167
             window.clearInterval(this.intervalId);
146
             this.intervalId = null;
168
             this.intervalId = null;
153
      * Adds the current time to the array of send ping timestamps.
175
      * Adds the current time to the array of send ping timestamps.
154
      * @private
176
      * @private
155
      */
177
      */
156
-    _addPingExecutionTimestamp() {
178
+    _addPingExecutionTimestamp(): void {
157
         this.pingExecIntervals.push(new Date().getTime());
179
         this.pingExecIntervals.push(new Date().getTime());
158
 
180
 
159
         // keep array length to PING_TIMESTAMPS_TO_KEEP
181
         // keep array length to PING_TIMESTAMPS_TO_KEEP
170
      *
192
      *
171
      * @returns {int} the time ping was suspended, if it was not 0 is returned.
193
      * @returns {int} the time ping was suspended, if it was not 0 is returned.
172
      */
194
      */
173
-    getPingSuspendTime() {
195
+    getPingSuspendTime(): number {
174
         const pingIntervals = this.pingExecIntervals.slice();
196
         const pingIntervals = this.pingExecIntervals.slice();
175
 
197
 
176
         // we need current time, as if ping was sent now
198
         // we need current time, as if ping was sent now

正在加载...
取消
保存