Skip to content

Commit

Permalink
Fix error when initializing quest. Improve time to show initial state
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Sep 11, 2024
1 parent c065315 commit 500a974
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ We have prebuilt binaries for MacOS (x86-64 and ARM64), Linux (x86-64), and Wind

#### Windows

1. Download the installer (either `.msi` or `.exe) for your platform (arm64 for ARM chips, x64 otherwise).
1. Download the installer (either `.msi` or `.exe`) for your platform (arm64 for ARM chips, x64 otherwise).
2. Run the installer.
3. Launch RepoQuest, e.g. by searching for "RepoQuest" in your applications list.

Expand Down
22 changes: 17 additions & 5 deletions js/packages/repo-quest/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ let LoaderEntry = () => {
<Await promise={promise}>
{quest_res =>
quest_res.status === "ok" ? (
<QuestView quest={quest_res.data} />
<QuestView
quest={quest_res.data[0]}
initialState={quest_res.data[1]}
/>
) : (
<InitForm />
)
Expand Down Expand Up @@ -163,7 +166,10 @@ let InitForm = () => {
<Await promise={commands.loadQuest(selected.dir)}>
{quest_res =>
quest_res.status === "ok" ? (
<QuestView quest={quest_res.data} />
<QuestView
quest={quest_res.data[0]}
initialState={quest_res.data[1]}
/>
) : (
<ErrorView action="Creating new quest" message={quest_res.error} />
)
Expand Down Expand Up @@ -229,7 +235,10 @@ let NewQuest = () => {
<Await promise={commands.newQuest(dir!, quest!)}>
{quest_res =>
quest_res.status === "ok" ? (
<QuestView quest={quest_res.data} />
<QuestView
quest={quest_res.data[0]}
initialState={quest_res.data[1]}
/>
) : (
<ErrorView action="Creating new quest" message={quest_res.error} />
)
Expand All @@ -238,9 +247,12 @@ let NewQuest = () => {
);
};

let QuestView: React.FC<{ quest: QuestConfig }> = ({ quest }) => {
let QuestView: React.FC<{
quest: QuestConfig;
initialState: StateDescriptor;
}> = ({ quest, initialState }) => {
let loader = useContext(Loader.context)!;
let [state, setState] = useState<StateDescriptor | undefined>(undefined);
let [state, setState] = useState<StateDescriptor | undefined>(initialState);
let setTitle = useContext(TitleContext)!;
useEffect(() => setTitle(quest.title), [quest.title]);

Expand Down
10 changes: 6 additions & 4 deletions rs/crates/repo-quest/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ impl GithubRepo {
}
}

pub async fn fetch(&self) -> Result<()> {
/// Returns true if repo
pub async fn fetch(&self) -> Result<bool> {
let (pr_handler, issue_handler) = (self.pr_handler(), self.issue_handler());
let res = try_join!(
pr_handler.list().state(octocrab::params::State::All).send(),
Expand All @@ -69,7 +70,7 @@ impl GithubRepo {
..
},
..
}) => return Ok(()),
}) => return Ok(false),
Err(e) => return Err(e.into()),
};
let (prs, mut issues) = (pr_page.take_items(), issue_page.take_items());
Expand All @@ -79,7 +80,8 @@ impl GithubRepo {

*self.prs.lock() = Some(prs);
*self.issues.lock() = Some(issues);
Ok(())

Ok(true)
}

pub fn remote(&self) -> String {
Expand Down Expand Up @@ -236,7 +238,7 @@ impl GithubRepo {
let is_reset = matches!(merge_type, MergeType::HardReset);
if is_reset {
body.push_str(r#"
Note: due to a merge conflict, this PR is a hard reset to the reference solution, and may have overwritten your previous changes."#);
}

Expand Down
21 changes: 15 additions & 6 deletions rs/crates/repo-quest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{env, path::PathBuf, sync::Arc};

use self::quest::{Quest, QuestConfig};
use github::GithubToken;
use quest::StateEvent;
use quest::{StateDescriptor, StateEvent};
use tauri::{AppHandle, Manager, State};
use tauri_specta::collect_events;

Expand Down Expand Up @@ -58,19 +58,28 @@ async fn load_quest_core(

#[tauri::command]
#[specta::specta]
async fn load_quest(dir: PathBuf, app: AppHandle) -> Result<QuestConfig, String> {
async fn load_quest(
dir: PathBuf,
app: AppHandle,
) -> Result<(QuestConfig, StateDescriptor), String> {
let config = fmt_err(QuestConfig::load(&dir))?;
load_quest_core(dir, &config, app).await?;
Ok(config)
let quest = load_quest_core(dir, &config, app).await?;
let state = fmt_err(quest.state_descriptor().await)?;
Ok((config, state))
}

#[tauri::command]
#[specta::specta]
async fn new_quest(dir: PathBuf, quest: String, app: AppHandle) -> Result<QuestConfig, String> {
async fn new_quest(
dir: PathBuf,
quest: String,
app: AppHandle,
) -> Result<(QuestConfig, StateDescriptor), String> {
let config = fmt_err(quest::load_config_from_remote("cognitive-engineering-lab", &quest).await)?;
let quest = load_quest_core(dir.join(quest), &config, app).await?;
fmt_err(quest.create_repo().await)?;
Ok(config)
let state = fmt_err(quest.state_descriptor().await)?;
Ok((config, state))
}

#[tauri::command]
Expand Down
38 changes: 28 additions & 10 deletions rs/crates/repo-quest/src/quest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
env::{self, set_current_dir},
path::{Path, PathBuf},
process::Command,
sync::atomic::{AtomicBool, Ordering},
time::Duration,
};

Expand Down Expand Up @@ -77,6 +78,7 @@ pub struct Quest {
upstream: GithubRepo,
origin: GithubRepo,
origin_git: GitRepo,
origin_exists: AtomicBool,
stage_index: HashMap<String, usize>,
dir: PathBuf,
app: Option<AppHandle>,
Expand Down Expand Up @@ -129,20 +131,21 @@ impl Quest {
.map(|(i, stage)| (stage.label.clone(), i))
.collect::<HashMap<_, _>>();

let (origin_exists, _) = try_join!(origin.fetch(), upstream.fetch())?;
let origin_exists = AtomicBool::new(origin_exists);

let q = Quest {
dir,
user,
config,
upstream,
origin,
origin_git,
origin_exists,
stage_index,
app,
};

try_join!(q.origin.fetch(), q.upstream.fetch())?;

// Need to infer_state_update after fetching repo data so issues/PRs are populated
q.infer_state_update().await?;

if q.dir.exists() {
Expand Down Expand Up @@ -297,15 +300,26 @@ impl Quest {
})
}

pub async fn state_descriptor(&self) -> Result<StateDescriptor> {
let state = self.infer_state().await?;
Ok(StateDescriptor {
dir: self.dir.clone(),
stages: self.stage_states(),
state,
})
}

pub async fn infer_state_update(&self) -> Result<()> {
let (new_state, _) = try_join!(self.infer_state(), self.origin.fetch())?;
// If the repo is not yet created, then there's nothing to fetch.
// This also prevents panics wrt trying to access issues/PRs on origin
if !self.origin_exists.load(Ordering::SeqCst) {
return Ok(());
}

self.origin.fetch().await?;
let state = self.state_descriptor().await?;
if let Some(app) = &self.app {
let descriptor = StateDescriptor {
dir: self.dir.clone(),
stages: self.stage_states(),
state: new_state,
};
StateEvent(descriptor).emit(app)?;
StateEvent(state).emit(app)?;
}

Ok(())
Expand Down Expand Up @@ -336,6 +350,10 @@ impl Quest {
// Initialize the upstreams and fetch content
self.origin_git.setup_upstream(&self.upstream)?;

self.origin_exists.store(true, Ordering::SeqCst);

self.infer_state_update().await?;

Ok(())
}

Expand Down

0 comments on commit 500a974

Please sign in to comment.