Skip to content

Commit

Permalink
fix: wait until the connection is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
jpvargasdev committed Sep 25, 2024
1 parent 514f41a commit 85f095f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-peaches-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sebspark/promise-cache": patch
---

Wait until the connection is ready
63 changes: 32 additions & 31 deletions packages/promise-cache/src/persistor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,42 +49,43 @@ export class Persistor {
}

if (!this.client || !this.client.isReady) {
this.connect()
this.startConnection()
}
}

public async connect() {
await this.startConnection()
}

public startConnection(): Promise<unknown> {
return new Promise((resolve, reject) => {
this.client = CACHE_CLIENT({
...this.redis,
socket: {
reconnectStrategy: (retries, cause) => {
console.error(cause)
return 1000 * 2 ** retries
public async startConnection() {
try {
await new Promise((resolve, reject) => {
this.client = CACHE_CLIENT({
...this.redis,
socket: {
reconnectStrategy: (retries, cause) => {
console.error(cause)
return 1000 * 2 ** retries
},
},
},
})
.on('error', (err) => {
this.onError(err)
reject()
})
.on('ready', () => {
this.onSuccess()
resolve(true)
})
.on('reconnecting', () => {
console.log('reconnecting...', this.clientId)
})
.on('end', () => {
console.log('end...', this.clientId)
})

return this.client.connect()
})
.on('error', (err) => {
this.onError(err)
reject(err)
})
.on('ready', () => {
this.onSuccess()
resolve(true)
})
.on('reconnecting', () => {
console.log('reconnecting...', this.clientId)
})
.on('end', () => {
console.log('end...', this.clientId)
})

this.client.connect()
})
} catch (ex) {
this.onError(`${ex}`)
console.error(ex)
}
}

public async size(): Promise<number> {
Expand Down

0 comments on commit 85f095f

Please sign in to comment.