chore(sdk): Move code inside the same module.

This commit is contained in:
Ivan Enderlin
2024-05-22 10:24:36 +02:00
parent ea6e15086e
commit b4b2ee4716
2 changed files with 46 additions and 47 deletions

View File

@@ -62,9 +62,8 @@ impl<Item, Gap> AsVector<Item, Gap> {
Item: Clone,
{
let mut updates = self.updates.write().unwrap();
let updates = updates.take_with_token(self.token);
self.mapper.map(updates)
self.mapper.map(updates.take_with_token(self.token))
}
}

View File

@@ -123,6 +123,51 @@ pub struct Updates<Item, Gap> {
pub(super) inner: Arc<RwLock<UpdatesInner<Item, Gap>>>,
}
impl<Item, Gap> Updates<Item, Gap> {
/// Create a new [`Self`].
pub(super) fn new() -> Self {
Self { inner: Arc::new(RwLock::new(UpdatesInner::new())) }
}
/// Push a new update.
pub(super) fn push(&mut self, update: Update<Item, Gap>) {
self.inner.write().unwrap().push(update);
}
/// Take new updates.
///
/// Updates that have been taken will not be read again.
pub(super) fn take(&mut self) -> Vec<Update<Item, Gap>>
where
Item: Clone,
Gap: Clone,
{
self.inner.write().unwrap().take().to_owned()
}
/// Subscribe to updates by using a [`Stream`].
pub(super) fn subscribe(&mut self) -> UpdatesSubscriber<Item, Gap> {
// A subscriber is a new update reader, it needs its own token.
let token = self.new_reader_token();
UpdatesSubscriber::new(Arc::downgrade(&self.inner), token)
}
/// Generate a new [`ReaderToken`].
pub(super) fn new_reader_token(&mut self) -> ReaderToken {
let mut inner = self.inner.write().unwrap();
// Add 1 before reading the `last_token`, in this particular order, because the
// 0 token is reserved by `MAIN_READER_TOKEN`.
inner.last_token += 1;
let last_token = inner.last_token;
inner.last_index_per_reader.insert(last_token, 0);
last_token
}
}
/// A token used to represent readers that read the updates in
/// [`UpdatesInner`].
pub(super) type ReaderToken = usize;
@@ -249,51 +294,6 @@ impl<Item, Gap> UpdatesInner<Item, Gap> {
}
}
impl<Item, Gap> Updates<Item, Gap> {
/// Create a new [`Self`].
pub(super) fn new() -> Self {
Self { inner: Arc::new(RwLock::new(UpdatesInner::new())) }
}
/// Push a new update.
pub(super) fn push(&mut self, update: Update<Item, Gap>) {
self.inner.write().unwrap().push(update);
}
/// Take new updates.
///
/// Updates that have been taken will not be read again.
pub(super) fn take(&mut self) -> Vec<Update<Item, Gap>>
where
Item: Clone,
Gap: Clone,
{
self.inner.write().unwrap().take().to_owned()
}
/// Subscribe to updates by using a [`Stream`].
pub(super) fn subscribe(&mut self) -> UpdatesSubscriber<Item, Gap> {
// A subscriber is a new update reader, it needs its own token.
let token = self.new_reader_token();
UpdatesSubscriber::new(Arc::downgrade(&self.inner), token)
}
/// Generate a new [`ReaderToken`].
pub(super) fn new_reader_token(&mut self) -> ReaderToken {
let mut inner = self.inner.write().unwrap();
// Add 1 before reading the `last_token`, in this particular order, because the
// 0 token is reserved by `MAIN_READER_TOKEN`.
inner.last_token += 1;
let last_token = inner.last_token;
inner.last_index_per_reader.insert(last_token, 0);
last_token
}
}
/// A subscriber to [`Updates`]. It is helpful to receive updates via a
/// [`Stream`].
pub(super) struct UpdatesSubscriber<Item, Gap> {