home-assistant-js-websocket
Advanced tools
Comparing version 5.12.0 to 6.0.0
@@ -32,2 +32,3 @@ import { ERR_CONNECTION_LOST } from "./errors.js"; | ||
suspendReconnectPromise?: Promise<void>; | ||
oldSubscriptions?: Map<number, CommandInFlight>; | ||
_queuedMessages?: Array<{ | ||
@@ -37,7 +38,10 @@ resolve: (value?: unknown) => unknown; | ||
}>; | ||
socket: HaWebSocket; | ||
socket?: HaWebSocket; | ||
/** | ||
* Version string of the Home Assistant instance. Set to version of last connection while reconnecting. | ||
*/ | ||
haVersion: string; | ||
constructor(socket: HaWebSocket, options: ConnectionOptions); | ||
get haVersion(): string; | ||
get connected(): boolean; | ||
setSocket(socket: HaWebSocket): void; | ||
private _setSocket; | ||
addEventListener(eventType: Events, callback: ConnectionEventListener): void; | ||
@@ -44,0 +48,0 @@ removeEventListener(eventType: Events, callback: ConnectionEventListener): void; |
@@ -58,4 +58,9 @@ /** | ||
this._handleClose = async () => { | ||
const oldCommands = this.commands; | ||
// reset to original state except haVersion | ||
this.commandId = 1; | ||
this.commands = new Map(); | ||
this.socket = undefined; | ||
// Reject in-flight sendMessagePromise requests | ||
this.commands.forEach((info) => { | ||
oldCommands.forEach((info) => { | ||
// We don't cancel subscribeEvents commands in flight | ||
@@ -75,2 +80,5 @@ // as we will be able to recover them. | ||
setTimeout(async () => { | ||
if (this.closeRequested) { | ||
return; | ||
} | ||
if (DEBUG) { | ||
@@ -81,3 +89,3 @@ console.log("Trying to reconnect"); | ||
const socket = await options.createSocket(options); | ||
this.setSocket(socket); | ||
this._setSocket(socket); | ||
} | ||
@@ -124,22 +132,17 @@ catch (err) { | ||
this.closeRequested = false; | ||
this.setSocket(socket); | ||
this._setSocket(socket); | ||
} | ||
get haVersion() { | ||
return this.socket.haVersion; | ||
} | ||
get connected() { | ||
// Using conn.socket.OPEN instead of WebSocket for better node support | ||
return this.socket.readyState == this.socket.OPEN; | ||
return (this.socket !== undefined && this.socket.readyState == this.socket.OPEN); | ||
} | ||
setSocket(socket) { | ||
const oldSocket = this.socket; | ||
_setSocket(socket) { | ||
this.socket = socket; | ||
this.haVersion = socket.haVersion; | ||
socket.addEventListener("message", this._handleMessage); | ||
socket.addEventListener("close", this._handleClose); | ||
if (oldSocket) { | ||
const oldCommands = this.commands; | ||
// reset to original state | ||
this.commandId = 1; | ||
this.commands = new Map(); | ||
oldCommands.forEach((info) => { | ||
const oldSubscriptions = this.oldSubscriptions; | ||
if (oldSubscriptions) { | ||
this.oldSubscriptions = undefined; | ||
oldSubscriptions.forEach((info) => { | ||
if ("subscribe" in info && info.subscribe) { | ||
@@ -155,11 +158,11 @@ info.subscribe().then((unsub) => { | ||
}); | ||
const queuedMessages = this._queuedMessages; | ||
if (queuedMessages) { | ||
this._queuedMessages = undefined; | ||
for (const queuedMsg of queuedMessages) { | ||
queuedMsg.resolve(); | ||
} | ||
} | ||
const queuedMessages = this._queuedMessages; | ||
if (queuedMessages) { | ||
this._queuedMessages = undefined; | ||
for (const queuedMsg of queuedMessages) { | ||
queuedMsg.resolve(); | ||
} | ||
this.fireEvent("ready"); | ||
} | ||
this.fireEvent("ready"); | ||
} | ||
@@ -194,3 +197,5 @@ addEventListener(eventType, callback) { | ||
} | ||
this.socket.close(); | ||
if (this.socket) { | ||
this.socket.close(); | ||
} | ||
} | ||
@@ -202,2 +207,5 @@ /** | ||
reconnect(force = false) { | ||
if (!this.socket) { | ||
return; | ||
} | ||
if (!force) { | ||
@@ -214,3 +222,5 @@ this.socket.close(); | ||
this.closeRequested = true; | ||
this.socket.close(); | ||
if (this.socket) { | ||
this.socket.close(); | ||
} | ||
} | ||
@@ -231,2 +241,5 @@ /** | ||
sendMessage(message, commandId) { | ||
if (!this.connected) { | ||
throw ERR_CONNECTION_LOST; | ||
} | ||
if (DEBUG) { | ||
@@ -233,0 +246,0 @@ console.log("Sending", message); |
@@ -211,4 +211,9 @@ (function (global, factory) { | ||
this._handleClose = async () => { | ||
const oldCommands = this.commands; | ||
// reset to original state except haVersion | ||
this.commandId = 1; | ||
this.commands = new Map(); | ||
this.socket = undefined; | ||
// Reject in-flight sendMessagePromise requests | ||
this.commands.forEach((info) => { | ||
oldCommands.forEach((info) => { | ||
// We don't cancel subscribeEvents commands in flight | ||
@@ -228,5 +233,8 @@ // as we will be able to recover them. | ||
setTimeout(async () => { | ||
if (this.closeRequested) { | ||
return; | ||
} | ||
try { | ||
const socket = await options.createSocket(options); | ||
this.setSocket(socket); | ||
this._setSocket(socket); | ||
} | ||
@@ -273,22 +281,17 @@ catch (err) { | ||
this.closeRequested = false; | ||
this.setSocket(socket); | ||
this._setSocket(socket); | ||
} | ||
get haVersion() { | ||
return this.socket.haVersion; | ||
} | ||
get connected() { | ||
// Using conn.socket.OPEN instead of WebSocket for better node support | ||
return this.socket.readyState == this.socket.OPEN; | ||
return (this.socket !== undefined && this.socket.readyState == this.socket.OPEN); | ||
} | ||
setSocket(socket) { | ||
const oldSocket = this.socket; | ||
_setSocket(socket) { | ||
this.socket = socket; | ||
this.haVersion = socket.haVersion; | ||
socket.addEventListener("message", this._handleMessage); | ||
socket.addEventListener("close", this._handleClose); | ||
if (oldSocket) { | ||
const oldCommands = this.commands; | ||
// reset to original state | ||
this.commandId = 1; | ||
this.commands = new Map(); | ||
oldCommands.forEach((info) => { | ||
const oldSubscriptions = this.oldSubscriptions; | ||
if (oldSubscriptions) { | ||
this.oldSubscriptions = undefined; | ||
oldSubscriptions.forEach((info) => { | ||
if ("subscribe" in info && info.subscribe) { | ||
@@ -304,11 +307,11 @@ info.subscribe().then((unsub) => { | ||
}); | ||
const queuedMessages = this._queuedMessages; | ||
if (queuedMessages) { | ||
this._queuedMessages = undefined; | ||
for (const queuedMsg of queuedMessages) { | ||
queuedMsg.resolve(); | ||
} | ||
} | ||
const queuedMessages = this._queuedMessages; | ||
if (queuedMessages) { | ||
this._queuedMessages = undefined; | ||
for (const queuedMsg of queuedMessages) { | ||
queuedMsg.resolve(); | ||
} | ||
this.fireEvent("ready"); | ||
} | ||
this.fireEvent("ready"); | ||
} | ||
@@ -343,3 +346,5 @@ addEventListener(eventType, callback) { | ||
} | ||
this.socket.close(); | ||
if (this.socket) { | ||
this.socket.close(); | ||
} | ||
} | ||
@@ -351,2 +356,5 @@ /** | ||
reconnect(force = false) { | ||
if (!this.socket) { | ||
return; | ||
} | ||
if (!force) { | ||
@@ -363,3 +371,5 @@ this.socket.close(); | ||
this.closeRequested = true; | ||
this.socket.close(); | ||
if (this.socket) { | ||
this.socket.close(); | ||
} | ||
} | ||
@@ -380,2 +390,5 @@ /** | ||
sendMessage(message, commandId) { | ||
if (!this.connected) { | ||
throw ERR_CONNECTION_LOST; | ||
} | ||
if (this._queuedMessages) { | ||
@@ -382,0 +395,0 @@ if (commandId) { |
@@ -5,3 +5,3 @@ { | ||
"sideEffects": false, | ||
"version": "5.12.0", | ||
"version": "6.0.0", | ||
"description": "Home Assistant websocket client", | ||
@@ -8,0 +8,0 @@ "source": "lib/index.ts", |
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
154935
3193