Skip to content

Commit

Permalink
Fix options not being applied on extension start-up.
Browse files Browse the repository at this point in the history
Also cleanup some messy code.
  • Loading branch information
null-dev committed May 31, 2021
1 parent 543eace commit 74963bc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 40 deletions.
15 changes: 8 additions & 7 deletions src/cmd/initialize.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::state::AppState;
use crate::profiles::{ProfilesIniState, write_profiles};
use crate::native_req::NativeMessageInitialize;
use crate::native_resp::{NativeResponse, NativeResponseData, write_native_response, NativeResponseWrapper, NATIVE_RESP_ID_EVENT, NativeResponseEvent, NativeResponseProfileListProfileEntry};
use crate::native_resp::{NativeResponse, NativeResponseData, write_native_response, NativeResponseWrapper, NATIVE_RESP_ID_EVENT, NativeResponseEvent, NativeResponseProfileListProfileEntry, write_native_event};
use std::{thread, fs};
use crate::ipc::setup_ipc;
use crate::options::native_notify_updated_options;

pub fn process_cmd_initialize(app_state: &mut AppState,
profiles: &mut ProfilesIniState,
Expand Down Expand Up @@ -69,14 +70,14 @@ fn finish_init(app_state: &mut AppState, profiles: &mut ProfilesIniState, profil
}

// Notify extension of new profile list
write_native_response(NativeResponseWrapper {
id: NATIVE_RESP_ID_EVENT,
resp: NativeResponse::event(NativeResponseEvent::ProfileList {
current_profile_id: profile_id.to_owned(),
profiles: profiles.profile_entries.iter().map(NativeResponseProfileListProfileEntry::from_profile_entry).collect()
})
write_native_event(NativeResponseEvent::ProfileList {
current_profile_id: profile_id.to_owned(),
profiles: profiles.profile_entries.iter().map(NativeResponseProfileListProfileEntry::from_profile_entry).collect()
});

// Notify extension of current options
native_notify_updated_options(app_state);

// Begin IPC
{
let profile_id = profile_id.to_owned();
Expand Down
35 changes: 9 additions & 26 deletions src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::state::AppState;
use byteorder::{WriteBytesExt, NetworkEndian, ReadBytesExt};
use std::io::{Write, Read, BufReader, BufWriter};
use std::path::PathBuf;
use crate::native_resp::{write_native_response, NativeResponseWrapper, NATIVE_RESP_ID_EVENT, NativeResponse, NativeResponseEvent, NativeResponseProfileListProfileEntry};
use crate::native_resp::{write_native_response, NativeResponseWrapper, NATIVE_RESP_ID_EVENT, NativeResponse, NativeResponseEvent, NativeResponseProfileListProfileEntry, write_native_event};
use crate::profiles::{read_profiles, ProfilesIniState};
use crate::options::read_global_options;
use crate::options::{read_global_options, native_notify_updated_options};
use crate::storage::options_data_path;
use cfg_if::cfg_if;
use serde::{Serialize, Deserialize};
Expand Down Expand Up @@ -108,11 +108,8 @@ fn handle_ipc_cmd(app_state: &AppState, cmd: IPCCommand) {
match cmd {
IPCCommand::FocusWindow(options) => {
// Focus window
write_native_response(NativeResponseWrapper {
id: NATIVE_RESP_ID_EVENT,
resp: NativeResponse::event(NativeResponseEvent::FocusWindow {
url: options.url
})
write_native_event(NativeResponseEvent::FocusWindow {
url: options.url
});
}
IPCCommand::UpdateProfileList => {
Expand All @@ -122,12 +119,9 @@ fn handle_ipc_cmd(app_state: &AppState, cmd: IPCCommand) {
.as_ref()
.map(|it| it.clone()) {
// Notify updated profile list
write_native_response(NativeResponseWrapper {
id: NATIVE_RESP_ID_EVENT,
resp: NativeResponse::event(NativeResponseEvent::ProfileList {
current_profile_id: pid.to_owned(),
profiles: profiles.profile_entries.iter().map(NativeResponseProfileListProfileEntry::from_profile_entry).collect()
})
write_native_event(NativeResponseEvent::ProfileList {
current_profile_id: pid.to_owned(),
profiles: profiles.profile_entries.iter().map(NativeResponseProfileListProfileEntry::from_profile_entry).collect()
});
}
},
Expand All @@ -137,21 +131,10 @@ fn handle_ipc_cmd(app_state: &AppState, cmd: IPCCommand) {
};
}
IPCCommand::CloseManager => {
write_native_response(NativeResponseWrapper {
id: NATIVE_RESP_ID_EVENT,
resp: NativeResponse::event(NativeResponseEvent::CloseManager)
});
write_native_event(NativeResponseEvent::CloseManager);
}
IPCCommand::UpdateOptions => {
let new_options = read_global_options(
&options_data_path(&app_state.config_dir));

write_native_response(NativeResponseWrapper {
id: NATIVE_RESP_ID_EVENT,
resp: NativeResponse::event(NativeResponseEvent::OptionsUpdated {
options: new_options
})
})
native_notify_updated_options(app_state);
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use rand::Rng;
use crate::config::{read_configuration};
use crate::profiles::read_profiles;
use crate::state::AppState;
use crate::native_resp::{NativeResponseEvent, write_native_response, NativeResponseWrapper, NATIVE_RESP_ID_EVENT, NativeResponse};
use crate::native_resp::{NativeResponseEvent, write_native_response, NativeResponseWrapper, NATIVE_RESP_ID_EVENT, NativeResponse, write_native_event};
use crate::cmd::execute_cmd_for_message;
use crate::native_req::{read_incoming_message, NativeMessage};

Expand All @@ -58,11 +58,8 @@ mod state {

fn main() {
// Notify extension of our version
write_native_response(NativeResponseWrapper {
id: NATIVE_RESP_ID_EVENT,
resp: NativeResponse::event(NativeResponseEvent::ConnectorInformation {
version: APP_VERSION.to_string()
})
write_native_event(NativeResponseEvent::ConnectorInformation {
version: APP_VERSION.to_string()
});

// Calculate storage dirs
Expand Down
14 changes: 13 additions & 1 deletion src/native_resp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ pub struct NativeResponseWrapper {
pub resp: NativeResponse
}

impl NativeResponseWrapper {
fn event(event: NativeResponseEvent) -> NativeResponseWrapper {
NativeResponseWrapper {
id: NATIVE_RESP_ID_EVENT,
resp: NativeResponse::event(event)
}
}
}

impl NativeResponse {
pub fn error(msg: &str) -> NativeResponse {
NativeResponse::Error {
Expand Down Expand Up @@ -61,7 +70,7 @@ impl NativeResponse {
data
}
}
pub fn event(event: NativeResponseEvent) -> NativeResponse {
fn event(event: NativeResponseEvent) -> NativeResponse {
NativeResponse::Event(event)
}
}
Expand Down Expand Up @@ -127,3 +136,6 @@ pub fn write_native_response(resp: NativeResponseWrapper) {
handle.flush();
}

pub fn write_native_event(resp: NativeResponseEvent) {
write_native_response(NativeResponseWrapper::event(resp));
}
12 changes: 12 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use std::collections::HashMap;
use serde_json::Value;
use std::fs::OpenOptions;
use std::io;
use crate::storage::options_data_path;
use crate::native_resp::{write_native_response, NativeResponseWrapper, write_native_event, NativeResponseEvent};
use crate::state::AppState;

// === GLOBAL OPTIONS ===

Expand Down Expand Up @@ -36,3 +39,12 @@ pub fn write_global_options(path: &PathBuf, new_options: &HashMap<String, Value>
serde_json::to_writer(options_file, &new_options)
.map_err(WriteGlobalOptionsError::WriteFileError)
}

pub fn native_notify_updated_options(app_state: &AppState) {
let new_options = read_global_options(
&options_data_path(&app_state.config_dir));

write_native_event(NativeResponseEvent::OptionsUpdated {
options: new_options
});
}

0 comments on commit 74963bc

Please sign in to comment.