Skip to content

Commit f9ef9f3

Browse files
authored
Merge pull request #344 from supabase/fix/promise-allsettled-refactor
fix: refactor removeAllSubscriptions to use Promise.all
2 parents 5a92fd8 + 7c166e8 commit f9ef9f3

File tree

1 file changed

+58
-23
lines changed

1 file changed

+58
-23
lines changed

src/SupabaseClient.ts

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -133,43 +133,79 @@ export default class SupabaseClient {
133133
/**
134134
* Remove all subscriptions.
135135
*/
136-
async removeAllSubscriptions() {
137-
const subscriptions = this.realtime.channels.slice()
138-
return await Promise.allSettled(subscriptions.map((sub) => this.removeSubscription(sub)))
136+
async removeAllSubscriptions(): Promise<
137+
(
138+
| {
139+
status: 'fulfilled'
140+
value: {
141+
error: null
142+
}
143+
}
144+
| { status: 'rejected'; reason: { error: Error } }
145+
)[]
146+
> {
147+
const subs: RealtimeSubscription[] = this.realtime.channels.slice()
148+
const removeSubPromises = subs.map((sub: RealtimeSubscription) =>
149+
this.removeSubscription(sub)
150+
.then((): { status: 'fulfilled'; value: { error: null } } => ({
151+
status: 'fulfilled',
152+
value: { error: null },
153+
}))
154+
.catch((reason: { error: Error }): { status: 'rejected'; reason: { error: Error } } => ({
155+
status: 'rejected',
156+
reason,
157+
}))
158+
)
159+
return Promise.all(removeSubPromises)
139160
}
140161

141162
/**
142163
* Removes an active subscription and returns the number of open connections.
143164
*
144165
* @param subscription The subscription you want to remove.
145166
*/
146-
removeSubscription(subscription: RealtimeSubscription) {
147-
return new Promise(async (resolve) => {
148-
try {
149-
await this._closeSubscription(subscription)
167+
removeSubscription(subscription: RealtimeSubscription): Promise<
168+
| {
169+
data: { openSubscriptions: number }
170+
error: null
171+
}
172+
| { error: Error }
173+
> {
174+
return new Promise(async (resolve, reject) => {
175+
const { error } = await this._closeSubscription(subscription)
176+
177+
if (error) {
178+
return reject({ error })
179+
}
180+
181+
const allSubscriptions = this.getSubscriptions()
182+
const openSubscriptionsCount = allSubscriptions.filter((chan) => chan.isJoined()).length
150183

151-
const allSubscriptions = this.getSubscriptions()
152-
const openSubscriptionsCount = allSubscriptions.filter((chan) => chan.isJoined()).length
184+
if (allSubscriptions.length === 0) {
185+
const { error } = await this.realtime.disconnect()
153186

154-
if (!allSubscriptions.length) {
155-
const { error } = await this.realtime.disconnect()
156-
if (error) return resolve({ error })
187+
if (error) {
188+
return reject({ error })
157189
}
158-
return resolve({ error: null, data: { openSubscriptions: openSubscriptionsCount } })
159-
} catch (error) {
160-
return resolve({ error })
161190
}
191+
192+
return resolve({
193+
data: { openSubscriptions: openSubscriptionsCount },
194+
error: null,
195+
})
162196
})
163197
}
164198

165-
private async _closeSubscription(subscription: RealtimeSubscription) {
199+
private async _closeSubscription(
200+
subscription: RealtimeSubscription
201+
): Promise<{ error: null | Error }> {
166202
if (!subscription.isClosed()) {
167-
await this._closeChannel(subscription)
203+
return await this._closeChannel(subscription)
168204
}
169205

170206
return new Promise((resolve) => {
171207
this.realtime.remove(subscription)
172-
return resolve(true)
208+
return resolve({ error: null })
173209
})
174210
}
175211

@@ -226,14 +262,13 @@ export default class SupabaseClient {
226262
return headers
227263
}
228264

229-
private _closeChannel(subscription: RealtimeSubscription) {
265+
private _closeChannel(subscription: RealtimeSubscription): Promise<{ error: null | Error }> {
230266
return new Promise((resolve, reject) => {
231267
subscription
232268
.unsubscribe()
233-
.receive('ok', () => {
234-
return resolve(true)
235-
})
236-
.receive('error', (e: Error) => reject(e))
269+
.receive('ok', () => resolve({ error: null }))
270+
.receive('error', (error: Error) => reject({ error }))
271+
.receive('timeout', () => reject({ error: 'timed out' }))
237272
})
238273
}
239274

0 commit comments

Comments
 (0)