@knocklabs/client
Advanced tools
Comparing version 0.8.8 to 0.8.9
@@ -47,10 +47,12 @@ "use strict"; | ||
}); | ||
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: { | ||
user_token: this.userToken, | ||
api_key: this.apiKey | ||
} | ||
}); | ||
if (typeof window !== "undefined") { | ||
this.socket = new _phoenix.Socket("".concat(this.host.replace("http", "ws"), "/ws/v1"), { | ||
params: { | ||
user_token: this.userToken, | ||
api_key: this.apiKey | ||
} | ||
}); | ||
} | ||
(0, _axiosRetry["default"])(this.axiosClient, { | ||
@@ -57,0 +59,0 @@ retries: 3, |
@@ -58,10 +58,14 @@ "use strict"; | ||
}); | ||
this.defaultOptions = _objectSpread(_objectSpread({}, feedClientDefaults), options); | ||
this.channel = this.apiClient.socket.channel("feeds:".concat(this.userFeedId), this.defaultOptions); | ||
this.channel.on("new-message", function (resp) { | ||
return _this.onNewMessageReceived(resp); | ||
}); // Attempt to bind to listen to other events from this feed in different tabs | ||
this.defaultOptions = _objectSpread(_objectSpread({}, feedClientDefaults), options); // In server environments we might not have a socket connection | ||
if (this.apiClient.socket) { | ||
this.channel = this.apiClient.socket.channel("feeds:".concat(this.userFeedId), this.defaultOptions); | ||
this.channel.on("new-message", function (resp) { | ||
return _this.onNewMessageReceived(resp); | ||
}); | ||
} // Attempt to bind to listen to other events from this feed in different tabs | ||
// Note: here we ensure `self` is available (it's not in server rendered envs) | ||
this.broadcastChannel = self && "BroadcastChannel" in self ? new BroadcastChannel("knock:feed:".concat(this.userFeedId)) : null; | ||
this.broadcastChannel = typeof self !== "undefined" && "BroadcastChannel" in self ? new BroadcastChannel("knock:feed:".concat(this.userFeedId)) : null; | ||
} | ||
@@ -77,5 +81,8 @@ /** | ||
value: function teardown() { | ||
this.channel.leave(); | ||
if (this.channel) { | ||
this.channel.leave(); | ||
this.channel.off("new-message"); | ||
} | ||
this.broadcaster.removeAllListeners(); | ||
this.channel.off("new-message"); | ||
this.store.destroy(); | ||
@@ -98,3 +105,3 @@ | ||
// Connect the socket only if we don't already have a connection | ||
if (!this.apiClient.socket.isConnected()) { | ||
if (this.apiClient.socket && !this.apiClient.socket.isConnected()) { | ||
this.apiClient.socket.connect(); | ||
@@ -104,3 +111,3 @@ } // Only join the channel if we're not already in a joining state | ||
if (["closed", "errored"].includes(this.channel.state)) { | ||
if (this.channel && ["closed", "errored"].includes(this.channel.state)) { | ||
this.channel.join(); | ||
@@ -107,0 +114,0 @@ } // Opt into receiving updates from _other tabs for the same user / feed_ via the broadcast |
@@ -88,3 +88,6 @@ "use strict"; | ||
if (!this.apiClient) return; | ||
this.apiClient.socket.disconnect(); | ||
if (this.apiClient.socket) { | ||
this.apiClient.socket.disconnect(); | ||
} | ||
} | ||
@@ -91,0 +94,0 @@ }]); |
@@ -5,3 +5,3 @@ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; | ||
import axiosRetry from "axios-retry"; | ||
import { LongPoll, Socket } from "phoenix"; | ||
import { Socket } from "phoenix"; | ||
@@ -33,10 +33,12 @@ class ApiClient { | ||
}); | ||
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: { | ||
user_token: this.userToken, | ||
api_key: this.apiKey | ||
} | ||
}); | ||
if (typeof window !== "undefined") { | ||
this.socket = new Socket("".concat(this.host.replace("http", "ws"), "/ws/v1"), { | ||
params: { | ||
user_token: this.userToken, | ||
api_key: this.apiKey | ||
} | ||
}); | ||
} | ||
axiosRetry(this.axiosClient, { | ||
@@ -43,0 +45,0 @@ retries: 3, |
@@ -44,8 +44,12 @@ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; | ||
}); | ||
this.defaultOptions = _objectSpread(_objectSpread({}, feedClientDefaults), options); | ||
this.channel = this.apiClient.socket.channel("feeds:".concat(this.userFeedId), this.defaultOptions); | ||
this.channel.on("new-message", resp => this.onNewMessageReceived(resp)); // Attempt to bind to listen to other events from this feed in different tabs | ||
this.defaultOptions = _objectSpread(_objectSpread({}, feedClientDefaults), options); // In server environments we might not have a socket connection | ||
if (this.apiClient.socket) { | ||
this.channel = this.apiClient.socket.channel("feeds:".concat(this.userFeedId), this.defaultOptions); | ||
this.channel.on("new-message", resp => this.onNewMessageReceived(resp)); | ||
} // Attempt to bind to listen to other events from this feed in different tabs | ||
// Note: here we ensure `self` is available (it's not in server rendered envs) | ||
this.broadcastChannel = self && "BroadcastChannel" in self ? new BroadcastChannel("knock:feed:".concat(this.userFeedId)) : null; | ||
this.broadcastChannel = typeof self !== "undefined" && "BroadcastChannel" in self ? new BroadcastChannel("knock:feed:".concat(this.userFeedId)) : null; | ||
} | ||
@@ -59,5 +63,8 @@ /** | ||
teardown() { | ||
this.channel.leave(); | ||
if (this.channel) { | ||
this.channel.leave(); | ||
this.channel.off("new-message"); | ||
} | ||
this.broadcaster.removeAllListeners(); | ||
this.channel.off("new-message"); | ||
this.store.destroy(); | ||
@@ -77,3 +84,3 @@ | ||
// Connect the socket only if we don't already have a connection | ||
if (!this.apiClient.socket.isConnected()) { | ||
if (this.apiClient.socket && !this.apiClient.socket.isConnected()) { | ||
this.apiClient.socket.connect(); | ||
@@ -83,3 +90,3 @@ } // Only join the channel if we're not already in a joining state | ||
if (["closed", "errored"].includes(this.channel.state)) { | ||
if (this.channel && ["closed", "errored"].includes(this.channel.state)) { | ||
this.channel.join(); | ||
@@ -86,0 +93,0 @@ } // Opt into receiving updates from _other tabs for the same user / feed_ via the broadcast |
@@ -72,3 +72,6 @@ import _defineProperty from "@babel/runtime/helpers/defineProperty"; | ||
if (!this.apiClient) return; | ||
this.apiClient.socket.disconnect(); | ||
if (this.apiClient.socket) { | ||
this.apiClient.socket.disconnect(); | ||
} | ||
} | ||
@@ -75,0 +78,0 @@ |
@@ -19,3 +19,3 @@ import { AxiosRequestConfig } from "axios"; | ||
private axiosClient; | ||
socket: Socket; | ||
socket: Socket | undefined; | ||
constructor(options: ApiClientOptions); | ||
@@ -22,0 +22,0 @@ makeRequest(req: AxiosRequestConfig): Promise<ApiResponse>; |
{ | ||
"name": "@knocklabs/client", | ||
"version": "0.8.8", | ||
"version": "0.8.9", | ||
"description": "The clientside library for interacting with Knock", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/knocklabs/knock-client-js", |
@@ -68,3 +68,3 @@ # Knock Javascript client library | ||
// Connect to the real-time socket | ||
const teardown = feedClient.listenForUpdates(); | ||
feedClient.listenForUpdates(); | ||
@@ -101,3 +101,3 @@ // Setup a callback for when a batch of items is received (including on first load and subsequent page load) | ||
teardown(); | ||
feedClient.teardown(); | ||
``` | ||
@@ -104,0 +104,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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
280500
3014