@@ -133,43 +133,79 @@ export default class SupabaseClient {
133
133
/**
134
134
* Remove all subscriptions.
135
135
*/
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 )
139
160
}
140
161
141
162
/**
142
163
* Removes an active subscription and returns the number of open connections.
143
164
*
144
165
* @param subscription The subscription you want to remove.
145
166
*/
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
150
183
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 ( )
153
186
154
- if ( ! allSubscriptions . length ) {
155
- const { error } = await this . realtime . disconnect ( )
156
- if ( error ) return resolve ( { error } )
187
+ if ( error ) {
188
+ return reject ( { error } )
157
189
}
158
- return resolve ( { error : null , data : { openSubscriptions : openSubscriptionsCount } } )
159
- } catch ( error ) {
160
- return resolve ( { error } )
161
190
}
191
+
192
+ return resolve ( {
193
+ data : { openSubscriptions : openSubscriptionsCount } ,
194
+ error : null ,
195
+ } )
162
196
} )
163
197
}
164
198
165
- private async _closeSubscription ( subscription : RealtimeSubscription ) {
199
+ private async _closeSubscription (
200
+ subscription : RealtimeSubscription
201
+ ) : Promise < { error : null | Error } > {
166
202
if ( ! subscription . isClosed ( ) ) {
167
- await this . _closeChannel ( subscription )
203
+ return await this . _closeChannel ( subscription )
168
204
}
169
205
170
206
return new Promise ( ( resolve ) => {
171
207
this . realtime . remove ( subscription )
172
- return resolve ( true )
208
+ return resolve ( { error : null } )
173
209
} )
174
210
}
175
211
@@ -226,14 +262,13 @@ export default class SupabaseClient {
226
262
return headers
227
263
}
228
264
229
- private _closeChannel ( subscription : RealtimeSubscription ) {
265
+ private _closeChannel ( subscription : RealtimeSubscription ) : Promise < { error : null | Error } > {
230
266
return new Promise ( ( resolve , reject ) => {
231
267
subscription
232
268
. 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' } ) )
237
272
} )
238
273
}
239
274
0 commit comments