Skip to content

Commit 02c1ddd

Browse files
authored
Merge pull request #118 from ftonato/refactor/reduce-classes-complexity
refactor: reducing the complexity of the SupabaseRealtimeClient and SupabaseClient classes
2 parents db403ad + 134105f commit 02c1ddd

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

src/SupabaseClient.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export default class SupabaseClient {
8585
* @param params The parameters to pass to the function call.
8686
*/
8787
rpc<T = any>(fn: string, params?: object) {
88-
let rest = this._initPostgRESTClient()
88+
const rest = this._initPostgRESTClient()
8989
return rest.rpc<T>(fn, params)
9090
}
9191

@@ -97,12 +97,11 @@ export default class SupabaseClient {
9797
removeSubscription(subscription: RealtimeSubscription) {
9898
return new Promise(async (resolve) => {
9999
try {
100-
if (!subscription.isClosed()) {
101-
await this._closeChannel(subscription)
102-
}
103-
let openSubscriptions = this.realtime.channels.length
100+
await this._closeSubscription(subscription)
101+
102+
const openSubscriptions = this.getSubscriptions().length
104103
if (!openSubscriptions) {
105-
let { error } = await this.realtime.disconnect()
104+
const { error } = await this.realtime.disconnect()
106105
if (error) return resolve({ error })
107106
}
108107
return resolve({ error: null, data: { openSubscriptions } })
@@ -112,6 +111,12 @@ export default class SupabaseClient {
112111
})
113112
}
114113

114+
private async _closeSubscription(subscription: RealtimeSubscription) {
115+
if (!subscription.isClosed()) {
116+
await this._closeChannel(subscription)
117+
}
118+
}
119+
115120
/**
116121
* Returns an array of all your subscriptions.
117122
*/
@@ -152,8 +157,8 @@ export default class SupabaseClient {
152157
}
153158

154159
private _getAuthHeaders(): { [key: string]: string } {
155-
let headers: { [key: string]: string } = {}
156-
let authBearer = this.auth.session()?.access_token ?? this.supabaseKey
160+
const headers: { [key: string]: string } = {}
161+
const authBearer = this.auth.session()?.access_token ?? this.supabaseKey
157162
headers['apikey'] = this.supabaseKey
158163
headers['Authorization'] = `Bearer ${authBearer}`
159164
return headers
@@ -167,9 +172,7 @@ export default class SupabaseClient {
167172
this.realtime.remove(subscription)
168173
return resolve(true)
169174
})
170-
.receive('error', (e: Error) => {
171-
return reject(e)
172-
})
175+
.receive('error', (e: Error) => reject(e))
173176
})
174177
}
175178
}

src/lib/SupabaseRealtimeClient.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,12 @@ export class SupabaseRealtimeClient {
1515
old: {},
1616
}
1717

18-
switch (payload.type) {
19-
case 'INSERT':
20-
records.new = Transformers.convertChangeData(payload.columns, payload.record)
21-
break
22-
23-
case 'UPDATE':
24-
records.new = Transformers.convertChangeData(payload.columns, payload.record)
25-
records.old = Transformers.convertChangeData(payload.columns, payload.old_record)
26-
break
18+
if (payload.type === 'INSERT' || payload.type === 'UPDATE') {
19+
records.new = Transformers.convertChangeData(payload.columns, payload.record)
20+
}
2721

28-
case 'DELETE':
29-
records.old = Transformers.convertChangeData(payload.columns, payload.old_record)
30-
break
22+
if (payload.type === 'UPDATE' || payload.type === 'DELETE') {
23+
records.old = Transformers.convertChangeData(payload.columns, payload.old_record)
3124
}
3225

3326
return records

0 commit comments

Comments
 (0)