Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing logical collection pointers #393

Merged
merged 30 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
cd15621
moved Branch, BranchPtr to branch.rs
Horusiath Feb 11, 2024
877499a
introduced RootRefs
Horusiath Feb 15, 2024
4ab1faf
exposed Root and Nested types
Horusiath Feb 16, 2024
73d2626
Working YArray on ywasm
Horusiath Feb 22, 2024
7afa7b9
ywasm: YDoc and YTransaction methods
Horusiath Feb 22, 2024
c1f1ce4
ywasm: working YArray
Horusiath Feb 24, 2024
fb8073c
ywasm: prepared UndoManager
Horusiath Feb 24, 2024
6c38c3d
ywasm: prepared WeakLink
Horusiath Feb 24, 2024
3e98907
ywasm: flattened YDoc
Horusiath Feb 24, 2024
d33f946
ywasm: make YDoc compilable through wasm-interpreter
Horusiath Feb 29, 2024
064444c
ywasm: support YWeakLink
Horusiath Feb 29, 2024
5d24059
ywasm: support YMap
Horusiath Feb 29, 2024
ddf8a8a
ywasm: support YText
Horusiath Feb 29, 2024
5b1f1d8
ywasm: support XML types
Horusiath Mar 2, 2024
d45b345
ywasm: support for snapshots and sticky indexes
Horusiath Mar 3, 2024
7d60a01
ywasm: first batch of fixes
Horusiath Mar 7, 2024
9054653
ywasm: second batch of fixes
Horusiath Mar 7, 2024
56c8963
ywasm: third batch of fixes
Horusiath Mar 8, 2024
7f137df
ywasm: fourth batch of fixes
Horusiath Mar 8, 2024
b07878e
ywasm: fixed remaining tests
Horusiath Mar 8, 2024
19cdcd8
exposed logical IDs in ywasm API
Horusiath Mar 8, 2024
9b70f79
added documentation
Horusiath Mar 8, 2024
e732534
fixed doc.rs example
Horusiath Mar 8, 2024
7d50569
updated yffi tests
Horusiath Mar 8, 2024
9ed4e5d
added BranchID to yffi
Horusiath Mar 8, 2024
80dbfba
yffi: fixed one test
Horusiath Mar 8, 2024
10e6f1c
yffi: fixed event target resolution
Horusiath Mar 8, 2024
a105298
wtf 1
Horusiath Mar 8, 2024
00f8e48
added test for logical branches
Horusiath Mar 9, 2024
10f3b6d
fixed c ffi tests
Horusiath Mar 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ywasm: working YArray
  • Loading branch information
Horusiath committed Feb 24, 2024
commit c1f1ce4eabe429764eda2ed59c8dc72ac30766be
1 change: 1 addition & 0 deletions tests-wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"description": "Test suite for Yrs Web Assembly package.",
"main": "index.js",
"scripts": {
"build": "wasm-pack build --target nodejs ../ywasm",
"test": "node --experimental-wasm-modules index.js"
},
"contributors": [
Expand Down
62 changes: 34 additions & 28 deletions yrs/src/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ impl<'a, T: ReadTxn> Iterator for Iter<'a, T> {
///
/// // logical descriptors of both TextRef are the same as they refer to the
/// // same logical entity
/// assert_eq!(txt1.desc(), txt2.desc());
/// assert_eq!(txt1.hook(), txt2.hook());
/// ```
#[repr(transparent)]
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
Expand Down Expand Up @@ -650,7 +650,7 @@ impl<S> Into<BranchID> for Root<S> {
/// let text = root.insert(&mut txn, "nested", TextPrelim::new("")); // nested collection
///
/// // convert nested TextRef into logical pointer
/// let nested: Nested<TextRef> = text.desc().into_nested().unwrap();
/// let nested: Nested<TextRef> = text.hook().into_nested().unwrap();
///
/// // logical reference can be used to retrieve accessible TextRef when its alive
/// assert_eq!(nested.get(&txn), Some(text));
Expand Down Expand Up @@ -706,60 +706,66 @@ impl<S> Into<BranchID> for Nested<S> {

/// A descriptor used to reference to shared collections by their unique logical identifiers,
/// which can be either [Root]-level collections or shared collections [Nested] into each other.
/// It can be resolved from any shared reference using [SharedRef::desc].
#[derive(Debug, Clone)]
pub struct Desc<S> {
/// It can be resolved from any shared reference using [SharedRef::hook].
#[derive(Clone)]
pub struct Hook<S> {
id: BranchID,
_tag: PhantomData<S>,
}

impl<S> Desc<S> {
impl<S> Hook<S> {
/// Unique logical identifier of a shared collection.
pub fn id(&self) -> &BranchID {
&self.id
}
}

impl<S> Eq for Desc<S> {}
impl<S> std::fmt::Debug for Hook<S> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self.id)
}
}

impl<S> Eq for Hook<S> {}

impl<S> PartialEq for Desc<S> {
impl<S> PartialEq for Hook<S> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}

impl<S> Hash for Desc<S> {
impl<S> Hash for Hook<S> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state)
}
}

impl<S: SharedRef> Desc<S> {
/// Returns a reference to a shared collection current descriptor points to, if it exists and
impl<S: SharedRef> Hook<S> {
/// Returns a reference to a shared collection current hook points to, if it exists and
/// (in case of nested collections) has not been deleted.
///
/// # Example
///
/// ```rust
/// use yrs::{Desc, Doc, Map, MapRef, Nested, SharedRef, TextPrelim, TextRef, Transact, WriteTxn};
/// use yrs::{Hook, Doc, Map, MapRef, Nested, SharedRef, TextPrelim, TextRef, Transact, WriteTxn};
///
/// let doc = Doc::new();
/// let mut txn = doc.transact_mut();
/// let root = txn.get_or_insert_map("root"); // root-level collection
/// let nested = root.insert(&mut txn, "nested", TextPrelim::new("")); // nested collection
///
/// let root_desc: Desc<MapRef> = root.desc();
/// let nested_desc: Desc<TextRef> = nested.desc();
/// let root_hook: Hook<MapRef> = root.hook();
/// let nested_hook: Hook<TextRef> = nested.hook();
///
/// // descriptor can be used to retrieve collection reference as long as its alive
/// assert_eq!(nested_desc.get(&txn), Some(nested));
/// // hook can be used to retrieve collection reference as long as its alive
/// assert_eq!(nested_hook.get(&txn), Some(nested));
///
/// // after nested collection is deleted it can no longer be referenced
/// root.remove(&mut txn, "nested");
/// assert_eq!(nested_desc.get(&txn), None, "wtf");
/// assert_eq!(nested_hook.get(&txn), None, "wtf");
///
/// // descriptors work also for root types
/// assert_eq!(root_desc.get(&txn), Some(root));
/// assert_eq!(root_hook.get(&txn), Some(root));
/// ```
pub fn get<T: ReadTxn>(&self, txn: &T) -> Option<S> {
let branch = self.id.get_branch(txn)?;
Expand All @@ -769,7 +775,7 @@ impl<S: SharedRef> Desc<S> {
}
}

/// Attempts to convert current [Desc] type into [Nested] one.
/// Attempts to convert current [Hook] type into [Nested] one.
/// Returns `None` if current descriptor doesn't reference a nested shared collection.
pub fn into_nested(self) -> Option<Nested<S>> {
match self.id {
Expand All @@ -779,8 +785,8 @@ impl<S: SharedRef> Desc<S> {
}
}

impl<S: RootRef> Desc<S> {
/// Attempts to convert current [Desc] type into [Root] one.
impl<S: RootRef> Hook<S> {
/// Attempts to convert current [Hook] type into [Root] one.
/// Returns `None` if current descriptor doesn't reference a root-level shared collection.
pub fn into_root(self) -> Option<Root<S>> {
match self.id {
Expand All @@ -790,34 +796,34 @@ impl<S: RootRef> Desc<S> {
}
}

impl<S> From<Root<S>> for Desc<S> {
impl<S> From<Root<S>> for Hook<S> {
fn from(root: Root<S>) -> Self {
Desc {
Hook {
id: root.into(),
_tag: PhantomData::default(),
}
}
}

impl<S> From<Nested<S>> for Desc<S> {
impl<S> From<Nested<S>> for Hook<S> {
fn from(nested: Nested<S>) -> Self {
Desc {
Hook {
id: nested.into(),
_tag: PhantomData::default(),
}
}
}

impl<S> From<BranchID> for Desc<S> {
impl<S> From<BranchID> for Hook<S> {
fn from(id: BranchID) -> Self {
Desc {
Hook {
id,
_tag: PhantomData::default(),
}
}
}

impl<S> Into<BranchID> for Desc<S> {
impl<S> Into<BranchID> for Hook<S> {
fn into(self) -> BranchID {
self.id
}
Expand Down
2 changes: 1 addition & 1 deletion yrs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ pub use crate::alt::{
pub use crate::any::Any;
pub use crate::block::ID;
pub use crate::branch::BranchID;
pub use crate::branch::Desc;
pub use crate::branch::Hook;
pub use crate::branch::Nested;
pub use crate::branch::Root;
pub use crate::doc::Doc;
Expand Down
4 changes: 2 additions & 2 deletions yrs/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ pub trait ReadTxn: Sized {

/// Check if given node is alive. Returns false if node has been deleted.
fn is_alive<B>(&self, node: &B) -> bool
where
B: SharedRef,
where
B: SharedRef,
{
let ptr = BranchPtr::from(node.as_ref());
self.store().is_alive(&ptr)
Expand Down
8 changes: 4 additions & 4 deletions yrs/src/types/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,12 +1015,12 @@ mod test {
let c1 = Rc::new(RefCell::new(None));
let c1c = c1.clone();
let _s1 = a1.observe(move |_, e| {
*c1c.borrow_mut() = Some(e.target().desc());
*c1c.borrow_mut() = Some(e.target().hook());
});
let c2 = Rc::new(RefCell::new(None));
let c2c = c2.clone();
let _s2 = a2.observe(move |_, e| {
*c2c.borrow_mut() = Some(e.target().desc());
*c2c.borrow_mut() = Some(e.target().hook());
});

{
Expand All @@ -1029,8 +1029,8 @@ mod test {
}
exchange_updates(&[&d1, &d2]);

assert_eq!(c1.borrow_mut().take(), Some(a1.desc()));
assert_eq!(c2.borrow_mut().take(), Some(a2.desc()));
assert_eq!(c1.borrow_mut().take(), Some(a1.hook()));
assert_eq!(c2.borrow_mut().take(), Some(a2.hook()));
}

use crate::transaction::ReadTxn;
Expand Down
4 changes: 2 additions & 2 deletions yrs/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ pub trait RootRef: SharedRef {
/// Common trait for shared collaborative collection types in Yrs.
pub trait SharedRef: From<BranchPtr> + AsRef<Branch> {
/// Returns a logical descriptor of a current shared collection.
fn desc(&self) -> Desc<Self> {
fn hook(&self) -> Hook<Self> {
let branch = self.as_ref();
Desc::from(branch.id())
Hook::from(branch.id())
}
}

Expand Down
16 changes: 8 additions & 8 deletions yrs/src/types/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,13 +1304,13 @@ mod test {
"query selector should found two paragraphs"
);
assert_eq!(
actual[0].desc(),
p1.desc(),
actual[0].hook(),
p1.hook(),
"query selector found 1st paragraph"
);
assert_eq!(
actual[1].desc(),
p2.desc(),
actual[1].hook(),
p2.hook(),
"query selector found 2nd paragraph"
);
}
Expand Down Expand Up @@ -1338,23 +1338,23 @@ mod test {

assert_eq!(
&first.siblings(&txn).next().unwrap().id(),
second.desc().id(),
second.hook().id(),
"first.next_sibling should point to second"
);
assert_eq!(
&second.siblings(&txn).next_back().unwrap().id(),
first.desc().id(),
first.hook().id(),
"second.prev_sibling should point to first"
);
assert_eq!(
&first.parent().unwrap().id(),
root.desc().id(),
root.hook().id(),
"first.parent should point to root"
);
assert!(root.parent().is_none(), "root parent should not exist");
assert_eq!(
&root.first_child().unwrap().id(),
first.desc().id(),
first.hook().id(),
"root.first_child should point to first"
);
}
Expand Down
2 changes: 1 addition & 1 deletion ywasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "ywasm"
version = "0.17.4"
authors = ["Kevin Jahns <kevin.jahns@protonmail.com>","Bartosz Sypytkowski <b.sypytkowski@gmail.com>"]
authors = ["Kevin Jahns <kevin.jahns@protonmail.com>", "Bartosz Sypytkowski <b.sypytkowski@gmail.com>"]
keywords = ["crdt", "wasm", "yrs"]
edition = "2018"
license = "MIT"
Expand Down
41 changes: 31 additions & 10 deletions ywasm/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::iter::FromIterator;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use yrs::types::array::ArrayEvent;
use yrs::types::ToJson;
use yrs::types::{ToJson, TYPE_REFS_ARRAY};
use yrs::{Array, ArrayRef, DeepObservable, Observable, SharedRef, TransactionMut};

/// A collection used to store data in an indexed sequence structure. This type is internally
Expand Down Expand Up @@ -44,6 +44,11 @@ impl YArray {
YArray(SharedCollection::prelim(items.unwrap_or_default()))
}

#[wasm_bindgen(getter, js_name = type)]
pub fn get_type(&self) -> u8 {
TYPE_REFS_ARRAY
}

/// Returns true if this is a preliminary instance of `YArray`.
///
/// Preliminary instances can be nested into other shared data types such as `YArray` and `YMap`.
Expand Down Expand Up @@ -83,7 +88,8 @@ impl YArray {
Ok(a.into())
}
SharedCollection::Integrated(c) => c.readonly(txn, |c, txn| {
JsValue::from_serde(&c.to_json(txn)).map_err(|e| JsValue::from_str(&e.to_string()))
let any = c.to_json(txn);
JsValue::from_serde(&any).map_err(|e| JsValue::from_str(&e.to_string()))
}),
}
}
Expand Down Expand Up @@ -305,17 +311,22 @@ pub(crate) trait ArrayExt: Array + SharedRef {
let js = Js::from(value);
match js.as_value()? {
ValueRef::Any(any) => primitive.push(any),
ValueRef::Shared(shared) if shared.prelim() => {
let len = primitive.len() as u32;
if len > 0 {
self.insert_range(txn, j, std::mem::take(&mut primitive));
j += len;
ValueRef::Shared(shared) => {
if shared.prelim() {
let len = primitive.len() as u32;
if len > 0 {
self.insert_range(txn, j, std::mem::take(&mut primitive));
j += len;
}
self.insert(txn, j, shared);
j += 1;
} else {
let err = format!("cannot insert item at index {}: shared collection is not a preliminary type", i);
return Err(JsValue::from(&err));
}
self.insert(txn, j, shared);
j += 1;
}
_ => {
let err = format!("item at position {} cannot be inserted", i);
let err = format!("cannot insert item at index {}", i);
return Err(JsValue::from(&err));
}
}
Expand Down Expand Up @@ -370,6 +381,16 @@ impl YArrayEvent {
js.clone()
}

#[wasm_bindgen(getter)]
pub fn origin(&mut self) -> JsValue {
let origin = self.txn.origin();
if let Some(origin) = origin {
Js::from(origin).into()
} else {
JsValue::UNDEFINED
}
}

/// Returns a list of text changes made over corresponding `YArray` collection within
/// bounds of current transaction. These changes follow a format:
///
Expand Down
Loading