Closes #34 - Fix connection state logic

This commit is contained in:
Skylar Sadlier 2021-11-25 00:00:31 -07:00
parent 2341cbef10
commit 447b18835c

View File

@ -133,36 +133,70 @@ module.exports = function(RED) {
}); });
node.matrixClient.on("sync", async function(state, prevState, data) { node.matrixClient.on("sync", async function(state, prevState, data) {
switch (state) { node.debug("SYNC [STATE=" + state + "] [PREVSTATE=" + prevState + "]");
case "ERROR": if(prevState === null && state === "PREPARED" ) {
node.setConnected(false, function(){ // Occurs when the initial sync is completed first time.
node.error("Connection to Matrix server lost"); // This involves setting up filters and obtaining push rules.
}); node.setConnected(true, function(){
break; node.log("Matrix client connected");
});
case "RECONNECTING": } else if(prevState === null && state === "ERROR") {
node.setConnected(false, function(){ // Occurs when the initial sync failed first time.
node.log("Trying to reconnect to matrix server"); node.setConnected(false, function(){
}); node.error("Failed to connect to Matrix server");
break; });
case "STOPPED": } else if(prevState === "ERROR" && state === "PREPARED") {
node.setConnected(false, function(){ // Occurs when the initial sync succeeds
node.log("Matrix client stopped"); // after previously failing.
}); node.setConnected(true, function(){
break; node.log("Matrix client connected");
});
case "SYNCING": } else if(prevState === "PREPARED" && state === "SYNCING") {
case "PREPARED": // Occurs immediately after transitioning to PREPARED.
node.setConnected(true, function(){ // Starts listening for live updates rather than catching up.
node.log("Matrix client connected"); node.setConnected(true, function(){
}); node.log("Matrix client connected");
break; });
} else if(prevState === "SYNCING" && state === "RECONNECTING") {
// case "PREPARED": // Occurs when the live update fails.
// // the client instance is ready to be queried. node.setConnected(false, function(){
// node.log("Matrix server connection ready."); node.error("Connection to Matrix server lost");
// node.setConnected(true); });
// break; } else if(prevState === "RECONNECTING" && state === "RECONNECTING") {
// Can occur if the update calls continue to fail,
// but the keepalive calls (to /versions) succeed.
node.setConnected(false, function(){
node.error("Connection to Matrix server lost");
});
} else if(prevState === "RECONNECTING" && state === "ERROR") {
// Occurs when the keepalive call also fails
node.setConnected(false, function(){
node.error("Connection to Matrix server lost");
});
} else if(prevState === "ERROR" && state === "SYNCING") {
// Occurs when the client has performed a
// live update after having previously failed.
node.setConnected(true, function(){
node.log("Matrix client connected");
});
} else if(prevState === "ERROR" && state === "ERROR") {
// Occurs when the client has failed to
// keepalive for a second time or more.
node.setConnected(false, function(){
node.error("Connection to Matrix server lost");
});
} else if(prevState === "SYNCING" && state === "SYNCING") {
// Occurs when the client has performed a live update.
// This is called <i>after</i> processing.
node.setConnected(true, function(){
node.log("Matrix client connected");
});
} else if(state === "STOPPED") {
// Occurs once the client has stopped syncing or
// trying to sync after stopClient has been called.
node.setConnected(false, function(){
node.error("Connection to Matrix server lost");
});
} }
}); });