Skip to content

Commit

Permalink
use Set instead of Array (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxpain authored Dec 19, 2020
1 parent 82fa512 commit 7520500
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function createPostgresSubscriber<Events extends Record<string, any> = { [channe
let closing = false
let dbClient = initialDBClient
let reinitializingRightNow = false
let subscribedChannels: string[] = []
let subscribedChannels: Set<string> = new Set()

let cancelEventForwarding: () => void = () => undefined
let cancelParanoidChecking: () => void = () => undefined
Expand Down Expand Up @@ -269,11 +269,13 @@ function createPostgresSubscriber<Events extends Record<string, any> = { [channe
dbClient = await reconnect(attempt => emitter.emit("reconnect", attempt))
initialize(dbClient)

const subscribedChannelsArray = Array.from(subscribedChannels)

if (subscriptionLogger.enabled) {
subscriptionLogger(`Re-subscribing to channels: ${subscribedChannels.join(", ")}`)
subscriptionLogger(`Re-subscribing to channels: ${subscribedChannelsArray.join(", ")}`)
}

await Promise.all(subscribedChannels.map(
await Promise.all(subscribedChannelsArray.map(
channelName => dbClient.query(`LISTEN ${format.ident(channelName)}`)
))

Expand Down Expand Up @@ -309,15 +311,15 @@ function createPostgresSubscriber<Events extends Record<string, any> = { [channe
return dbClient.end()
},
getSubscribedChannels () {
return subscribedChannels
return Array.from(subscribedChannels)
},
listenTo (channelName: string) {
if (subscribedChannels.indexOf(channelName) > -1) {
if (subscribedChannels.has(channelName)) {
return
}
subscriptionLogger(`Subscribing to PostgreSQL notification "${channelName}"`)

subscribedChannels = [ ...subscribedChannels, channelName ]
subscribedChannels.add(channelName)
return dbClient.query(`LISTEN ${format.ident(channelName)}`)
},
notify (channelName: string, payload?: any) {
Expand All @@ -331,18 +333,18 @@ function createPostgresSubscriber<Events extends Record<string, any> = { [channe
}
},
unlisten (channelName: string) {
if (subscribedChannels.indexOf(channelName) === -1) {
if (!subscribedChannels.has(channelName)) {
return
}
subscriptionLogger(`Unsubscribing from PostgreSQL notification "${channelName}"`)

subscribedChannels = subscribedChannels.filter(someChannel => someChannel !== channelName)
subscribedChannels.delete(channelName)
return dbClient.query(`UNLISTEN ${format.ident(channelName)}`)
},
unlistenAll () {
subscriptionLogger("Unsubscribing from all PostgreSQL notifications.")

subscribedChannels = []
subscribedChannels = new Set()
return dbClient.query(`UNLISTEN *`)
}
}
Expand Down

0 comments on commit 7520500

Please sign in to comment.