Skip to content

Commit

Permalink
refactor mod worker, strengthening the abstraction boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
gulbanana committed Mar 18, 2024
1 parent 57a0e50 commit 000b0ad
Show file tree
Hide file tree
Showing 8 changed files with 362 additions and 354 deletions.
2 changes: 1 addition & 1 deletion src-tauri/src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use anyhow::Result;
use jj_lib::{git::RemoteCallbacks, repo::MutableRepo};
use tauri::{Manager, WebviewWindow};

use crate::{gui_util::WorkerCallbacks, messages::InputRequest, AppState};
use crate::{messages::InputRequest, worker::WorkerCallbacks, AppState};

pub struct FrontendCallbacks(pub WebviewWindow);

Expand Down
6 changes: 1 addition & 5 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

mod callbacks;
mod config;
mod gui_util;
mod handler;
mod menu;
mod messages;
#[cfg(all(test, not(feature = "ts-rs")))]
mod tests;
mod worker;

use std::collections::HashMap;
Expand All @@ -23,14 +20,13 @@ use tauri::{ipc::InvokeError, Manager};
use tauri::{State, WebviewWindow, Window, WindowEvent, Wry};
use tauri_plugin_window_state::StateFlags;

use gui_util::WorkerSession;
use messages::{
AbandonRevisions, CheckoutRevision, CopyChanges, CreateRevision, DescribeRevision,
DuplicateRevisions, FetchRemote, InputResponse, InsertRevision, MoveBranch, MoveChanges,
MoveRevision, MoveSource, MutationResult, PushRemote, RevId, TrackBranch, UndoOperation,
UntrackBranch,
};
use worker::{Mutation, Session, SessionEvent};
use worker::{Mutation, Session, SessionEvent, WorkerSession};

use crate::callbacks::FrontendCallbacks;

Expand Down
43 changes: 3 additions & 40 deletions src-tauri/src/gui_util.rs → src-tauri/src/worker/gui_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use jj_cli::{
config::LayeredConfigs,
git_util::is_colocated_git_workspace,
};
use jj_lib::{backend::BackendError, default_index::{AsCompositeIndex, DefaultReadonlyIndex}, file_util::relative_path, git::RemoteCallbacks, gitignore::GitIgnoreFile, op_store::WorkspaceId, repo::{MutableRepo, RepoLoaderError}, repo_path::RepoPath, revset::{RevsetEvaluationError, RevsetIteratorExt, RevsetResolutionError}, rewrite, view::View, working_copy::{CheckoutStats, SnapshotOptions}};
use jj_lib::{backend::BackendError, default_index::{AsCompositeIndex, DefaultReadonlyIndex}, file_util::relative_path, gitignore::GitIgnoreFile, op_store::WorkspaceId, repo::RepoLoaderError, repo_path::RepoPath, revset::{RevsetEvaluationError, RevsetIteratorExt, RevsetResolutionError}, rewrite, view::View, working_copy::{CheckoutStats, SnapshotOptions}};
use jj_lib::{
backend::{ChangeId, CommitId},
commit::Commit,
Expand All @@ -35,44 +35,7 @@ use jj_lib::{
use thiserror::Error;

use crate::{config::GGSettings, messages::{self, RevId}};

pub trait WorkerCallbacks {
fn with_git(&self, repo: &mut MutableRepo, f: &dyn Fn(&mut MutableRepo, RemoteCallbacks<'_>) -> Result<()>) -> Result<()>;
}

struct NoCallbacks;

impl WorkerCallbacks for NoCallbacks {
fn with_git(&self, repo: &mut MutableRepo, f: &dyn Fn(&mut MutableRepo, RemoteCallbacks<'_>) -> Result<()>) -> Result<()> {
f(repo, RemoteCallbacks::default())
}
}

/// state that doesn't depend on jj-lib borrowings
pub struct WorkerSession {
pub force_log_page_size: Option<usize>,
pub latest_query: Option<String>,
pub callbacks: Box<dyn WorkerCallbacks>
}

impl WorkerSession {
pub fn new<T: WorkerCallbacks + 'static>(callbacks: T) -> Self {
WorkerSession {
callbacks: Box::new(callbacks),
..Default::default()
}
}
}

impl Default for WorkerSession {
fn default() -> Self {
WorkerSession {
force_log_page_size: None,
latest_query: None,
callbacks: Box::new(NoCallbacks)
}
}
}
use super::WorkerSession;

/// jj-dependent state, available when a workspace is open
pub struct WorkspaceSession<'a> {
Expand Down Expand Up @@ -120,7 +83,7 @@ impl WorkerSession {

let defaults = Config::builder()
.add_source(jj_cli::config::default_config())
.add_source(config::File::from_str(include_str!("config/gg.toml"), config::FileFormat::Toml))
.add_source(config::File::from_str(include_str!("../config/gg.toml"), config::FileFormat::Toml))
.build()?;

let mut configs = LayeredConfigs::from_environment(defaults);
Expand Down
Loading

0 comments on commit 000b0ad

Please sign in to comment.