Skip to content

Commit 754952d

Browse files
committed
fixed bug where if there are no servers the editor spawns with an empty
json object "{}"
1 parent ad2d840 commit 754952d

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

src/configuration.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ impl Config {
2020
self.port = new_config.port;
2121
self.servers = new_config.servers.clone();
2222
self.themes_folder = new_config.themes_folder.clone();
23+
self.slave = new_config.slave;
24+
self.slave_connections = new_config.slave_connections.clone();
2325
}
2426

2527
pub fn update_config_file(&self, file_path: &str) {

src/html_src/index.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,15 @@ $(document).ready(function () {
404404
var justStarted = true;
405405
var editor; // Monaco editor instance
406406

407+
// Send requestConfig at startup
408+
function sendRequestConfig() {
409+
if (socket && socket.readyState === WebSocket.OPEN) {
410+
socket.send(JSON.stringify({ type: "requestConfig" }));
411+
} else {
412+
setTimeout(sendRequestConfig, 100);
413+
}
414+
}
415+
407416
// Initialize Monaco Editor
408417
function initMonacoEditor() {
409418
if (typeof monaco !== "undefined") {
@@ -468,19 +477,23 @@ $(document).ready(function () {
468477
socket.onmessage = function (message) {
469478
var obj = JSON.parse(message.data);
470479
switch (obj.type) {
480+
case "ConfigInfo":
481+
// Always update config editor with received config
482+
window.config = obj.config;
483+
const jsonConfig = JSON.stringify(obj.config, undefined, 4);
484+
$(".editorText").val(jsonConfig);
485+
if (editor) {
486+
editor.setValue(jsonConfig);
487+
}
488+
justStarted = false;
489+
break;
471490
case "ServerInfo":
472491
for (index in obj.servers) {
473492
var server = obj.servers[index];
474493
let serverName = server.name;
475494
addDropdownNoDupe(serverName, !server.active);
476495
if (justStarted) {
477-
window.config = obj.config;
478-
const jsonConfig = JSON.stringify(obj.config, undefined, 4);
479-
$(".editorText").val(jsonConfig);
480-
if (editor) {
481-
editor.setValue(jsonConfig);
482-
}
483-
justStarted = false;
496+
// Do not update config here, wait for ConfigInfo
484497
var lines = server.output.split("\r\n");
485498
for (linePos in lines) {
486499
var line = lines[linePos];
@@ -490,15 +503,6 @@ $(document).ready(function () {
490503
p.innerHTML = line;
491504
}
492505
}
493-
if (JSON.stringify(window.config) != JSON.stringify(obj.config)) {
494-
//the config is out of sync, copy it to the config textarea
495-
const jsonConfig = JSON.stringify(obj.config, undefined, 4);
496-
$(".editorText").val(jsonConfig);
497-
if (editor) {
498-
editor.setValue(jsonConfig);
499-
}
500-
window.config = obj.config;
501-
}
502506
}
503507
window.serverInfoObj = obj;
504508
break;
@@ -598,6 +602,9 @@ $(document).ready(function () {
598602

599603
// Server will send theme list after connection
600604

605+
// Request config at startup (only once)
606+
sendRequestConfig();
607+
601608
// if (typeof InstallTrigger !== 'undefined') { // Check if Firefox
602609
// document.getElementById('scrollable-div').classList.add('scrollbar'); // broke firefox
603610
// }

src/messages.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ pub struct StdinInput {
3939
pub value: String,
4040
}
4141

42+
#[derive(Clone, Serialize, Deserialize)]
43+
pub struct ConfigInfo {
44+
pub r#type: String,
45+
pub config: crate::configuration::Config,
46+
}
47+
4248
#[derive(Clone, Serialize, Deserialize)]
4349
pub struct GetThemesList {
4450
pub r#type: String, // Should be "getThemesList"

src/websocket.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ async fn process_message(text: String, state: AppState) {
9191
}
9292
}
9393
match ev_type {
94+
"requestConfig" => {
95+
let config = state.config.lock().await;
96+
let config_info = crate::messages::ConfigInfo {
97+
r#type: "ConfigInfo".to_owned(),
98+
config: config.clone(),
99+
};
100+
let _ = state.tx.send(serde_json::to_string(&config_info).unwrap());
101+
}
94102
"requestInfo" => {
95103
let servers = state.servers.lock().await;
96104
let val: SInfoRequestMessage = serde_json::from_str(&text).unwrap();
@@ -373,6 +381,12 @@ async fn process_message(text: String, state: AppState) {
373381
servers.push(desc_clone.into_instance());
374382
}
375383
}
384+
// Broadcast ConfigInfo to all clients
385+
let config_info = crate::messages::ConfigInfo {
386+
r#type: "ConfigInfo".to_owned(),
387+
config: config.clone(),
388+
};
389+
let _ = state.tx.send(serde_json::to_string(&config_info).unwrap());
376390
drop(config);
377391
drop(servers)
378392
}

0 commit comments

Comments
 (0)