use std::collections::BTreeMap;
use crate::state::{ChannelId, ImState};
use helix_core::Correlation;
pub(crate) struct ImWsContext<'a> {
pub(crate) state: &'a mut ImState,
pub(crate) now_ms: u64,
pub(crate) api_base_url: &'a str,
pub(crate) auth_user_id: &'a str,
pub(crate) scoped_channel_ids: Option<&'a [ChannelId]>,
alloc_corr: &'a mut dyn FnMut() -> Correlation,
attached_timeline_refreshes: BTreeMap<ChannelId, Option<String>>,
}
impl<'a> ImWsContext<'a> {
pub(crate) fn new(
state: &'a mut ImState,
now_ms: u64,
api_base_url: &'a str,
auth_user_id: &'a str,
alloc_corr: &'a mut dyn FnMut() -> Correlation,
) -> Self {
Self {
state,
now_ms,
api_base_url,
auth_user_id,
scoped_channel_ids: None,
alloc_corr,
attached_timeline_refreshes: BTreeMap::new(),
}
}
pub(crate) fn set_scoped_channel_ids(&mut self, channel_ids: &'a [ChannelId]) {
self.scoped_channel_ids = Some(channel_ids);
}
pub(crate) fn alloc_corr(&mut self) -> Correlation {
(self.alloc_corr)()
}
pub(crate) fn request_attached_timeline_refresh(&mut self, channel_id: ChannelId) {
self.attached_timeline_refreshes
.entry(channel_id)
.or_insert(None);
}
pub(crate) fn request_attached_timeline_refresh_with_causation(
&mut self,
channel_id: ChannelId,
causation_id: Option<String>,
) {
let entry = self
.attached_timeline_refreshes
.entry(channel_id)
.or_insert(None);
if causation_id.is_some() {
*entry = causation_id;
}
}
pub(crate) fn take_attached_timeline_refreshes(
&mut self,
) -> BTreeMap<ChannelId, Option<String>> {
std::mem::take(&mut self.attached_timeline_refreshes)
}
pub(crate) fn split_state_alloc(
&mut self,
) -> (&mut ImState, &mut (dyn FnMut() -> Correlation + 'a)) {
(self.state, self.alloc_corr)
}
}