@knocklabs/client
Advanced tools
Comparing version 0.5.10 to 0.6.0-rc.1
@@ -27,5 +27,2 @@ "use strict"; | ||
var ApiClient = /*#__PURE__*/function () { | ||
/** | ||
* @deprecated Use `socket.connectionState` instead. | ||
*/ | ||
function ApiClient(options) { | ||
@@ -37,3 +34,2 @@ (0, _classCallCheck2["default"])(this, ApiClient); | ||
(0, _defineProperty2["default"])(this, "axiosClient", void 0); | ||
(0, _defineProperty2["default"])(this, "socketConnected", false); | ||
(0, _defineProperty2["default"])(this, "socket", void 0); | ||
@@ -54,2 +50,4 @@ this.host = options.host; | ||
this.socket = new _phoenix.Socket("".concat(this.host.replace("http", "ws"), "/ws/v1"), { | ||
// If we're in a non-browser environment, then fallback to longpolling | ||
transport: typeof window === "undefined" ? _phoenix.LongPoll : window.WebSocket, | ||
params: { | ||
@@ -66,42 +64,4 @@ user_token: this.userToken, | ||
} | ||
/** | ||
* @deprecated Use `socket.connect()` instead. | ||
*/ | ||
(0, _createClass2["default"])(ApiClient, [{ | ||
key: "connectSocket", | ||
value: function connectSocket() { | ||
var _this = this; | ||
if (this.socketConnected) { | ||
return; | ||
} | ||
this.socket.connect(); | ||
this.socket.onOpen(function () { | ||
_this.socketConnected = true; | ||
}); | ||
} | ||
/** | ||
* @deprecated Use `socket.disconnect()` instead. | ||
*/ | ||
}, { | ||
key: "disconnectSocket", | ||
value: function disconnectSocket() { | ||
this.socket.disconnect(); | ||
this.socketConnected = false; | ||
return; | ||
} | ||
/** | ||
* @deprecated Use `socket.channel(name: string, params?: object)` instead. | ||
*/ | ||
}, { | ||
key: "createChannel", | ||
value: function createChannel(name, params) { | ||
return this.socket.channel(name, params); | ||
} | ||
}, { | ||
key: "makeRequest", | ||
@@ -108,0 +68,0 @@ value: function () { |
@@ -33,2 +33,4 @@ "use strict"; | ||
function Feed(knock, feedId, options) { | ||
var _this = this; | ||
(0, _classCallCheck2["default"])(this, Feed); | ||
@@ -51,42 +53,39 @@ this.knock = knock; | ||
}); | ||
this.defaultOptions = options; // Try and connect to the socket | ||
this.apiClient.socket.connect(); | ||
this.defaultOptions = options; | ||
this.channel = this.apiClient.socket.channel("feeds:".concat(this.userFeedId), this.defaultOptions); | ||
this.channel.on("new-message", function (resp) { | ||
return _this.onNewMessageReceived(resp); | ||
}); | ||
} | ||
/* | ||
Returns a socket to listen for feed updates | ||
*/ | ||
/** | ||
* Cleans up a feed instance by destroying the store and disconnecting | ||
* an open socket connection. | ||
*/ | ||
(0, _createClass2["default"])(Feed, [{ | ||
key: "teardown", | ||
value: function teardown() { | ||
this.channel.leave(); | ||
this.broadcaster.removeAllListeners(); | ||
this.channel.off("new-message"); | ||
this.store.destroy(); | ||
} | ||
/* | ||
Initializes a real-time connection to Knock, connecting the websocket for the | ||
current ApiClient instance if the socket is not already connected. | ||
*/ | ||
}, { | ||
key: "listenForUpdates", | ||
value: function listenForUpdates() { | ||
var _this = this; | ||
// Connect the socket only if we don't already have a connection | ||
if (!this.apiClient.socket.isConnected()) { | ||
this.apiClient.socket.connect(); | ||
} // Only join the channel if we're not already in a joining state | ||
try { | ||
if (["closed", "errored"].includes(this.channel.state)) { | ||
this.channel.join(); | ||
} catch (e) { | ||
// Phoenix raises an error when trying to call join more than once. Given calling listenForUpdates unintentionally | ||
// may happen fairly often, we choose to supress the error and print a warning. | ||
if (e.message.includes("join")) { | ||
console.warn(e.message); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
this.channel.on("new-message", function (resp) { | ||
return _this.onNewMessageReceived(resp); | ||
}); | ||
return function () { | ||
try { | ||
_this.channel.leave(); | ||
_this.channel.off("new-message"); | ||
} catch (e) { | ||
// tslint:disable-next-line | ||
console.error("error while leaving channel", e); | ||
} | ||
}; | ||
} | ||
@@ -93,0 +92,0 @@ /* Binds a handler to be invoked when event occurs */ |
@@ -76,6 +76,3 @@ "use strict"; | ||
value: function teardown() { | ||
if (!this.apiClient) { | ||
return; | ||
} | ||
if (!this.apiClient) return; | ||
this.apiClient.socket.disconnect(); | ||
@@ -82,0 +79,0 @@ } |
@@ -5,8 +5,5 @@ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; | ||
import axiosRetry from "axios-retry"; | ||
import { Socket } from "phoenix"; | ||
import { LongPoll, Socket } from "phoenix"; | ||
class ApiClient { | ||
/** | ||
* @deprecated Use `socket.connectionState` instead. | ||
*/ | ||
constructor(options) { | ||
@@ -21,4 +18,2 @@ _defineProperty(this, "host", void 0); | ||
_defineProperty(this, "socketConnected", false); | ||
_defineProperty(this, "socket", void 0); | ||
@@ -40,2 +35,4 @@ | ||
this.socket = new Socket("".concat(this.host.replace("http", "ws"), "/ws/v1"), { | ||
// If we're in a non-browser environment, then fallback to longpolling | ||
transport: typeof window === "undefined" ? LongPoll : window.WebSocket, | ||
params: { | ||
@@ -52,36 +49,3 @@ user_token: this.userToken, | ||
} | ||
/** | ||
* @deprecated Use `socket.connect()` instead. | ||
*/ | ||
connectSocket() { | ||
if (this.socketConnected) { | ||
return; | ||
} | ||
this.socket.connect(); | ||
this.socket.onOpen(() => { | ||
this.socketConnected = true; | ||
}); | ||
} | ||
/** | ||
* @deprecated Use `socket.disconnect()` instead. | ||
*/ | ||
disconnectSocket() { | ||
this.socket.disconnect(); | ||
this.socketConnected = false; | ||
return; | ||
} | ||
/** | ||
* @deprecated Use `socket.channel(name: string, params?: object)` instead. | ||
*/ | ||
createChannel(name, params) { | ||
return this.socket.channel(name, params); | ||
} | ||
makeRequest(req) { | ||
@@ -88,0 +52,0 @@ var _this = this; |
@@ -38,9 +38,21 @@ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; | ||
}); | ||
this.defaultOptions = options; // Try and connect to the socket | ||
this.apiClient.socket.connect(); | ||
this.defaultOptions = options; | ||
this.channel = this.apiClient.socket.channel("feeds:".concat(this.userFeedId), this.defaultOptions); | ||
this.channel.on("new-message", resp => this.onNewMessageReceived(resp)); | ||
} | ||
/** | ||
* Cleans up a feed instance by destroying the store and disconnecting | ||
* an open socket connection. | ||
*/ | ||
teardown() { | ||
this.channel.leave(); | ||
this.broadcaster.removeAllListeners(); | ||
this.channel.off("new-message"); | ||
this.store.destroy(); | ||
} | ||
/* | ||
Returns a socket to listen for feed updates | ||
Initializes a real-time connection to Knock, connecting the websocket for the | ||
current ApiClient instance if the socket is not already connected. | ||
*/ | ||
@@ -50,24 +62,11 @@ | ||
listenForUpdates() { | ||
try { | ||
// Connect the socket only if we don't already have a connection | ||
if (!this.apiClient.socket.isConnected()) { | ||
this.apiClient.socket.connect(); | ||
} // Only join the channel if we're not already in a joining state | ||
if (["closed", "errored"].includes(this.channel.state)) { | ||
this.channel.join(); | ||
} catch (e) { | ||
// Phoenix raises an error when trying to call join more than once. Given calling listenForUpdates unintentionally | ||
// may happen fairly often, we choose to supress the error and print a warning. | ||
if (e.message.includes("join")) { | ||
console.warn(e.message); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
this.channel.on("new-message", resp => this.onNewMessageReceived(resp)); | ||
return () => { | ||
try { | ||
this.channel.leave(); | ||
this.channel.off("new-message"); | ||
} catch (e) { | ||
// tslint:disable-next-line | ||
console.error("error while leaving channel", e); | ||
} | ||
}; | ||
} | ||
@@ -74,0 +73,0 @@ /* Binds a handler to be invoked when event occurs */ |
@@ -61,6 +61,3 @@ import _defineProperty from "@babel/runtime/helpers/defineProperty"; | ||
teardown() { | ||
if (!this.apiClient) { | ||
return; | ||
} | ||
if (!this.apiClient) return; | ||
this.apiClient.socket.disconnect(); | ||
@@ -67,0 +64,0 @@ } |
@@ -19,20 +19,4 @@ import { AxiosRequestConfig } from "axios"; | ||
private axiosClient; | ||
/** | ||
* @deprecated Use `socket.connectionState` instead. | ||
*/ | ||
socketConnected: boolean; | ||
socket: Socket; | ||
constructor(options: ApiClientOptions); | ||
/** | ||
* @deprecated Use `socket.connect()` instead. | ||
*/ | ||
connectSocket(): void; | ||
/** | ||
* @deprecated Use `socket.disconnect()` instead. | ||
*/ | ||
disconnectSocket(): void; | ||
/** | ||
* @deprecated Use `socket.channel(name: string, params?: object)` instead. | ||
*/ | ||
createChannel(name: string, params?: object): import("phoenix").Channel; | ||
makeRequest(req: AxiosRequestConfig): Promise<ApiResponse>; | ||
@@ -39,0 +23,0 @@ private canRetryRequest; |
@@ -16,3 +16,8 @@ import { StoreApi } from "zustand"; | ||
constructor(knock: Knock, feedId: string, options: FeedClientOptions); | ||
listenForUpdates(): () => void; | ||
/** | ||
* Cleans up a feed instance by destroying the store and disconnecting | ||
* an open socket connection. | ||
*/ | ||
teardown(): void; | ||
listenForUpdates(): void; | ||
on(eventName: BindableFeedEvent, callback: FeedEventCallback | FeedRealTimeCallback): void; | ||
@@ -19,0 +24,0 @@ off(eventName: BindableFeedEvent, callback: FeedEventCallback | FeedRealTimeCallback): void; |
{ | ||
"name": "@knocklabs/client", | ||
"version": "0.5.10", | ||
"version": "0.6.0-rc.1", | ||
"description": "The clientside library for interacting with Knock", | ||
@@ -51,3 +51,3 @@ "homepage": "https://github.com/knocklabs/knock-client-js", | ||
"@babel/preset-typescript": "^7.16.7", | ||
"@types/phoenix": "^1.5.1", | ||
"@types/phoenix": "^1.5.4", | ||
"@typescript-eslint/eslint-plugin": "^5.4.0", | ||
@@ -54,0 +54,0 @@ "@typescript-eslint/parser": "^5.4.0", |
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
212320
2360