1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

factor out helper

This commit is contained in:
Bartek Iwańczuk 2022-12-13 19:38:16 +01:00
parent 5cd7b9fc6e
commit 5f6dd678de
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750

View file

@ -172,16 +172,51 @@ fn save_session_to_file(
session_history: &[String], session_history: &[String],
maybe_filename: Option<String>, maybe_filename: Option<String>,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
// TODO(bartlomieju): make date shorter
let filename = maybe_filename.unwrap_or_else(|| { let filename = maybe_filename.unwrap_or_else(|| {
format!("./repl-{}.ts", chrono::Local::now().to_rfc3339()) format!(
"./repl-{}.ts",
chrono::Local::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
)
}); });
std::fs::write(&filename, session_history.join("\n")) std::fs::write(&filename, session_history.join("\n"))
.context("Unable to save session file")?; .context("Unable to save session file")?;
println!("Saved session to {}", filename); println!("Saved session to \"{}\".", filename);
Ok(()) Ok(())
} }
async fn handle_repl_state(
ps: &ProcState,
repl_session: &mut ReplSession,
main_module: &ModuleSpecifier,
repl_flags: &ReplFlags,
session_history: &[String],
) -> Result<Option<ReplSession>, AnyError> {
let op_state = repl_session.worker.js_runtime.op_state();
let repl_state = {
let op_state = op_state.borrow();
op_state.borrow::<ReplState>().clone()
};
if repl_state.needs_reload {
drop(op_state);
let repl_session =
create_repl_session(ps.clone(), main_module.clone(), repl_flags.clone())
.await?;
println!("Started a new REPL session. Global state has been reset.");
Ok(Some(repl_session))
} else if repl_state.needs_save {
let mut op_state = op_state.borrow_mut();
op_state.put(ReplState {
needs_reload: false,
needs_save: false,
maybe_session_filename: None,
});
save_session_to_file(session_history, repl_state.maybe_session_filename)?;
Ok(None)
} else {
Ok(None)
}
}
pub async fn run(flags: Flags, repl_flags: ReplFlags) -> Result<i32, AnyError> { pub async fn run(flags: Flags, repl_flags: ReplFlags) -> Result<i32, AnyError> {
let main_module = resolve_url_or_path("./$deno$repl.ts").unwrap(); let main_module = resolve_url_or_path("./$deno$repl.ts").unwrap();
let ps = ProcState::build(flags).await?; let ps = ProcState::build(flags).await?;
@ -240,33 +275,16 @@ pub async fn run(flags: Flags, repl_flags: ReplFlags) -> Result<i32, AnyError> {
println!("{}", output); println!("{}", output);
{ let maybe_new_session = handle_repl_state(
let op_state = repl_session.worker.js_runtime.op_state(); &ps,
let repl_state = { &mut repl_session,
let op_state = op_state.borrow(); &main_module,
op_state.borrow::<ReplState>().clone() &repl_flags,
}; &session_history,
if repl_state.needs_reload {
drop(op_state);
repl_session = create_repl_session(
ps.clone(),
main_module.clone(),
repl_flags.clone(),
) )
.await?; .await?;
println!("Started a new REPL session. Global scope is now clean."); if let Some(new_session) = maybe_new_session {
} else if repl_state.needs_save { repl_session = new_session;
let mut op_state = op_state.borrow_mut();
op_state.put(ReplState {
needs_reload: false,
needs_save: false,
maybe_session_filename: None,
});
save_session_to_file(
&session_history,
repl_state.maybe_session_filename,
)?;
}
} }
} }
Err(ReadlineError::Interrupted) => { Err(ReadlineError::Interrupted) => {