@slack/socket-mode
Advanced tools
Comparing version 1.3.0 to 1.3.1
@@ -46,2 +46,6 @@ import { EventEmitter } from 'eventemitter3'; | ||
private stateMachine; | ||
/** | ||
* Internal count for managing the reconnection state | ||
*/ | ||
private numOfConsecutiveReconnectionFailures; | ||
private connectingStateMachineConfig; | ||
@@ -48,0 +52,0 @@ private connectedStateMachineConfig; |
@@ -18,8 +18,4 @@ "use strict"; | ||
(function (State) { | ||
State["Ready"] = "ready"; | ||
State["Connecting"] = "connecting"; | ||
State["Connected"] = "connected"; | ||
State["Handshaking"] = "handshaking"; | ||
State["Authenticating"] = "authenticating"; | ||
State["Authenticated"] = "authenticated"; | ||
State["Reconnecting"] = "reconnecting"; | ||
@@ -30,2 +26,16 @@ State["Disconnecting"] = "disconnecting"; | ||
})(State || (State = {})); | ||
var ConnectingState; | ||
(function (ConnectingState) { | ||
ConnectingState["Handshaking"] = "handshaking"; | ||
ConnectingState["Authenticating"] = "authenticating"; | ||
ConnectingState["Authenticated"] = "authenticated"; | ||
ConnectingState["Reconnecting"] = "reconnecting"; | ||
ConnectingState["Failed"] = "failed"; | ||
})(ConnectingState || (ConnectingState = {})); | ||
var ConnectedState; | ||
(function (ConnectedState) { | ||
ConnectedState["Preparing"] = "preparing"; | ||
ConnectedState["Ready"] = "ready"; | ||
ConnectedState["Failed"] = "failed"; | ||
})(ConnectedState || (ConnectedState = {})); | ||
// These enum values are used only in the state machine | ||
@@ -65,2 +75,6 @@ var Event; | ||
this.authenticated = false; | ||
/** | ||
* Internal count for managing the reconnection state | ||
*/ | ||
this.numOfConsecutiveReconnectionFailures = 0; | ||
/* eslint-disable @typescript-eslint/indent, newline-per-chained-call */ | ||
@@ -72,13 +86,24 @@ this.connectingStateMachineConfig = finity_1.default.configure() | ||
}) | ||
.initialState(State.Authenticating) | ||
.initialState(ConnectingState.Authenticating) | ||
.do(this.retrieveWSSURL.bind(this)) | ||
.onSuccess().transitionTo(State.Authenticated) | ||
.onSuccess().transitionTo(ConnectingState.Authenticated) | ||
.onFailure() | ||
.transitionTo(State.Reconnecting).withCondition(this.reconnectingCondition.bind(this)) | ||
.transitionTo(State.Failed) | ||
.state(State.Authenticated) | ||
.transitionTo(ConnectingState.Reconnecting).withCondition(this.reconnectingCondition.bind(this)) | ||
.transitionTo(ConnectingState.Failed) | ||
.state(ConnectingState.Reconnecting) | ||
.do(async () => { | ||
// Trying to reconnect after waiting for a bit... | ||
this.numOfConsecutiveReconnectionFailures += 1; | ||
const millisBeforeRetry = this.clientPingTimeoutMillis * this.numOfConsecutiveReconnectionFailures; | ||
this.logger.debug(`Before trying to reconnect, this client will wait for ${millisBeforeRetry} milliseconds`); | ||
setTimeout(() => { | ||
this.emit(ConnectingState.Authenticating); | ||
}, millisBeforeRetry); | ||
}) | ||
.onFailure().transitionTo(ConnectingState.Failed) | ||
.state(ConnectingState.Authenticated) | ||
.onEnter(this.configureAuthenticatedWebSocket.bind(this)) | ||
.on(Event.WebSocketOpen).transitionTo(State.Handshaking) | ||
.state(State.Handshaking) // a state in which to wait until the Event.ServerHello event | ||
.state(State.Failed) | ||
.on(Event.WebSocketOpen).transitionTo(ConnectingState.Handshaking) | ||
.state(ConnectingState.Handshaking) // a state in which to wait until the Event.ServerHello event | ||
.state(ConnectingState.Failed) | ||
.onEnter(this.handleConnectionFailure.bind(this)) | ||
@@ -91,4 +116,4 @@ .getConfig(); | ||
}) | ||
.initialState(State.Ready) | ||
.onEnter(() => { | ||
.initialState(ConnectedState.Preparing) | ||
.do(async () => { | ||
if (this.isSwitchingConnection) { | ||
@@ -103,7 +128,7 @@ this.switchWebSocketConnection(); | ||
this.startMonitoringPingFromSlack(); | ||
// The transition isn't done yet, so we delay the following statement until after the event loop returns | ||
setImmediate(() => { | ||
this.emit(State.Ready); | ||
}); | ||
}) | ||
.onSuccess().transitionTo(ConnectedState.Ready) | ||
.onFailure().transitionTo(ConnectedState.Failed) | ||
.state(ConnectedState.Failed) | ||
.onEnter(this.handleConnectionFailure.bind(this)) | ||
.getConfig(); | ||
@@ -246,3 +271,3 @@ /** | ||
return new Promise((resolve, reject) => { | ||
this.once(State.Authenticated, (result) => { | ||
this.once(ConnectingState.Authenticated, (result) => { | ||
this.removeListener(State.Disconnected, reject); | ||
@@ -252,3 +277,3 @@ resolve(result); | ||
this.once(State.Disconnected, (err) => { | ||
this.removeListener(State.Authenticated, resolve); | ||
this.removeListener(ConnectingState.Authenticated, resolve); | ||
reject(err); | ||
@@ -317,3 +342,3 @@ }); | ||
catch (error) { | ||
this.logger.error(`Faled to retrieve a new WSS URL for reconnection (error: ${error})`); | ||
this.logger.error(`Failed to retrieve a new WSS URL for reconnection (error: ${error})`); | ||
throw error; | ||
@@ -344,6 +369,7 @@ } | ||
configureAuthenticatedWebSocket(_state, context) { | ||
this.numOfConsecutiveReconnectionFailures = 0; // Reset the failure count | ||
this.authenticated = true; | ||
this.setupWebSocket(context.result.url); | ||
setImmediate(() => { | ||
this.emit(State.Authenticated, context.result); | ||
this.emit(ConnectingState.Authenticated, context.result); | ||
}); | ||
@@ -556,3 +582,4 @@ } | ||
stateHierarchy.length >= 2 && | ||
stateHierarchy[1] === State.Ready; | ||
// When the primary state is State.Connected, the second one is always set by the sub state machine | ||
stateHierarchy[1].toString() === ConnectedState.Ready; | ||
} | ||
@@ -559,0 +586,0 @@ /** |
{ | ||
"name": "@slack/socket-mode", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "Official library for using the Slack Platform's Socket Mode API", | ||
@@ -5,0 +5,0 @@ "author": "Slack Technologies, LLC", |
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
84603
1025