Skip to content

Commit

Permalink
feat(useWebSocket): allow to pass predicate function to autoReconnect… (
Browse files Browse the repository at this point in the history
  • Loading branch information
iendeavor committed Mar 19, 2022
1 parent 2f8ca30 commit e3de556
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/core/useWebSocket/index.ts
Expand Up @@ -42,9 +42,11 @@ export interface WebSocketOptions {
/**
* Maximum retry times.
*
* Or you can pass a predicate function (which returns true if you want to retry).
*
* @default -1
*/
retries?: number
retries?: number | (() => boolean)

/**
* Delay for reconnect, in milliseconds
Expand Down Expand Up @@ -211,7 +213,9 @@ export function useWebSocket<Data = any>(
} = resolveNestedOptions(options.autoReconnect)
retried += 1

if (retries < 0 || retried < retries)
if (typeof retries === 'number' && (retries < 0 || retried < retries))
setTimeout(_init, delay)
else if (typeof retries === 'function' && retries())
setTimeout(_init, delay)
else
onFailed?.()
Expand Down

0 comments on commit e3de556

Please sign in to comment.