Skip to content

Commit 59b3d85

Browse files
authored
Allow disconnect call to cancel current connection attempt (qzind#1117)
Allow disconnect call to cancel current connection attempt
1 parent c6b0ffa commit 59b3d85

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

js/qz-tray.js

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ var qz = (function() {
5252
websocket: {
5353
/** The actual websocket object managing the connection. */
5454
connection: null,
55+
/** Track if a connection attempt is being cancelled. */
56+
shutdown: false,
5557

5658
/** Default parameters used on new connections. Override values using options parameter on {@link qz.websocket.connect}. */
5759
connectConfig: {
@@ -75,6 +77,11 @@ var qz = (function() {
7577
setup: {
7678
/** Loop through possible ports to open connection, sets web socket calls that will settle the promise. */
7779
findConnection: function(config, resolve, reject) {
80+
if (_qz.websocket.shutdown) {
81+
reject(new Error("Connection attempt cancelled by user"));
82+
return;
83+
}
84+
7885
//force flag if missing ports
7986
if (!config.port.secure.length) {
8087
if (!config.port.insecure.length) {
@@ -90,6 +97,12 @@ var qz = (function() {
9097
}
9198

9299
var deeper = function() {
100+
if (_qz.websocket.shutdown) {
101+
//connection attempt was cancelled, bail out
102+
reject(new Error("Connection attempt cancelled by user"));
103+
return;
104+
}
105+
93106
config.port.portIndex++;
94107

95108
if ((config.usingSecure && config.port.portIndex >= config.port.secure.length)
@@ -765,7 +778,9 @@ var qz = (function() {
765778
},
766779

767780
isActive: function() {
768-
return _qz.websocket.connection != null && _qz.websocket.connection.established;
781+
return !_qz.websocket.shutdown && _qz.websocket.connection != null
782+
&& (_qz.websocket.connection.readyState === _qz.tools.ws.OPEN
783+
|| _qz.websocket.connection.readyState === _qz.tools.ws.CONNECTING);
769784
},
770785

771786
assertActive: function() {
@@ -1164,12 +1179,19 @@ var qz = (function() {
11641179
*/
11651180
connect: function(options) {
11661181
return _qz.tools.promise(function(resolve, reject) {
1167-
if (_qz.tools.isActive()) {
1168-
reject(new Error("An open connection with QZ Tray already exists"));
1169-
return;
1170-
} else if (_qz.websocket.connection != null) {
1171-
reject(new Error("The current connection attempt has not returned yet"));
1172-
return;
1182+
if (_qz.websocket.connection) {
1183+
const state = _qz.websocket.connection.readyState;
1184+
1185+
if (state === _qz.tools.ws.OPEN) {
1186+
reject(new Error("An open connection with QZ Tray already exists"));
1187+
return;
1188+
} else if (state === _qz.tools.ws.CONNECTING) {
1189+
reject(new Error("The current connection attempt has not returned yet"));
1190+
return;
1191+
} else if (state === _qz.tools.ws.CLOSING) {
1192+
reject(new Error("Waiting for previous disconnect request to complete"));
1193+
return;
1194+
}
11731195
}
11741196

11751197
if (!_qz.tools.ws) {
@@ -1197,6 +1219,7 @@ var qz = (function() {
11971219
options.host = [options.host];
11981220
}
11991221

1222+
_qz.websocket.shutdown = false; //reset state for new connection attempt
12001223
var attempt = function(count) {
12011224
var tried = false;
12021225
var nextAttempt = function() {
@@ -1236,11 +1259,17 @@ var qz = (function() {
12361259
*/
12371260
disconnect: function() {
12381261
return _qz.tools.promise(function(resolve, reject) {
1239-
if (_qz.tools.isActive()) {
1240-
_qz.websocket.connection.close();
1241-
_qz.websocket.connection.promise = { resolve: resolve, reject: reject };
1262+
if (_qz.websocket.connection != null) {
1263+
if (_qz.tools.isActive()) {
1264+
// handles closing both 'connecting' and 'connected' states
1265+
_qz.websocket.shutdown = true;
1266+
_qz.websocket.connection.promise = { resolve: resolve, reject: reject };
1267+
_qz.websocket.connection.close();
1268+
} else {
1269+
reject(new Error("Current connection is still closing"));
1270+
}
12421271
} else {
1243-
reject(new Error("No open connection with QZ Tray"))
1272+
reject(new Error("No open connection with QZ Tray"));
12441273
}
12451274
});
12461275
},

0 commit comments

Comments
 (0)