mirror of
https://github.com/loro-dev/loro.git
synced 2025-02-02 11:06:14 +00:00
4414053a82
* chore: init ffi * feat: impl doc and LoroList * feat: impl containers * feat: unknown container * feat: event ffi * chore: clean * feat: ffi undo manager * chore: cargo fix * chore: cargo fix * fix: ffi value or container * fix: ffi arc * fix: is attached for movable list * bk * feat: all LoroDoc func * feat: refine vv * feat: ffi frontiers * feat: ffi awareness * fix: merge
83 lines
2 KiB
Rust
83 lines
2 KiB
Rust
use std::{
|
|
collections::HashMap,
|
|
sync::{Arc, Mutex},
|
|
};
|
|
|
|
use loro::PeerID;
|
|
|
|
use crate::{LoroValue, LoroValueLike};
|
|
pub struct Awareness(Mutex<loro::awareness::Awareness>);
|
|
|
|
impl Awareness {
|
|
pub fn new(peer: PeerID, timeout: i64) -> Self {
|
|
Self(Mutex::new(loro::awareness::Awareness::new(peer, timeout)))
|
|
}
|
|
|
|
pub fn encode(&self, peers: &[PeerID]) -> Vec<u8> {
|
|
self.0.try_lock().unwrap().encode(peers)
|
|
}
|
|
|
|
pub fn encode_all(&self) -> Vec<u8> {
|
|
self.0.try_lock().unwrap().encode_all()
|
|
}
|
|
|
|
pub fn apply(&self, encoded_peers_info: &[u8]) -> AwarenessPeerUpdate {
|
|
let (updated, added) = self.0.try_lock().unwrap().apply(encoded_peers_info);
|
|
AwarenessPeerUpdate { updated, added }
|
|
}
|
|
|
|
pub fn set_local_state(&self, value: Arc<dyn LoroValueLike>) {
|
|
self.0
|
|
.try_lock()
|
|
.unwrap()
|
|
.set_local_state(value.as_loro_value());
|
|
}
|
|
|
|
pub fn get_local_state(&self) -> Option<LoroValue> {
|
|
self.0
|
|
.try_lock()
|
|
.unwrap()
|
|
.get_local_state()
|
|
.map(|x| x.into())
|
|
}
|
|
|
|
pub fn remove_outdated(&self) -> Vec<PeerID> {
|
|
self.0.try_lock().unwrap().remove_outdated()
|
|
}
|
|
|
|
pub fn get_all_states(&self) -> HashMap<PeerID, PeerInfo> {
|
|
self.0
|
|
.try_lock()
|
|
.unwrap()
|
|
.get_all_states()
|
|
.iter()
|
|
.map(|(p, i)| (*p, i.into()))
|
|
.collect()
|
|
}
|
|
|
|
pub fn peer(&self) -> PeerID {
|
|
self.0.try_lock().unwrap().peer()
|
|
}
|
|
}
|
|
|
|
pub struct AwarenessPeerUpdate {
|
|
pub updated: Vec<PeerID>,
|
|
pub added: Vec<PeerID>,
|
|
}
|
|
|
|
pub struct PeerInfo {
|
|
pub state: LoroValue,
|
|
pub counter: i32,
|
|
// This field is generated locally
|
|
pub timestamp: i64,
|
|
}
|
|
|
|
impl From<&loro::awareness::PeerInfo> for PeerInfo {
|
|
fn from(value: &loro::awareness::PeerInfo) -> Self {
|
|
PeerInfo {
|
|
state: value.state.clone().into(),
|
|
counter: value.counter,
|
|
timestamp: value.timestamp,
|
|
}
|
|
}
|
|
}
|