1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! Attaching to a session (`rho/session/attach`): following its events,
//! loading it first if the daemon has not, and continuing a turn the
//! restart that ran this daemon interrupted.
use std::sync::Arc;
use rho_protocol::{RpcError, SessionAttachAnswer, SessionAttachQuery, Subscription, Surfaces};
use super::{
attach_response, grant, host_prompt, open::OpenWith, reopen::FirstTurn, restart_resume, setup,
turn::TurnTask, ResumeFrom, Sessions,
};
impl Sessions {
/// Subscribes a client to a session's events, resuming after its cursor
/// when the session's feed still can, and continues the session's turn
/// if the restart that ran this daemon interrupted it.
pub(in crate::app::daemon_host) async fn attach(
&self,
query: SessionAttachQuery,
) -> Result<(SessionAttachAnswer, Subscription), RpcError> {
self.refuse_when_stopping()?;
let SessionAttachQuery {
params,
after_entry,
window,
defer_tool_output,
..
} = query;
// Taken under the turn lock a prompt starts its turn under, so an
// attach never sees the last turn's end once the job list says the
// next turn runs, and the idle unload never takes a session a client
// just attached to.
// Applied only if this attach reopens the session: one the daemon
// has loaded keeps the environment and setup it opened with.
// Its grant is its environment's: an attach names none.
let draws = params.setup.surfaces;
// Applied to a loaded session too, from its next turn.
let prompt = params
.setup
.system_prompt
.as_ref()
.map(setup::host_prompt)
.transpose()?;
let with = OpenWith::under(
grant::asked(/*named*/ None, ¶ms.environment)?,
¶ms.environment,
params.setup,
)?;
// A turn the restart interrupted continues first, so the client
// attaches to the turn that continues it.
let (session, (resumed, attached)) = self
.enter_session(
¶ms.session,
&with,
FirstTurn::Daemon,
|session, turn| {
let resumed = self.resume_interrupted(session, turn);
// Read from every attach, not only one that opens the
// session: a `ui` call waits for the user only while a
// client that draws surfaces follows the session.
let viewer = match draws {
Surfaces::Unsupported => None,
Surfaces::Drawn => session
.background
.surfaces()
.map(|surfaces| surfaces.viewer()),
};
Ok((
resumed,
session.feed.attach_viewer(
ResumeFrom {
after: params.after.as_ref(),
entry: after_entry.as_ref(),
window,
deferral: defer_tool_output,
},
viewer,
),
))
},
)
.await?;
if let Some(prompt) = prompt {
host_prompt::apply_when_free(&session, prompt, self.stopping.clone());
}
if let Some(turn) = resumed {
// Spawned before anything awaits, as a prompt's turn is.
TurnTask {
session: Arc::clone(&session),
turn,
stopping: self.stopping.clone(),
pull_requests: self.pull_requests.service().clone(),
}
.spawn_daemon(restart_resume::resume_turn());
}
Ok(attach_response(&session.workspace, attached))
}
}