2021-04-08 12:19:38 +03:00
|
|
|
/**
|
|
|
|
* Declares a strategy to handle reconnection.
|
|
|
|
* When a number is returned, that number of MS will be waited before trying to reconnect.
|
|
|
|
* When `false` is returned, connection is not reconnected
|
|
|
|
*/
|
|
|
|
export type ReconnectionStrategy<T> = (
|
|
|
|
params: T,
|
|
|
|
lastError: Error | null,
|
|
|
|
consequentFails: number,
|
|
|
|
previousWait: number | null
|
|
|
|
) => number | false
|
|
|
|
|
|
|
|
/**
|
|
|
|
* default reconnection strategy: first - immediate reconnection,
|
|
|
|
* then 1s with linear increase up to 5s (with 1s step)
|
|
|
|
*/
|
2023-06-05 03:30:48 +03:00
|
|
|
export const defaultReconnectionStrategy: ReconnectionStrategy<object> = (
|
2021-04-08 12:19:38 +03:00
|
|
|
params,
|
|
|
|
lastError,
|
|
|
|
consequentFails,
|
2023-06-05 03:30:48 +03:00
|
|
|
previousWait,
|
2021-04-08 12:19:38 +03:00
|
|
|
) => {
|
|
|
|
if (previousWait === null) return 0
|
|
|
|
if (previousWait === 0) return 1000
|
|
|
|
if (previousWait >= 5000) return 5000
|
2023-06-05 03:30:48 +03:00
|
|
|
|
2021-04-08 12:19:38 +03:00
|
|
|
return Math.min(5000, previousWait + 1000)
|
|
|
|
}
|