Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

added getSubscription method and put in socket check to unsubscribe

awaliasab834899301f
diff --git a/libraries/supabase-js/package.json b/libraries/supabase-js/package.json
index 76d9fbab221..beeca0918b4 100644
--- a/libraries/supabase-js/package.json
+++ b/libraries/supabase-js/package.json
@@ -6,7 +6,7 @@
"scripts": {
"clean": "rimraf lib",
"test": "npm run cover",
- "test:integration": "docker-compose -f ./test/docker-compose.yml up -d && sleep 2 && mocha --require babel-core/register --require babel-polyfill test/integration/**/*.js ; docker-compose -f ./test/docker-compose.yml down",
+ "test:integration": "docker-compose -f ./test/docker-compose.yml up -d && sleep 5 && mocha --require babel-core/register --require babel-polyfill test/integration/**/*.js ; docker-compose -f ./test/docker-compose.yml down",
"test:prod": "cross-env BABEL_ENV=production npm run test",
"test:only": "mocha --require babel-core/register --require babel-polyfill --recursive",
"test:watch": "npm test -- --watch",
diff --git a/libraries/supabase-js/src/Supabase.js b/libraries/supabase-js/src/Supabase.js
index 857b7d493d7..c9d6c0ceb07 100644
--- a/libraries/supabase-js/src/Supabase.js
+++ b/libraries/supabase-js/src/Supabase.js
@@ -31,7 +31,7 @@ class Supabase {
case 'filter':
// temporary solution
// this is the only thing we are supporting at the moment
- if(queryFilter.operator === 'eq'){
+ if (queryFilter.operator === 'eq') {
// right now, the string is replaced instead of being stacked
// the server does not support multiple eq. statements
// as such, we will only process the very last eq. statement provided
@@ -43,7 +43,10 @@ class Supabase {
}
})
- let channel = this.tableName == "*" ? 'realtime:*' : `realtime:${this.schema}:${this.tableName}${filterString}`
+ let channel =
+ this.tableName == '*'
+ ? 'realtime:*'
+ : `realtime:${this.schema}:${this.tableName}${filterString}`
this.socket = new Socket(socketUrl, { params: { apikey: this.apikey } })
this.channel = this.socket.channel(channel)
@@ -121,16 +124,20 @@ class Supabase {
if (this.channel.state !== 'joined') {
this.channel
.join()
- .receive('ok', resp => console.debug(`${this.realtimeUrl}: Joined Realtime successfully `, resp))
+ .receive('ok', resp =>
+ console.debug(`${this.realtimeUrl}: Joined Realtime successfully `, resp)
+ )
.receive('error', resp => console.debug(`${this.realtimeUrl}: Unable to join `, resp))
- .receive('timeout', () => console.debug(`${this.realtimeUrl}: Network timeout. Still waiting...`))
+ .receive('timeout', () =>
+ console.debug(`${this.realtimeUrl}: Network timeout. Still waiting...`)
+ )
}
return this
}
unsubscribe() {
- this.socket.disconnect()
+ if (this.socket) this.socket.disconnect()
return this
}
@@ -140,7 +147,7 @@ class Supabase {
*/
initClient() {
- let rest = new PostgrestClient(this.restUrl, {queryParams: { apikey: this.apikey }})
+ let rest = new PostgrestClient(this.restUrl, { queryParams: { apikey: this.apikey } })
let api = rest.from(this.tableName)
// go through queryFilters
@@ -199,7 +206,7 @@ class Supabase {
filter: 'filter',
columnName,
operator,
- criteria
+ criteria,
})
return this
diff --git a/libraries/supabase-js/src/index.js b/libraries/supabase-js/src/index.js
index 4a8ad0db459..2af76b6f012 100644
--- a/libraries/supabase-js/src/index.js
+++ b/libraries/supabase-js/src/index.js
@@ -40,6 +40,10 @@ class SupabaseClient {
return rest.rpc(functionName, functionParameters)
}
+ getSubscriptions() {
+ return Object.values(this.subscriptions)
+ }
+
removeSubscription(mySubscription) {
mySubscription.unsubscribe()
delete this.subscriptions[mySubscription.uuid]
diff --git a/libraries/supabase-js/test/integration/testRealtime.js b/libraries/supabase-js/test/integration/testRealtime.js
index 91ce4208f14..11a7fca8086 100644
--- a/libraries/supabase-js/test/integration/testRealtime.js
+++ b/libraries/supabase-js/test/integration/testRealtime.js
@@ -9,24 +9,27 @@ import { createClient } from '../../src'
describe('test subscribing to an insert', () => {
const supabase = createClient('http://localhost:8000', 'examplekey')
- afterEach(function() {
- for (const key in supabase.subscriptions) {
- console.log(`removing: ${key}`)
- console.log(supabase.subscriptions[key])
- supabase.removeSubscription(supabase.subscriptions[key])
+ afterEach(async() => {
+ console.log(supabase.getSubscriptions())
+ for (let subscription of supabase.getSubscriptions()) {
+ supabase.removeSubscription(subscription)
}
})
- it('on() and subscribe()', async () => {
+ it('on() and subscribe()', done => {
function callbackAction(record) {
- assert(record.new.message === 'hello', 'insert made')
+ assert(record.new.message === 'hello', 'inserted message is incorrect')
+ done()
}
- const mySubscription = await supabase
+ supabase
.from('messages')
.on('*', callbackAction)
.subscribe()
- const response = await supabase
- .from('messages')
- .insert([{ message: 'hello', user_id: 1, channel_id: 1 }])
+
+ supabase.from('messages').insert([{ message: 'hello', user_id: 1, channel_id: 1 }])
}).timeout(10000)
})
+
+after(async () => {
+ setTimeout(() => process.exit(0), 5000)
+})