teckos-client 0.2.0 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,8 +2,8 @@ import debug from 'debug'
2
2
  import WebSocket from 'isomorphic-ws'
3
3
  import { decodePacket, encodePacket } from './util/Converter'
4
4
  import { ConnectionState, OptionalOptions, Options, Packet, PacketType, SocketEvent } from './types'
5
- import ITeckosClient from './ITeckosClient'
6
- import SocketEventEmitter from './util/SocketEventEmitter'
5
+ import { ITeckosClient } from './ITeckosClient'
6
+ import { SocketEventEmitter } from './util/SocketEventEmitter'
7
7
 
8
8
  const d = debug('teckos:client')
9
9
 
@@ -26,11 +26,11 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
26
26
 
27
27
  protected currentReconnectDelay: number
28
28
 
29
- protected currentReconnectionAttempts: number = 0
29
+ protected currentReconnectionAttempts = 0
30
30
 
31
31
  protected acks: Map<number, (...args: any[]) => void> = new Map()
32
32
 
33
- protected fnId: number = 0
33
+ protected fnId = 0
34
34
 
35
35
  protected connectionTimeout: any | undefined
36
36
 
@@ -46,7 +46,7 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
46
46
  this.url = url
47
47
  }
48
48
 
49
- protected attachHandler = () => {
49
+ protected attachHandler = (): void => {
50
50
  if (this.ws) {
51
51
  this.ws.onopen = this.handleOpen
52
52
  this.ws.onerror = this.handleError
@@ -55,11 +55,11 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
55
55
  }
56
56
  }
57
57
 
58
- public get webSocket() {
58
+ public get webSocket(): WebSocket | undefined {
59
59
  return this.ws
60
60
  }
61
61
 
62
- public connect = () => {
62
+ public connect = (): void => {
63
63
  if (this.options.debug) d(`Connecting to ${this.url}...`)
64
64
 
65
65
  // This will try to connect immediately
@@ -74,7 +74,7 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
74
74
  }, this.options.timeout)
75
75
  }
76
76
 
77
- protected reconnect = () => {
77
+ protected reconnect = (): void => {
78
78
  this.listeners('reconnect_attempt').forEach((listener) => listener())
79
79
  this.connect()
80
80
  }
@@ -116,6 +116,7 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
116
116
  }
117
117
 
118
118
  if (typeof args[args.length - 1] === 'function') {
119
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
119
120
  this.acks.set(this.fnId, args.pop())
120
121
  packet.id = this.fnId
121
122
  this.fnId += 1
@@ -142,20 +143,23 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
142
143
  return false
143
144
  }
144
145
 
145
- protected handleMessage = (msg: WebSocket.MessageEvent) => {
146
+ protected handleMessage = (msg: WebSocket.MessageEvent): void => {
146
147
  const packet =
147
148
  typeof msg.data === 'string'
148
- ? JSON.parse(msg.data)
149
+ ? (JSON.parse(msg.data) as Packet)
149
150
  : decodePacket(msg.data as ArrayBuffer)
150
151
  if (this.options.debug) d(`[${this.url}] Got packet: ${JSON.stringify(packet)}`)
151
152
  if (packet.type === PacketType.EVENT) {
152
- const event = packet.data[0]
153
+ const event = packet.data[0] as SocketEvent
153
154
  const args = packet.data.slice(1)
154
155
  if (event) {
156
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
155
157
  this.listeners(event).forEach((listener) => listener(...args))
156
158
  } else {
157
159
  throw new Error(
158
- `[teckos-client@${this.url}] Got invalid event message: ${msg.data}`
160
+ `[teckos-client@${this.url}] Got invalid event message: ${JSON.stringify(
161
+ msg.data
162
+ )}`
159
163
  )
160
164
  }
161
165
  } else if (packet.type === PacketType.ACK && packet.id !== undefined) {
@@ -170,7 +174,7 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
170
174
  }
171
175
  }
172
176
 
173
- protected handleOpen = () => {
177
+ protected handleOpen = (): void => {
174
178
  if (this.currentReconnectionAttempts > 0) {
175
179
  // Reset reconnection settings to default
176
180
  this.currentReconnectDelay = this.options.reconnectionDelay
@@ -178,6 +182,7 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
178
182
 
179
183
  // Inform listeners
180
184
  if (this.options.debug) d(`[${this.url}] Reconnected!`)
185
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
181
186
  this.listeners('reconnect').forEach((listener) => listener())
182
187
  }
183
188
  // Inform listeners
@@ -185,20 +190,23 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
185
190
  this.listeners('connect').forEach((listener) => listener())
186
191
  }
187
192
 
188
- protected handleError = (error: WebSocket.ErrorEvent) => {
193
+ protected handleError = (error: WebSocket.ErrorEvent): void => {
189
194
  if (this.handlers && this.handlers.error) {
190
- if (this.options.debug) d(`[${this.url}] Got error from server: ${error}`)
195
+ if (this.options.debug)
196
+ d(`[${this.url}] Got error from server: ${JSON.stringify(error)}`)
191
197
  this.handlers.error.forEach((listener) => listener(error))
192
198
  }
193
199
  }
194
200
 
195
- protected handleClose = () => {
201
+ protected handleClose = (): void => {
196
202
  // Stop connection timeout
197
203
  if (this.connectionTimeout) {
204
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
198
205
  clearTimeout(this.connectionTimeout)
199
206
  }
200
207
  // Stop reconnection timeout
201
208
  if (this.reconnectionTimeout) {
209
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
202
210
  clearTimeout(this.reconnectionTimeout)
203
211
  }
204
212
 
@@ -251,17 +259,18 @@ class TeckosClient extends SocketEventEmitter<SocketEvent> implements ITeckosCli
251
259
  }
252
260
  }
253
261
 
254
- public close = () => {
262
+ public close = (): void => {
255
263
  if (this.options.debug) d(`[${this.url}] Closing connection (client-side)`)
256
264
  if (this.ws !== undefined) {
257
265
  this.ws.onclose = () => {}
258
266
  this.ws.close()
267
+ this.listeners('disconnect').forEach((listener) => listener())
259
268
  }
260
269
  }
261
270
 
262
- public disconnect = () => {
271
+ public disconnect = (): void => {
263
272
  this.close()
264
273
  }
265
274
  }
266
275
 
267
- export default TeckosClient
276
+ export { TeckosClient }
@@ -1,6 +1,6 @@
1
1
  import debug from 'debug'
2
2
  import WebSocket from 'isomorphic-ws'
3
- import TeckosClient from './TeckosClient'
3
+ import { TeckosClient } from './TeckosClient'
4
4
  import { OptionalOptions, ConnectionState } from './types'
5
5
 
6
6
  const d = debug('teckos:client')
@@ -10,12 +10,17 @@ class TeckosClientWithJWT extends TeckosClient {
10
10
 
11
11
  protected readonly initialData: any
12
12
 
13
- protected receivedReady: boolean = false
13
+ protected receivedReady = false
14
14
 
15
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
15
16
  constructor(url: string, options: OptionalOptions, token: string, initialData?: any) {
16
17
  super(url, options)
17
18
  this.token = token
19
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
18
20
  this.initialData = initialData
21
+ this.on('disconnect', () => {
22
+ this.receivedReady = false
23
+ })
19
24
  }
20
25
 
21
26
  protected getConnectionState(): ConnectionState {
@@ -37,9 +42,9 @@ class TeckosClientWithJWT extends TeckosClient {
37
42
  return ConnectionState.DISCONNECTED
38
43
  }
39
44
 
40
- protected handleReadyEvent = () => {
45
+ protected handleReadyEvent = (): void => {
41
46
  if (this.options.debug) d(`[${this.url}] Connected!`)
42
- this.receivedReady = false
47
+ this.receivedReady = true
43
48
  if (this.currentReconnectionAttempts > 0) {
44
49
  if (this.options.debug) d(`[${this.url}] Reconnected!`)
45
50
  this.listeners('reconnect').forEach((listener) => listener())
@@ -50,7 +55,7 @@ class TeckosClientWithJWT extends TeckosClient {
50
55
  this.listeners('connect').forEach((listener) => listener())
51
56
  }
52
57
 
53
- protected handleOpen = () => {
58
+ protected handleOpen = (): void => {
54
59
  this.receivedReady = false
55
60
  this.once('ready', this.handleReadyEvent)
56
61
  if (this.options.debug) d('Connection opened, sending token now')
@@ -61,4 +66,4 @@ class TeckosClientWithJWT extends TeckosClient {
61
66
  }
62
67
  }
63
68
 
64
- export default TeckosClientWithJWT
69
+ export { TeckosClientWithJWT }
package/src/index.ts CHANGED
@@ -1,11 +1,11 @@
1
- import TeckosClientWithJWT from './TeckosClientWithJWT'
2
- import TeckosClient from './TeckosClient'
3
- import ITeckosClient from './ITeckosClient'
4
- import * as types from './types'
1
+ import { TeckosClientWithJWT } from './TeckosClientWithJWT'
2
+ import { TeckosClient } from './TeckosClient'
3
+ import { ITeckosClient } from './ITeckosClient'
4
+ import { ConnectionState, OptionalOptions, Options, Packet, PacketType, SocketEvent } from './types'
5
5
 
6
6
  /**
7
7
  * Expose all types
8
8
  */
9
- export type { ITeckosClient }
9
+ export type { Options, OptionalOptions, Packet, SocketEvent, ITeckosClient }
10
10
 
11
- export { types, TeckosClient, TeckosClientWithJWT }
11
+ export { ConnectionState, PacketType, TeckosClient, TeckosClientWithJWT }
@@ -4,4 +4,4 @@ enum ConnectionState {
4
4
  CONNECTED = 'connected',
5
5
  DISCONNECTING = 'disconnecting',
6
6
  }
7
- export default ConnectionState
7
+ export { ConnectionState }
@@ -1,4 +1,4 @@
1
- import PacketType from './PacketType'
1
+ import { PacketType } from './PacketType'
2
2
 
3
3
  export interface Packet {
4
4
  type: PacketType
@@ -2,4 +2,4 @@ enum PacketType {
2
2
  EVENT,
3
3
  ACK,
4
4
  }
5
- export default PacketType
5
+ export { PacketType }
@@ -1,8 +1,8 @@
1
1
  import { OptionalOptions, Options } from './Options'
2
- import PacketType from './PacketType'
2
+ import { PacketType } from './PacketType'
3
3
  import { Packet } from './Packet'
4
4
  import { SocketEvent } from './SocketEvent'
5
- import ConnectionState from './ConnectionState'
5
+ import { ConnectionState } from './ConnectionState'
6
6
 
7
7
  export { ConnectionState, PacketType }
8
8
 
@@ -4,5 +4,7 @@ const enc = new TextEncoder()
4
4
  const dec = new TextDecoder()
5
5
 
6
6
  const encodePacket = (packet: Packet): ArrayBufferLike => enc.encode(JSON.stringify(packet))
7
- const decodePacket = (buffer: ArrayBuffer): Packet => JSON.parse(dec.decode(buffer).toString())
7
+ const decodePacket = (buffer: ArrayBuffer): Packet =>
8
+ JSON.parse(dec.decode(buffer).toString()) as Packet
9
+
8
10
  export { encodePacket, decodePacket }
@@ -1,5 +1,7 @@
1
+ export type Listener = (...args: any[]) => void
2
+
1
3
  class SocketEventEmitter<T extends string> {
2
- protected maxListeners: number = 50
4
+ protected maxListeners = 50
3
5
 
4
6
  protected handlers: {
5
7
  [event: string]: ((...args: any[]) => void)[]
@@ -59,14 +61,14 @@ class SocketEventEmitter<T extends string> {
59
61
 
60
62
  public getMaxListeners = (): number => this.maxListeners
61
63
 
62
- public listeners = (event: T): Function[] => {
64
+ public listeners = (event: T): Listener[] => {
63
65
  if (this.handlers[event]) {
64
66
  return [...this.handlers[event]]
65
67
  }
66
68
  return []
67
69
  }
68
70
 
69
- public rawListeners = (event: T): Function[] => [...this.handlers[event]]
71
+ public rawListeners = (event: T): Listener[] => [...this.handlers[event]]
70
72
 
71
73
  public listenerCount = (event: T): number => {
72
74
  if (this.handlers[event]) {
@@ -114,4 +116,4 @@ class SocketEventEmitter<T extends string> {
114
116
  }
115
117
  }
116
118
 
117
- export default SocketEventEmitter
119
+ export { SocketEventEmitter }