@supabase/realtime-js
Advanced tools
Comparing version 1.6.2 to 1.7.0
@@ -31,2 +31,8 @@ export declare const DEFAULT_HEADERS: { | ||
} | ||
export declare enum CONNECTION_STATE { | ||
Connecting = "connecting", | ||
Open = "open", | ||
Closing = "closing", | ||
Closed = "closed" | ||
} | ||
//# sourceMappingURL=constants.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TRANSPORTS = exports.CHANNEL_EVENTS = exports.CHANNEL_STATES = exports.SOCKET_STATES = exports.WS_CLOSE_NORMAL = exports.DEFAULT_TIMEOUT = exports.VSN = exports.DEFAULT_HEADERS = void 0; | ||
exports.CONNECTION_STATE = exports.TRANSPORTS = exports.CHANNEL_EVENTS = exports.CHANNEL_STATES = exports.SOCKET_STATES = exports.WS_CLOSE_NORMAL = exports.DEFAULT_TIMEOUT = exports.VSN = exports.DEFAULT_HEADERS = void 0; | ||
const version_1 = require("./version"); | ||
@@ -37,2 +37,9 @@ exports.DEFAULT_HEADERS = { 'X-Client-Info': `realtime-js/${version_1.version}` }; | ||
})(TRANSPORTS = exports.TRANSPORTS || (exports.TRANSPORTS = {})); | ||
var CONNECTION_STATE; | ||
(function (CONNECTION_STATE) { | ||
CONNECTION_STATE["Connecting"] = "connecting"; | ||
CONNECTION_STATE["Open"] = "open"; | ||
CONNECTION_STATE["Closing"] = "closing"; | ||
CONNECTION_STATE["Closed"] = "closed"; | ||
})(CONNECTION_STATE = exports.CONNECTION_STATE || (exports.CONNECTION_STATE = {})); | ||
//# sourceMappingURL=constants.js.map |
@@ -1,2 +0,2 @@ | ||
export declare const version = "1.6.2"; | ||
export declare const version = "1.7.0"; | ||
//# sourceMappingURL=version.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.version = void 0; | ||
exports.version = '1.6.2'; | ||
exports.version = '1.7.0'; | ||
//# sourceMappingURL=version.js.map |
@@ -26,10 +26,19 @@ import { CHANNEL_EVENTS, CHANNEL_STATES } from './lib/constants'; | ||
subscribe(timeout?: number): Push; | ||
/** | ||
* Registers a callback that will be executed when the channel closes. | ||
*/ | ||
onClose(callback: Function): void; | ||
/** | ||
* Registers a callback that will be executed when the channel encounteres an error. | ||
*/ | ||
onError(callback: Function): void; | ||
on(type: string, eventFilter?: { | ||
on(type: string, filter?: { | ||
[key: string]: string; | ||
}, callback?: Function): void; | ||
off(type: string, eventFilter: { | ||
off(type: string, filter: { | ||
[key: string]: any; | ||
}): void; | ||
/** | ||
* Returns `true` if the socket is connected and the channel has been joined. | ||
*/ | ||
canPush(): boolean; | ||
@@ -41,3 +50,3 @@ push(event: CHANNEL_EVENTS, payload: any, timeout?: number): Push; | ||
/** | ||
* Leaves the channel | ||
* Leaves the channel. | ||
* | ||
@@ -44,0 +53,0 @@ * Unsubscribes from server events, and instructs channel to terminate on server. |
@@ -90,21 +90,29 @@ "use strict"; | ||
} | ||
/** | ||
* Registers a callback that will be executed when the channel closes. | ||
*/ | ||
onClose(callback) { | ||
this.on(constants_1.CHANNEL_EVENTS.close, {}, callback); | ||
} | ||
/** | ||
* Registers a callback that will be executed when the channel encounteres an error. | ||
*/ | ||
onError(callback) { | ||
this.on(constants_1.CHANNEL_EVENTS.error, {}, (reason) => callback(reason)); | ||
} | ||
on(type, eventFilter, callback) { | ||
on(type, filter, callback) { | ||
this.bindings.push({ | ||
type, | ||
eventFilter: eventFilter !== null && eventFilter !== void 0 ? eventFilter : {}, | ||
filter: filter !== null && filter !== void 0 ? filter : {}, | ||
callback: callback !== null && callback !== void 0 ? callback : (() => { }), | ||
}); | ||
} | ||
off(type, eventFilter) { | ||
off(type, filter) { | ||
this.bindings = this.bindings.filter((bind) => { | ||
return !(bind.type === type && | ||
RealtimeChannel.isEqual(bind.eventFilter, eventFilter)); | ||
return !(bind.type === type && RealtimeChannel.isEqual(bind.filter, filter)); | ||
}); | ||
} | ||
/** | ||
* Returns `true` if the socket is connected and the channel has been joined. | ||
*/ | ||
canPush() { | ||
@@ -131,3 +139,3 @@ return this.socket.isConnected() && this.isJoined(); | ||
/** | ||
* Leaves the channel | ||
* Leaves the channel. | ||
* | ||
@@ -142,3 +150,3 @@ * Unsubscribes from server events, and instructs channel to terminate on server. | ||
this.state = constants_1.CHANNEL_STATES.leaving; | ||
let onClose = () => { | ||
const onClose = () => { | ||
this.socket.log('channel', `leave ${this.topic}`); | ||
@@ -149,3 +157,3 @@ this.trigger(constants_1.CHANNEL_EVENTS.close, 'leave', this.joinRef()); | ||
this.joinPush.destroy(); | ||
let leavePush = new push_1.default(this, constants_1.CHANNEL_EVENTS.leave, {}, timeout); | ||
const leavePush = new push_1.default(this, constants_1.CHANNEL_EVENTS.leave, {}, timeout); | ||
leavePush.receive('ok', () => onClose()).receive('timeout', () => onClose()); | ||
@@ -195,4 +203,4 @@ leavePush.send(); | ||
return ((bind === null || bind === void 0 ? void 0 : bind.type) === type && | ||
(((_a = bind === null || bind === void 0 ? void 0 : bind.eventFilter) === null || _a === void 0 ? void 0 : _a.event) === '*' || | ||
((_b = bind === null || bind === void 0 ? void 0 : bind.eventFilter) === null || _b === void 0 ? void 0 : _b.event) === (payload === null || payload === void 0 ? void 0 : payload.event))); | ||
(((_a = bind === null || bind === void 0 ? void 0 : bind.filter) === null || _a === void 0 ? void 0 : _a.event) === '*' || | ||
((_b = bind === null || bind === void 0 ? void 0 : bind.filter) === null || _b === void 0 ? void 0 : _b.event) === (payload === null || payload === void 0 ? void 0 : payload.event))); | ||
}) | ||
@@ -199,0 +207,0 @@ .map((bind) => bind.callback(handledPayload, ref)); |
@@ -0,1 +1,2 @@ | ||
import { CONNECTION_STATE } from './lib/constants'; | ||
import Timer from './lib/timer'; | ||
@@ -27,2 +28,6 @@ import Serializer from './lib/serializer'; | ||
}; | ||
declare type ChannelParams = { | ||
selfBroadcast?: boolean; | ||
[key: string]: any; | ||
}; | ||
export default class RealtimeClient { | ||
@@ -59,4 +64,5 @@ accessToken: string | null; | ||
}; | ||
versionDate: Date | undefined; | ||
/** | ||
* Initializes the Socket | ||
* Initializes the Socket. | ||
* | ||
@@ -77,3 +83,3 @@ * @param endPoint The string WebSocket endpoint, ie, "ws://example.com/socket", "wss://example.com", "/socket" (inherited host & protocol) | ||
/** | ||
* Connects the socket. | ||
* Connects the socket, unless already connected. | ||
*/ | ||
@@ -92,3 +98,5 @@ connect(): void; | ||
/** | ||
* Logs the message. Override `this.logger` for specialized logging. | ||
* Logs the message. | ||
* | ||
* For customized logging, `this.logger` can be overriden. | ||
*/ | ||
@@ -98,2 +106,3 @@ log(kind: string, msg: string, data?: any): void; | ||
* Registers a callback for connection state change event. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -106,3 +115,4 @@ * | ||
/** | ||
* Registers a callbacks for connection state change events. | ||
* Registers a callback for connection state change events. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -116,2 +126,3 @@ * | ||
* Registers a callback for connection state change events. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -125,2 +136,3 @@ * | ||
* Calls a function any time a message is received. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -135,3 +147,3 @@ * | ||
*/ | ||
connectionState(): "closed" | "open" | "closing" | "connecting"; | ||
connectionState(): CONNECTION_STATE; | ||
/** | ||
@@ -147,7 +159,12 @@ * Retuns `true` is the connection is open. | ||
remove(channel: RealtimeSubscription | RealtimeChannel): void; | ||
channel(topic: string, chanParams?: { | ||
[key: string]: any; | ||
}): RealtimeSubscription | RealtimeChannel; | ||
channel(topic: string, chanParams?: ChannelParams): RealtimeChannel | RealtimeSubscription; | ||
/** | ||
* Push out a message if the socket is connected. | ||
* | ||
* If the socket is not connected, the message gets enqueued within a local buffer, and sent out when a connection is next established. | ||
*/ | ||
push(data: Message): void; | ||
onConnMessage(rawMessage: any): void; | ||
onConnMessage(rawMessage: { | ||
data: any; | ||
}): void; | ||
/** | ||
@@ -167,2 +184,5 @@ * Returns the URL of the websocket. | ||
setAuth(token: string | null): void; | ||
/** | ||
* Unsubscribe from channels with the specified topic. | ||
*/ | ||
leaveOpenTopic(topic: string): void; | ||
@@ -169,0 +189,0 @@ private _onConnOpen; |
@@ -35,3 +35,3 @@ "use strict"; | ||
/** | ||
* Initializes the Socket | ||
* Initializes the Socket. | ||
* | ||
@@ -73,2 +73,3 @@ * @param endPoint The string WebSocket endpoint, ie, "ws://example.com/socket", "wss://example.com", "/socket" (inherited host & protocol) | ||
}; | ||
this.versionDate = undefined; | ||
this.endPoint = `${endPoint}/${constants_1.TRANSPORTS.websocket}`; | ||
@@ -105,5 +106,6 @@ if (options === null || options === void 0 ? void 0 : options.params) | ||
/** | ||
* Connects the socket. | ||
* Connects the socket, unless already connected. | ||
*/ | ||
connect() { | ||
var _a; | ||
if (this.conn) { | ||
@@ -120,2 +122,6 @@ return; | ||
this.conn.onclose = (event) => this._onConnClose(event); | ||
const versionDate = new Date((_a = new URL(this.conn.url).searchParams.get('vsndate')) !== null && _a !== void 0 ? _a : ''); | ||
if (versionDate instanceof Date) { | ||
this.versionDate = versionDate; | ||
} | ||
} | ||
@@ -153,3 +159,5 @@ } | ||
/** | ||
* Logs the message. Override `this.logger` for specialized logging. | ||
* Logs the message. | ||
* | ||
* For customized logging, `this.logger` can be overriden. | ||
*/ | ||
@@ -161,2 +169,3 @@ log(kind, msg, data) { | ||
* Registers a callback for connection state change event. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -171,3 +180,4 @@ * | ||
/** | ||
* Registers a callbacks for connection state change events. | ||
* Registers a callback for connection state change events. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -183,2 +193,3 @@ * | ||
* Registers a callback for connection state change events. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -194,2 +205,3 @@ * | ||
* Calls a function any time a message is received. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -209,9 +221,9 @@ * | ||
case constants_1.SOCKET_STATES.connecting: | ||
return 'connecting'; | ||
return constants_1.CONNECTION_STATE.Connecting; | ||
case constants_1.SOCKET_STATES.open: | ||
return 'open'; | ||
return constants_1.CONNECTION_STATE.Open; | ||
case constants_1.SOCKET_STATES.closing: | ||
return 'closing'; | ||
return constants_1.CONNECTION_STATE.Closing; | ||
default: | ||
return 'closed'; | ||
return constants_1.CONNECTION_STATE.Closed; | ||
} | ||
@@ -223,3 +235,3 @@ } | ||
isConnected() { | ||
return this.connectionState() === 'open'; | ||
return this.connectionState() === constants_1.CONNECTION_STATE.Open; | ||
} | ||
@@ -234,7 +246,10 @@ /** | ||
} | ||
channel(topic, chanParams = { isNewVersion: false }) { | ||
const { isNewVersion } = chanParams, params = __rest(chanParams, ["isNewVersion"]); | ||
const chan = isNewVersion | ||
? new RealtimeChannel_1.default(topic, Object.assign({}, params), this) | ||
: new RealtimeSubscription_1.default(topic, Object.assign({}, params), this); | ||
channel(topic, chanParams = {}) { | ||
const { selfBroadcast } = chanParams, params = __rest(chanParams, ["selfBroadcast"]); | ||
if (selfBroadcast) { | ||
params.self_broadcast = selfBroadcast; | ||
} | ||
const chan = this.versionDate | ||
? new RealtimeChannel_1.default(topic, params, this) | ||
: new RealtimeSubscription_1.default(topic, params, this); | ||
if (chan instanceof RealtimeChannel_1.default) { | ||
@@ -264,4 +279,9 @@ chan.presence.onJoin((key, currentPresences, newPresences) => { | ||
} | ||
/** | ||
* Push out a message if the socket is connected. | ||
* | ||
* If the socket is not connected, the message gets enqueued within a local buffer, and sent out when a connection is next established. | ||
*/ | ||
push(data) { | ||
let { topic, event, payload, ref } = data; | ||
const { topic, event, payload, ref } = data; | ||
let callback = () => { | ||
@@ -321,14 +341,12 @@ this.encode(data, (result) => { | ||
this.accessToken = token; | ||
try { | ||
this.channels.forEach((channel) => { | ||
token && channel.updateJoinPayload({ user_token: token }); | ||
if (channel.joinedOnce && channel.isJoined()) { | ||
channel.push(constants_1.CHANNEL_EVENTS.access_token, { access_token: token }); | ||
} | ||
}); | ||
} | ||
catch (error) { | ||
console.log('setAuth error', error); | ||
} | ||
this.channels.forEach((channel) => { | ||
token && channel.updateJoinPayload({ user_token: token }); | ||
if (channel.joinedOnce && channel.isJoined()) { | ||
channel.push(constants_1.CHANNEL_EVENTS.access_token, { access_token: token }); | ||
} | ||
}); | ||
} | ||
/** | ||
* Unsubscribe from channels with the specified topic. | ||
*/ | ||
leaveOpenTopic(topic) { | ||
@@ -335,0 +353,0 @@ let dupChannel = this.channels.find((c) => c.topic === topic && (c.isJoined() || c.isJoining())); |
@@ -37,3 +37,4 @@ import { PresenceOpts, PresenceOnJoinCallback, PresenceOnLeaveCallback } from 'phoenix'; | ||
/** | ||
* Initializes the Presence | ||
* Initializes the Presence. | ||
* | ||
* @param channel - The RealtimeSubscription | ||
@@ -45,5 +46,7 @@ * @param opts - The options, | ||
/** | ||
* Used to sync the list of presences on the server | ||
* with the client's state. An optional `onJoin` and `onLeave` callback can | ||
* be provided to react to changes in the client's local presences across | ||
* Used to sync the list of presences on the server with the | ||
* client's state. | ||
* | ||
* An optional `onJoin` and `onLeave` callback can be provided to | ||
* react to changes in the client's local presences across | ||
* disconnects and reconnects with the server. | ||
@@ -53,7 +56,8 @@ */ | ||
/** | ||
* Used to sync a diff of presence join and leave events from the | ||
* server, as they happen. | ||
* | ||
* Used to sync a diff of presence join and leave | ||
* events from the server, as they happen. Like `syncState`, `syncDiff` | ||
* accepts optional `onJoin` and `onLeave` callbacks to react to a user | ||
* joining or leaving from a device. | ||
* Like `syncState`, `syncDiff` accepts optional `onJoin` and | ||
* `onLeave` callbacks to react to a user joining or leaving from a | ||
* device. | ||
*/ | ||
@@ -60,0 +64,0 @@ static syncDiff(state: PresenceState, diff: RawPresenceDiff | PresenceDiff, onJoin: PresenceOnJoinCallback, onLeave: PresenceOnLeaveCallback): PresenceState; |
@@ -9,3 +9,4 @@ "use strict"; | ||
/** | ||
* Initializes the Presence | ||
* Initializes the Presence. | ||
* | ||
* @param channel - The RealtimeSubscription | ||
@@ -51,5 +52,7 @@ * @param opts - The options, | ||
/** | ||
* Used to sync the list of presences on the server | ||
* with the client's state. An optional `onJoin` and `onLeave` callback can | ||
* be provided to react to changes in the client's local presences across | ||
* Used to sync the list of presences on the server with the | ||
* client's state. | ||
* | ||
* An optional `onJoin` and `onLeave` callback can be provided to | ||
* react to changes in the client's local presences across | ||
* disconnects and reconnects with the server. | ||
@@ -88,7 +91,8 @@ */ | ||
/** | ||
* Used to sync a diff of presence join and leave events from the | ||
* server, as they happen. | ||
* | ||
* Used to sync a diff of presence join and leave | ||
* events from the server, as they happen. Like `syncState`, `syncDiff` | ||
* accepts optional `onJoin` and `onLeave` callbacks to react to a user | ||
* joining or leaving from a device. | ||
* Like `syncState`, `syncDiff` accepts optional `onJoin` and | ||
* `onLeave` callbacks to react to a user joining or leaving from a | ||
* device. | ||
*/ | ||
@@ -95,0 +99,0 @@ static syncDiff(state, diff, onJoin, onLeave) { |
@@ -31,2 +31,8 @@ export declare const DEFAULT_HEADERS: { | ||
} | ||
export declare enum CONNECTION_STATE { | ||
Connecting = "connecting", | ||
Open = "open", | ||
Closing = "closing", | ||
Closed = "closed" | ||
} | ||
//# sourceMappingURL=constants.d.ts.map |
@@ -34,2 +34,9 @@ import { version } from './version'; | ||
})(TRANSPORTS || (TRANSPORTS = {})); | ||
export var CONNECTION_STATE; | ||
(function (CONNECTION_STATE) { | ||
CONNECTION_STATE["Connecting"] = "connecting"; | ||
CONNECTION_STATE["Open"] = "open"; | ||
CONNECTION_STATE["Closing"] = "closing"; | ||
CONNECTION_STATE["Closed"] = "closed"; | ||
})(CONNECTION_STATE || (CONNECTION_STATE = {})); | ||
//# sourceMappingURL=constants.js.map |
@@ -1,2 +0,2 @@ | ||
export declare const version = "1.6.2"; | ||
export declare const version = "1.7.0"; | ||
//# sourceMappingURL=version.d.ts.map |
@@ -1,2 +0,2 @@ | ||
export const version = '1.6.2'; | ||
export const version = '1.7.0'; | ||
//# sourceMappingURL=version.js.map |
@@ -26,10 +26,19 @@ import { CHANNEL_EVENTS, CHANNEL_STATES } from './lib/constants'; | ||
subscribe(timeout?: number): Push; | ||
/** | ||
* Registers a callback that will be executed when the channel closes. | ||
*/ | ||
onClose(callback: Function): void; | ||
/** | ||
* Registers a callback that will be executed when the channel encounteres an error. | ||
*/ | ||
onError(callback: Function): void; | ||
on(type: string, eventFilter?: { | ||
on(type: string, filter?: { | ||
[key: string]: string; | ||
}, callback?: Function): void; | ||
off(type: string, eventFilter: { | ||
off(type: string, filter: { | ||
[key: string]: any; | ||
}): void; | ||
/** | ||
* Returns `true` if the socket is connected and the channel has been joined. | ||
*/ | ||
canPush(): boolean; | ||
@@ -41,3 +50,3 @@ push(event: CHANNEL_EVENTS, payload: any, timeout?: number): Push; | ||
/** | ||
* Leaves the channel | ||
* Leaves the channel. | ||
* | ||
@@ -44,0 +53,0 @@ * Unsubscribes from server events, and instructs channel to terminate on server. |
@@ -85,21 +85,29 @@ import { CHANNEL_EVENTS, CHANNEL_STATES } from './lib/constants'; | ||
} | ||
/** | ||
* Registers a callback that will be executed when the channel closes. | ||
*/ | ||
onClose(callback) { | ||
this.on(CHANNEL_EVENTS.close, {}, callback); | ||
} | ||
/** | ||
* Registers a callback that will be executed when the channel encounteres an error. | ||
*/ | ||
onError(callback) { | ||
this.on(CHANNEL_EVENTS.error, {}, (reason) => callback(reason)); | ||
} | ||
on(type, eventFilter, callback) { | ||
on(type, filter, callback) { | ||
this.bindings.push({ | ||
type, | ||
eventFilter: eventFilter !== null && eventFilter !== void 0 ? eventFilter : {}, | ||
filter: filter !== null && filter !== void 0 ? filter : {}, | ||
callback: callback !== null && callback !== void 0 ? callback : (() => { }), | ||
}); | ||
} | ||
off(type, eventFilter) { | ||
off(type, filter) { | ||
this.bindings = this.bindings.filter((bind) => { | ||
return !(bind.type === type && | ||
RealtimeChannel.isEqual(bind.eventFilter, eventFilter)); | ||
return !(bind.type === type && RealtimeChannel.isEqual(bind.filter, filter)); | ||
}); | ||
} | ||
/** | ||
* Returns `true` if the socket is connected and the channel has been joined. | ||
*/ | ||
canPush() { | ||
@@ -126,3 +134,3 @@ return this.socket.isConnected() && this.isJoined(); | ||
/** | ||
* Leaves the channel | ||
* Leaves the channel. | ||
* | ||
@@ -137,3 +145,3 @@ * Unsubscribes from server events, and instructs channel to terminate on server. | ||
this.state = CHANNEL_STATES.leaving; | ||
let onClose = () => { | ||
const onClose = () => { | ||
this.socket.log('channel', `leave ${this.topic}`); | ||
@@ -144,3 +152,3 @@ this.trigger(CHANNEL_EVENTS.close, 'leave', this.joinRef()); | ||
this.joinPush.destroy(); | ||
let leavePush = new Push(this, CHANNEL_EVENTS.leave, {}, timeout); | ||
const leavePush = new Push(this, CHANNEL_EVENTS.leave, {}, timeout); | ||
leavePush.receive('ok', () => onClose()).receive('timeout', () => onClose()); | ||
@@ -190,4 +198,4 @@ leavePush.send(); | ||
return ((bind === null || bind === void 0 ? void 0 : bind.type) === type && | ||
(((_a = bind === null || bind === void 0 ? void 0 : bind.eventFilter) === null || _a === void 0 ? void 0 : _a.event) === '*' || | ||
((_b = bind === null || bind === void 0 ? void 0 : bind.eventFilter) === null || _b === void 0 ? void 0 : _b.event) === (payload === null || payload === void 0 ? void 0 : payload.event))); | ||
(((_a = bind === null || bind === void 0 ? void 0 : bind.filter) === null || _a === void 0 ? void 0 : _a.event) === '*' || | ||
((_b = bind === null || bind === void 0 ? void 0 : bind.filter) === null || _b === void 0 ? void 0 : _b.event) === (payload === null || payload === void 0 ? void 0 : payload.event))); | ||
}) | ||
@@ -194,0 +202,0 @@ .map((bind) => bind.callback(handledPayload, ref)); |
@@ -0,1 +1,2 @@ | ||
import { CONNECTION_STATE } from './lib/constants'; | ||
import Timer from './lib/timer'; | ||
@@ -27,2 +28,6 @@ import Serializer from './lib/serializer'; | ||
}; | ||
declare type ChannelParams = { | ||
selfBroadcast?: boolean; | ||
[key: string]: any; | ||
}; | ||
export default class RealtimeClient { | ||
@@ -59,4 +64,5 @@ accessToken: string | null; | ||
}; | ||
versionDate: Date | undefined; | ||
/** | ||
* Initializes the Socket | ||
* Initializes the Socket. | ||
* | ||
@@ -77,3 +83,3 @@ * @param endPoint The string WebSocket endpoint, ie, "ws://example.com/socket", "wss://example.com", "/socket" (inherited host & protocol) | ||
/** | ||
* Connects the socket. | ||
* Connects the socket, unless already connected. | ||
*/ | ||
@@ -92,3 +98,5 @@ connect(): void; | ||
/** | ||
* Logs the message. Override `this.logger` for specialized logging. | ||
* Logs the message. | ||
* | ||
* For customized logging, `this.logger` can be overriden. | ||
*/ | ||
@@ -98,2 +106,3 @@ log(kind: string, msg: string, data?: any): void; | ||
* Registers a callback for connection state change event. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -106,3 +115,4 @@ * | ||
/** | ||
* Registers a callbacks for connection state change events. | ||
* Registers a callback for connection state change events. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -116,2 +126,3 @@ * | ||
* Registers a callback for connection state change events. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -125,2 +136,3 @@ * | ||
* Calls a function any time a message is received. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -135,3 +147,3 @@ * | ||
*/ | ||
connectionState(): "closed" | "open" | "closing" | "connecting"; | ||
connectionState(): CONNECTION_STATE; | ||
/** | ||
@@ -147,7 +159,12 @@ * Retuns `true` is the connection is open. | ||
remove(channel: RealtimeSubscription | RealtimeChannel): void; | ||
channel(topic: string, chanParams?: { | ||
[key: string]: any; | ||
}): RealtimeSubscription | RealtimeChannel; | ||
channel(topic: string, chanParams?: ChannelParams): RealtimeChannel | RealtimeSubscription; | ||
/** | ||
* Push out a message if the socket is connected. | ||
* | ||
* If the socket is not connected, the message gets enqueued within a local buffer, and sent out when a connection is next established. | ||
*/ | ||
push(data: Message): void; | ||
onConnMessage(rawMessage: any): void; | ||
onConnMessage(rawMessage: { | ||
data: any; | ||
}): void; | ||
/** | ||
@@ -167,2 +184,5 @@ * Returns the URL of the websocket. | ||
setAuth(token: string | null): void; | ||
/** | ||
* Unsubscribe from channels with the specified topic. | ||
*/ | ||
leaveOpenTopic(topic: string): void; | ||
@@ -169,0 +189,0 @@ private _onConnOpen; |
@@ -22,3 +22,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { w3cwebsocket } from 'websocket'; | ||
import { VSN, CHANNEL_EVENTS, TRANSPORTS, SOCKET_STATES, DEFAULT_TIMEOUT, WS_CLOSE_NORMAL, DEFAULT_HEADERS, } from './lib/constants'; | ||
import { VSN, CHANNEL_EVENTS, TRANSPORTS, SOCKET_STATES, DEFAULT_TIMEOUT, WS_CLOSE_NORMAL, DEFAULT_HEADERS, CONNECTION_STATE, } from './lib/constants'; | ||
import Timer from './lib/timer'; | ||
@@ -31,3 +31,3 @@ import Serializer from './lib/serializer'; | ||
/** | ||
* Initializes the Socket | ||
* Initializes the Socket. | ||
* | ||
@@ -69,2 +69,3 @@ * @param endPoint The string WebSocket endpoint, ie, "ws://example.com/socket", "wss://example.com", "/socket" (inherited host & protocol) | ||
}; | ||
this.versionDate = undefined; | ||
this.endPoint = `${endPoint}/${TRANSPORTS.websocket}`; | ||
@@ -101,5 +102,6 @@ if (options === null || options === void 0 ? void 0 : options.params) | ||
/** | ||
* Connects the socket. | ||
* Connects the socket, unless already connected. | ||
*/ | ||
connect() { | ||
var _a; | ||
if (this.conn) { | ||
@@ -116,2 +118,6 @@ return; | ||
this.conn.onclose = (event) => this._onConnClose(event); | ||
const versionDate = new Date((_a = new URL(this.conn.url).searchParams.get('vsndate')) !== null && _a !== void 0 ? _a : ''); | ||
if (versionDate instanceof Date) { | ||
this.versionDate = versionDate; | ||
} | ||
} | ||
@@ -149,3 +155,5 @@ } | ||
/** | ||
* Logs the message. Override `this.logger` for specialized logging. | ||
* Logs the message. | ||
* | ||
* For customized logging, `this.logger` can be overriden. | ||
*/ | ||
@@ -157,2 +165,3 @@ log(kind, msg, data) { | ||
* Registers a callback for connection state change event. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -167,3 +176,4 @@ * | ||
/** | ||
* Registers a callbacks for connection state change events. | ||
* Registers a callback for connection state change events. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -179,2 +189,3 @@ * | ||
* Registers a callback for connection state change events. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -190,2 +201,3 @@ * | ||
* Calls a function any time a message is received. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -205,9 +217,9 @@ * | ||
case SOCKET_STATES.connecting: | ||
return 'connecting'; | ||
return CONNECTION_STATE.Connecting; | ||
case SOCKET_STATES.open: | ||
return 'open'; | ||
return CONNECTION_STATE.Open; | ||
case SOCKET_STATES.closing: | ||
return 'closing'; | ||
return CONNECTION_STATE.Closing; | ||
default: | ||
return 'closed'; | ||
return CONNECTION_STATE.Closed; | ||
} | ||
@@ -219,3 +231,3 @@ } | ||
isConnected() { | ||
return this.connectionState() === 'open'; | ||
return this.connectionState() === CONNECTION_STATE.Open; | ||
} | ||
@@ -230,7 +242,10 @@ /** | ||
} | ||
channel(topic, chanParams = { isNewVersion: false }) { | ||
const { isNewVersion } = chanParams, params = __rest(chanParams, ["isNewVersion"]); | ||
const chan = isNewVersion | ||
? new RealtimeChannel(topic, Object.assign({}, params), this) | ||
: new RealtimeSubscription(topic, Object.assign({}, params), this); | ||
channel(topic, chanParams = {}) { | ||
const { selfBroadcast } = chanParams, params = __rest(chanParams, ["selfBroadcast"]); | ||
if (selfBroadcast) { | ||
params.self_broadcast = selfBroadcast; | ||
} | ||
const chan = this.versionDate | ||
? new RealtimeChannel(topic, params, this) | ||
: new RealtimeSubscription(topic, params, this); | ||
if (chan instanceof RealtimeChannel) { | ||
@@ -260,4 +275,9 @@ chan.presence.onJoin((key, currentPresences, newPresences) => { | ||
} | ||
/** | ||
* Push out a message if the socket is connected. | ||
* | ||
* If the socket is not connected, the message gets enqueued within a local buffer, and sent out when a connection is next established. | ||
*/ | ||
push(data) { | ||
let { topic, event, payload, ref } = data; | ||
const { topic, event, payload, ref } = data; | ||
let callback = () => { | ||
@@ -317,14 +337,12 @@ this.encode(data, (result) => { | ||
this.accessToken = token; | ||
try { | ||
this.channels.forEach((channel) => { | ||
token && channel.updateJoinPayload({ user_token: token }); | ||
if (channel.joinedOnce && channel.isJoined()) { | ||
channel.push(CHANNEL_EVENTS.access_token, { access_token: token }); | ||
} | ||
}); | ||
} | ||
catch (error) { | ||
console.log('setAuth error', error); | ||
} | ||
this.channels.forEach((channel) => { | ||
token && channel.updateJoinPayload({ user_token: token }); | ||
if (channel.joinedOnce && channel.isJoined()) { | ||
channel.push(CHANNEL_EVENTS.access_token, { access_token: token }); | ||
} | ||
}); | ||
} | ||
/** | ||
* Unsubscribe from channels with the specified topic. | ||
*/ | ||
leaveOpenTopic(topic) { | ||
@@ -331,0 +349,0 @@ let dupChannel = this.channels.find((c) => c.topic === topic && (c.isJoined() || c.isJoining())); |
@@ -37,3 +37,4 @@ import { PresenceOpts, PresenceOnJoinCallback, PresenceOnLeaveCallback } from 'phoenix'; | ||
/** | ||
* Initializes the Presence | ||
* Initializes the Presence. | ||
* | ||
* @param channel - The RealtimeSubscription | ||
@@ -45,5 +46,7 @@ * @param opts - The options, | ||
/** | ||
* Used to sync the list of presences on the server | ||
* with the client's state. An optional `onJoin` and `onLeave` callback can | ||
* be provided to react to changes in the client's local presences across | ||
* Used to sync the list of presences on the server with the | ||
* client's state. | ||
* | ||
* An optional `onJoin` and `onLeave` callback can be provided to | ||
* react to changes in the client's local presences across | ||
* disconnects and reconnects with the server. | ||
@@ -53,7 +56,8 @@ */ | ||
/** | ||
* Used to sync a diff of presence join and leave events from the | ||
* server, as they happen. | ||
* | ||
* Used to sync a diff of presence join and leave | ||
* events from the server, as they happen. Like `syncState`, `syncDiff` | ||
* accepts optional `onJoin` and `onLeave` callbacks to react to a user | ||
* joining or leaving from a device. | ||
* Like `syncState`, `syncDiff` accepts optional `onJoin` and | ||
* `onLeave` callbacks to react to a user joining or leaving from a | ||
* device. | ||
*/ | ||
@@ -60,0 +64,0 @@ static syncDiff(state: PresenceState, diff: RawPresenceDiff | PresenceDiff, onJoin: PresenceOnJoinCallback, onLeave: PresenceOnLeaveCallback): PresenceState; |
@@ -7,3 +7,4 @@ /* | ||
/** | ||
* Initializes the Presence | ||
* Initializes the Presence. | ||
* | ||
* @param channel - The RealtimeSubscription | ||
@@ -49,5 +50,7 @@ * @param opts - The options, | ||
/** | ||
* Used to sync the list of presences on the server | ||
* with the client's state. An optional `onJoin` and `onLeave` callback can | ||
* be provided to react to changes in the client's local presences across | ||
* Used to sync the list of presences on the server with the | ||
* client's state. | ||
* | ||
* An optional `onJoin` and `onLeave` callback can be provided to | ||
* react to changes in the client's local presences across | ||
* disconnects and reconnects with the server. | ||
@@ -86,7 +89,8 @@ */ | ||
/** | ||
* Used to sync a diff of presence join and leave events from the | ||
* server, as they happen. | ||
* | ||
* Used to sync a diff of presence join and leave | ||
* events from the server, as they happen. Like `syncState`, `syncDiff` | ||
* accepts optional `onJoin` and `onLeave` callbacks to react to a user | ||
* joining or leaving from a device. | ||
* Like `syncState`, `syncDiff` accepts optional `onJoin` and | ||
* `onLeave` callbacks to react to a user joining or leaving from a | ||
* device. | ||
*/ | ||
@@ -93,0 +97,0 @@ static syncDiff(state, diff, onJoin, onLeave) { |
{ | ||
"name": "@supabase/realtime-js", | ||
"version": "1.6.2", | ||
"version": "1.7.0", | ||
"description": "Listen to realtime updates to your PostgreSQL database", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -38,1 +38,8 @@ import { version } from './version' | ||
} | ||
export enum CONNECTION_STATE { | ||
Connecting = 'connecting', | ||
Open = 'open', | ||
Closing = 'closing', | ||
Closed = 'closed', | ||
} |
@@ -1,1 +0,1 @@ | ||
export const version = '1.6.2' | ||
export const version = '1.7.0' |
@@ -111,2 +111,5 @@ import { CHANNEL_EVENTS, CHANNEL_STATES } from './lib/constants' | ||
/** | ||
* Registers a callback that will be executed when the channel closes. | ||
*/ | ||
onClose(callback: Function) { | ||
@@ -116,2 +119,5 @@ this.on(CHANNEL_EVENTS.close, {}, callback) | ||
/** | ||
* Registers a callback that will be executed when the channel encounteres an error. | ||
*/ | ||
onError(callback: Function) { | ||
@@ -121,10 +127,6 @@ this.on(CHANNEL_EVENTS.error, {}, (reason: string) => callback(reason)) | ||
on( | ||
type: string, | ||
eventFilter?: { [key: string]: string }, | ||
callback?: Function | ||
) { | ||
on(type: string, filter?: { [key: string]: string }, callback?: Function) { | ||
this.bindings.push({ | ||
type, | ||
eventFilter: eventFilter ?? {}, | ||
filter: filter ?? {}, | ||
callback: callback ?? (() => {}), | ||
@@ -134,7 +136,6 @@ }) | ||
off(type: string, eventFilter: { [key: string]: any }) { | ||
off(type: string, filter: { [key: string]: any }) { | ||
this.bindings = this.bindings.filter((bind) => { | ||
return !( | ||
bind.type === type && | ||
RealtimeChannel.isEqual(bind.eventFilter, eventFilter) | ||
bind.type === type && RealtimeChannel.isEqual(bind.filter, filter) | ||
) | ||
@@ -144,3 +145,6 @@ }) | ||
canPush() { | ||
/** | ||
* Returns `true` if the socket is connected and the channel has been joined. | ||
*/ | ||
canPush(): boolean { | ||
return this.socket.isConnected() && this.isJoined() | ||
@@ -169,3 +173,3 @@ } | ||
/** | ||
* Leaves the channel | ||
* Leaves the channel. | ||
* | ||
@@ -178,5 +182,5 @@ * Unsubscribes from server events, and instructs channel to terminate on server. | ||
*/ | ||
unsubscribe(timeout = this.timeout) { | ||
unsubscribe(timeout = this.timeout): Push { | ||
this.state = CHANNEL_STATES.leaving | ||
let onClose = () => { | ||
const onClose = () => { | ||
this.socket.log('channel', `leave ${this.topic}`) | ||
@@ -188,3 +192,3 @@ this.trigger(CHANNEL_EVENTS.close, 'leave', this.joinRef()) | ||
let leavePush = new Push(this, CHANNEL_EVENTS.leave, {}, timeout) | ||
const leavePush = new Push(this, CHANNEL_EVENTS.leave, {}, timeout) | ||
leavePush.receive('ok', () => onClose()).receive('timeout', () => onClose()) | ||
@@ -209,11 +213,11 @@ leavePush.send() | ||
isMember(topic: string) { | ||
isMember(topic: string): boolean { | ||
return this.topic === topic | ||
} | ||
joinRef() { | ||
joinRef(): string { | ||
return this.joinPush.ref | ||
} | ||
rejoin(timeout = this.timeout) { | ||
rejoin(timeout = this.timeout): void { | ||
if (this.isLeaving()) { | ||
@@ -242,4 +246,4 @@ return | ||
bind?.type === type && | ||
(bind?.eventFilter?.event === '*' || | ||
bind?.eventFilter?.event === payload?.event) | ||
(bind?.filter?.event === '*' || | ||
bind?.filter?.event === payload?.event) | ||
) | ||
@@ -259,19 +263,19 @@ }) | ||
replyEventName(ref: string) { | ||
replyEventName(ref: string): string { | ||
return `chan_reply_${ref}` | ||
} | ||
isClosed() { | ||
isClosed(): boolean { | ||
return this.state === CHANNEL_STATES.closed | ||
} | ||
isErrored() { | ||
isErrored(): boolean { | ||
return this.state === CHANNEL_STATES.errored | ||
} | ||
isJoined() { | ||
isJoined(): boolean { | ||
return this.state === CHANNEL_STATES.joined | ||
} | ||
isJoining() { | ||
isJoining(): boolean { | ||
return this.state === CHANNEL_STATES.joining | ||
} | ||
isLeaving() { | ||
isLeaving(): boolean { | ||
return this.state === CHANNEL_STATES.leaving | ||
@@ -278,0 +282,0 @@ } |
@@ -10,2 +10,3 @@ import { w3cwebsocket } from 'websocket' | ||
DEFAULT_HEADERS, | ||
CONNECTION_STATE, | ||
} from './lib/constants' | ||
@@ -36,2 +37,7 @@ import Timer from './lib/timer' | ||
type ChannelParams = { | ||
selfBroadcast?: boolean | ||
[key: string]: any | ||
} | ||
const noop = () => {} | ||
@@ -71,5 +77,6 @@ | ||
} | ||
versionDate: Date | undefined = undefined | ||
/** | ||
* Initializes the Socket | ||
* Initializes the Socket. | ||
* | ||
@@ -121,5 +128,5 @@ * @param endPoint The string WebSocket endpoint, ie, "ws://example.com/socket", "wss://example.com", "/socket" (inherited host & protocol) | ||
/** | ||
* Connects the socket. | ||
* Connects the socket, unless already connected. | ||
*/ | ||
connect() { | ||
connect(): void { | ||
if (this.conn) { | ||
@@ -130,2 +137,3 @@ return | ||
this.conn = new this.transport(this.endPointURL(), [], null, this.headers) | ||
if (this.conn) { | ||
@@ -138,2 +146,10 @@ // this.conn.timeout = this.longpollerTimeout // TYPE ERROR | ||
this.conn.onclose = (event) => this._onConnClose(event) | ||
const versionDate = new Date( | ||
new URL(this.conn.url).searchParams.get('vsndate') ?? '' | ||
) | ||
if (versionDate instanceof Date) { | ||
this.versionDate = versionDate | ||
} | ||
} | ||
@@ -174,3 +190,5 @@ } | ||
/** | ||
* Logs the message. Override `this.logger` for specialized logging. | ||
* Logs the message. | ||
* | ||
* For customized logging, `this.logger` can be overriden. | ||
*/ | ||
@@ -183,2 +201,3 @@ log(kind: string, msg: string, data?: any) { | ||
* Registers a callback for connection state change event. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -194,3 +213,4 @@ * | ||
/** | ||
* Registers a callbacks for connection state change events. | ||
* Registers a callback for connection state change events. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -207,2 +227,3 @@ * | ||
* Registers a callback for connection state change events. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -219,2 +240,3 @@ * | ||
* Calls a function any time a message is received. | ||
* | ||
* @param callback A function to be called when the event occurs. | ||
@@ -232,12 +254,12 @@ * | ||
*/ | ||
connectionState() { | ||
connectionState(): CONNECTION_STATE { | ||
switch (this.conn && this.conn.readyState) { | ||
case SOCKET_STATES.connecting: | ||
return 'connecting' | ||
return CONNECTION_STATE.Connecting | ||
case SOCKET_STATES.open: | ||
return 'open' | ||
return CONNECTION_STATE.Open | ||
case SOCKET_STATES.closing: | ||
return 'closing' | ||
return CONNECTION_STATE.Closing | ||
default: | ||
return 'closed' | ||
return CONNECTION_STATE.Closed | ||
} | ||
@@ -249,4 +271,4 @@ } | ||
*/ | ||
isConnected() { | ||
return this.connectionState() === 'open' | ||
isConnected(): boolean { | ||
return this.connectionState() === CONNECTION_STATE.Open | ||
} | ||
@@ -268,9 +290,12 @@ | ||
topic: string, | ||
chanParams: { [key: string]: any } = { isNewVersion: false } | ||
) { | ||
const { isNewVersion, ...params } = chanParams | ||
chanParams: ChannelParams = {} | ||
): RealtimeChannel | RealtimeSubscription { | ||
const { selfBroadcast, ...params } = chanParams | ||
if (selfBroadcast) { | ||
params.self_broadcast = selfBroadcast | ||
} | ||
const chan = isNewVersion | ||
? new RealtimeChannel(topic, { ...params }, this) | ||
: new RealtimeSubscription(topic, { ...params }, this) | ||
const chan = this.versionDate | ||
? new RealtimeChannel(topic, params, this) | ||
: new RealtimeSubscription(topic, params, this) | ||
@@ -305,4 +330,9 @@ if (chan instanceof RealtimeChannel) { | ||
push(data: Message) { | ||
let { topic, event, payload, ref } = data | ||
/** | ||
* Push out a message if the socket is connected. | ||
* | ||
* If the socket is not connected, the message gets enqueued within a local buffer, and sent out when a connection is next established. | ||
*/ | ||
push(data: Message): void { | ||
const { topic, event, payload, ref } = data | ||
let callback = () => { | ||
@@ -321,3 +351,3 @@ this.encode(data, (result: any) => { | ||
onConnMessage(rawMessage: any) { | ||
onConnMessage(rawMessage: { data: any }) { | ||
this.decode(rawMessage.data, (msg: Message) => { | ||
@@ -354,3 +384,3 @@ let { topic, event, payload, ref } = msg | ||
*/ | ||
endPointURL() { | ||
endPointURL(): string { | ||
return this._appendParams( | ||
@@ -365,3 +395,3 @@ this.endPoint, | ||
*/ | ||
makeRef() { | ||
makeRef(): string { | ||
let newRef = this.ref + 1 | ||
@@ -385,15 +415,14 @@ if (newRef === this.ref) { | ||
try { | ||
this.channels.forEach((channel) => { | ||
token && channel.updateJoinPayload({ user_token: token }) | ||
this.channels.forEach((channel) => { | ||
token && channel.updateJoinPayload({ user_token: token }) | ||
if (channel.joinedOnce && channel.isJoined()) { | ||
channel.push(CHANNEL_EVENTS.access_token, { access_token: token }) | ||
} | ||
}) | ||
} catch (error) { | ||
console.log('setAuth error', error) | ||
} | ||
if (channel.joinedOnce && channel.isJoined()) { | ||
channel.push(CHANNEL_EVENTS.access_token, { access_token: token }) | ||
} | ||
}) | ||
} | ||
/** | ||
* Unsubscribe from channels with the specified topic. | ||
*/ | ||
leaveOpenTopic(topic: string): void { | ||
@@ -441,3 +470,6 @@ let dupChannel = this.channels.find( | ||
private _appendParams(url: string, params: { [key: string]: string }) { | ||
private _appendParams( | ||
url: string, | ||
params: { [key: string]: string } | ||
): string { | ||
if (Object.keys(params).length === 0) { | ||
@@ -444,0 +476,0 @@ return url |
@@ -58,3 +58,4 @@ /* | ||
/** | ||
* Initializes the Presence | ||
* Initializes the Presence. | ||
* | ||
* @param channel - The RealtimeSubscription | ||
@@ -115,5 +116,7 @@ * @param opts - The options, | ||
/** | ||
* Used to sync the list of presences on the server | ||
* with the client's state. An optional `onJoin` and `onLeave` callback can | ||
* be provided to react to changes in the client's local presences across | ||
* Used to sync the list of presences on the server with the | ||
* client's state. | ||
* | ||
* An optional `onJoin` and `onLeave` callback can be provided to | ||
* react to changes in the client's local presences across | ||
* disconnects and reconnects with the server. | ||
@@ -169,7 +172,8 @@ */ | ||
/** | ||
* Used to sync a diff of presence join and leave events from the | ||
* server, as they happen. | ||
* | ||
* Used to sync a diff of presence join and leave | ||
* events from the server, as they happen. Like `syncState`, `syncDiff` | ||
* accepts optional `onJoin` and `onLeave` callbacks to react to a user | ||
* joining or leaving from a device. | ||
* Like `syncState`, `syncDiff` accepts optional `onJoin` and | ||
* `onLeave` callbacks to react to a user joining or leaving from a | ||
* device. | ||
*/ | ||
@@ -176,0 +180,0 @@ static syncDiff( |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
326342
5891