From be64fcab47f3048c6941ec237db88ad5ef83ae72 Mon Sep 17 00:00:00 2001 From: Angelico Date: Wed, 15 Jan 2020 16:29:52 +0800 Subject: [PATCH] Clean up libraries The following were done - Remove unused imports and console.logs. - Fix parameters after testing. --- libraries/supabase-js/src/Base.js | 25 +++++++++++++++++-------- libraries/supabase-js/src/index.js | 15 ++++++++------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/libraries/supabase-js/src/Base.js b/libraries/supabase-js/src/Base.js index 296705ad3d..1e39de5c79 100644 --- a/libraries/supabase-js/src/Base.js +++ b/libraries/supabase-js/src/Base.js @@ -2,10 +2,11 @@ import { Socket } from 'phoenix-channels' import BaseRequest from './BaseRequest' class Base { - constructor(tableName, restUrl, apiSocket, uuid) { + constructor(tableName, restUrl, apiSocket, schema, uuid) { this.tableName = tableName this.restUrl = restUrl this.apiSocket = apiSocket + this.schema = schema this.uuid = uuid this.socket = null @@ -16,8 +17,15 @@ class Base { } createListener() { - this.socket = new Socket(apiSocket) - this.channel = this.socket.channel(this.tableName) + this.socket = new Socket(this.apiSocket) + this.channel = this.socket.channel(`realtime:${this.schema}:${this.tableName}`) + + this.socket.onOpen(() => { + console.log('Socket connected') + }) + this.socket.onClose(() => { + console.log('Socket disconnected') + }) } on(eventType, callbackFunction) { @@ -25,6 +33,8 @@ class Base { var ref = this.channel.on(eventType, callbackFunction) this.listeners[eventType] = ref + + return this } subscribe() { @@ -39,15 +49,14 @@ class Base { .receive('error', resp => console.log('Unable to join ', resp)) .receive('timeout', () => console.log('Networking issue. Still waiting...')) } + + return this } unsubscribe() { - for (var ref in this.listeners) { - let eventName = this.listeners[ref] - this.off(eventName, ref) - } - this.socket.disconnect() + + return this } request(method) { diff --git a/libraries/supabase-js/src/index.js b/libraries/supabase-js/src/index.js index c23e6251ed..c9d8e1696d 100644 --- a/libraries/supabase-js/src/index.js +++ b/libraries/supabase-js/src/index.js @@ -1,5 +1,3 @@ -import BaseRequest from './BaseRequest' -import Subscription from './Subscription' import { uuid } from './utils/Helpers' import Base from './Base' @@ -10,6 +8,7 @@ class SupabaseClient { this.restUrl = null this.apiSocket = null + this.schema = null this.subscriptions = {} this.authenticate(supabaseUrl, supabaseKey) @@ -17,19 +16,21 @@ class SupabaseClient { authenticate(supabaseUrl, supabaseKey){ // do something to utilise parameters and receive - // the restUrl & apiSocket + // the restUrl, apiSocket and schema - var restUrl = supabaseUrl + var restUrl = '' var apiSocket = '' + var schema = '' - this.restUrl = restUrl - this.apiSocket = apiSocket + this.restUrl = 'http://localhost:3000' + this.apiSocket = 'ws://localhost:4000/socket' + this.schema = 'public' } from(tableName){ let identifier = uuid() - this.subscriptions[identifier] = new Base(tableName, this.restUrl, this.apiSocket, identifier) + this.subscriptions[identifier] = new Base(tableName, this.restUrl, this.apiSocket, this.schema, identifier) return this.subscriptions[identifier] }