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
exposed logical IDs in ywasm API
  • Loading branch information
Horusiath committed Mar 8, 2024
commit 19cdcd87bac6efcf2a49c1bc12b70f8ad8322ad4
37 changes: 36 additions & 1 deletion tests-wasm/y-doc.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export const testOnAfterTransaction = tc => {

text.insert(0, 'hello world')

console.log(event)
t.compare(event.beforeState, new Map());
let state = new Map()
state.set(1, 11)
Expand Down Expand Up @@ -298,4 +297,40 @@ export const testRoots = tc => {
let d2 = new Y.YDoc()
exchangeUpdates([d1, d2])
t.compare(d2.roots(), [['a', undefined]])
}

/**
* @param {t.TestCase} tc
*/
export const testIds = tc => {
const d1 = new Y.YDoc()
const a1 = d1.getArray('a')

const d2 = new Y.YDoc()
d2.getArray('a') // root types need to be pre-initialized

const m1 = new Y.YMap({'key1': 'value1'})
a1.push([m1]) // set nested type on a first doc

const arrayId = a1.id
const mapId = m1.id

// sync
exchangeUpdates([d1, d2])

// resolve instances using identifiers
const m2 = d2.transact(tx => tx.get(mapId))

t.compare(m2.toJson(), {'key1': 'value1'})

const a2 = d2.transact(tx => tx.get(arrayId))

// modify instance on remote end and sync
m2.set('key1', 'value2')
a2.insert(0, ['abc'])

exchangeUpdates([d1, d2])

// check if first doc has correctly updated values
t.compare(a1.toJson(), ['abc', {'key1': 'value2'}])
}
11 changes: 10 additions & 1 deletion yrs/src/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::types::{
Entries, Event, Events, Path, PathSegment, RootRef, SharedRef, TypePtr, TypeRef,
};
use crate::{
ArrayRef, MapRef, Observer, Origin, ReadTxn, Subscription, TextRef, TransactionMut, Value,
ArrayRef, Doc, MapRef, Observer, Origin, ReadTxn, Subscription, TextRef, TransactionMut, Value,
WriteTxn, XmlElementRef, XmlFragmentRef, XmlTextRef, ID,
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -256,6 +256,15 @@ impl Branch {
}
}

pub fn as_subdoc(&self) -> Option<Doc> {
let item = self.item?;
if let ItemContent::Doc(_, doc) = &item.content {
Some(doc.clone())
} else {
None
}
}

/// Returns an identifier of an underlying complex data type (eg. is it an Array or a Map).
pub fn type_ref(&self) -> &TypeRef {
&self.type_ref
Expand Down
8 changes: 8 additions & 0 deletions ywasm/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ impl YArray {
TYPE_REFS_ARRAY
}

/// Gets unique logical identifier of this type, shared across peers collaborating on the same
/// document.
#[wasm_bindgen(getter, js_name = id)]
#[inline]
pub fn id(&self) -> crate::Result<JsValue> {
self.0.id()
}

/// 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
13 changes: 13 additions & 0 deletions ywasm/src/collection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::transaction::{ImplicitTransaction, YTransaction};
use crate::Result;
use gloo_utils::format::JsValueSerdeExt;
use std::ops::Deref;
use wasm_bindgen::JsValue;
use yrs::{BranchID, Doc, Hook, ReadTxn, SharedRef, Transact, Transaction, TransactionMut};
Expand All @@ -20,6 +21,18 @@ impl<P, S: SharedRef + 'static> SharedCollection<P, S> {
SharedCollection::Integrated(Integrated::new(shared_ref, doc))
}

pub fn id(&self) -> crate::Result<JsValue> {
match self {
SharedCollection::Prelim(_) => {
Err(JsValue::from_str(crate::js::errors::INVALID_PRELIM_OP))
}
SharedCollection::Integrated(c) => {
let branch_id = c.hook.id();
JsValue::from_serde(branch_id).map_err(|e| JsValue::from_str(&e.to_string()))
}
}
}

pub fn try_integrated(&self) -> Result<(&BranchID, &Doc)> {
match self {
SharedCollection::Integrated(i) => {
Expand Down
8 changes: 8 additions & 0 deletions ywasm/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ impl YMap {
TYPE_REFS_MAP
}

/// Gets unique logical identifier of this type, shared across peers collaborating on the same
/// document.
#[wasm_bindgen(getter, js_name = id)]
#[inline]
pub fn id(&self) -> crate::Result<JsValue> {
self.0.id()
}

/// Returns true if this is a preliminary instance of `YMap`.
///
/// Preliminary instances can be nested into other shared data types such as `YArray` and `YMap`.
Expand Down
8 changes: 8 additions & 0 deletions ywasm/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ impl YText {
TYPE_REFS_TEXT
}

/// Gets unique logical identifier of this type, shared across peers collaborating on the same
/// document.
#[wasm_bindgen(getter, js_name = id)]
#[inline]
pub fn id(&self) -> crate::Result<JsValue> {
self.0.id()
}

/// 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
64 changes: 63 additions & 1 deletion ywasm/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
use crate::array::YArray;
use crate::collection::SharedCollection;
use crate::doc::YDoc;
use crate::js::Js;
use crate::map::YMap;
use crate::text::YText;
use crate::weak::YWeakLink;
use crate::xml_elem::YXmlElement;
use crate::xml_frag::YXmlFragment;
use crate::xml_text::YXmlText;
use crate::Result;
use gloo_utils::format::JsValueSerdeExt;
use js_sys::Uint8Array;
use std::ops::Deref;
use wasm_bindgen::__rt::{Ref, RefMut};
use wasm_bindgen::convert::{IntoWasmAbi, RefFromWasmAbi, RefMutFromWasmAbi};
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use yrs::types::TypeRef;
use yrs::updates::decoder::Decode;
use yrs::updates::encoder::Encode;
use yrs::{ReadTxn, TransactionMut, Update};
use yrs::{
ArrayRef, BranchID, MapRef, ReadTxn, TextRef, TransactionMut, Update, WeakRef, XmlElementRef,
XmlFragmentRef, XmlTextRef,
};

#[wasm_bindgen]
extern "C" {
Expand Down Expand Up @@ -89,6 +103,13 @@ impl YTransaction {
}
}

pub fn as_ref(&self) -> &TransactionMut<'static> {
match &self.inner {
Cell::Owned(v) => v,
Cell::Borrowed(v) => v,
}
}

pub fn as_mut(&mut self) -> Result<&mut TransactionMut<'static>> {
match &mut self.inner {
Cell::Owned(v) => Ok(v),
Expand Down Expand Up @@ -134,6 +155,47 @@ impl YTransaction {
}
}

/// Given a logical identifier of the collection (obtained via `YText.id`, `YArray.id` etc.),
/// attempts to return an instance of that collection in the scope of current document.
///
/// Returns `undefined` if an instance was not defined locally, haven't been integrated or
/// has been deleted.
#[wasm_bindgen(js_name = get)]
pub fn get(&self, id: JsValue) -> crate::Result<JsValue> {
let branch_id: BranchID =
JsValue::into_serde(&id).map_err(|e| JsValue::from_str(&e.to_string()))?;
let txn = self.as_ref();
let doc = txn.doc().clone();
Ok(match branch_id.get_branch(txn) {
None => JsValue::UNDEFINED,
Some(b) if b.is_deleted() => JsValue::UNDEFINED,
Some(b) => match b.type_ref() {
TypeRef::Array => {
YArray(SharedCollection::integrated(ArrayRef::from(b), doc)).into()
}
TypeRef::Map => YMap(SharedCollection::integrated(MapRef::from(b), doc)).into(),
TypeRef::Text => YText(SharedCollection::integrated(TextRef::from(b), doc)).into(),
TypeRef::XmlElement(_) => {
YXmlElement(SharedCollection::integrated(XmlElementRef::from(b), doc)).into()
}
TypeRef::XmlFragment => {
YXmlFragment(SharedCollection::integrated(XmlFragmentRef::from(b), doc)).into()
}
TypeRef::XmlText => {
YXmlText(SharedCollection::integrated(XmlTextRef::from(b), doc)).into()
}
TypeRef::WeakLink(_) => {
YWeakLink(SharedCollection::integrated(WeakRef::from(b), doc)).into()
}
TypeRef::SubDoc => match b.as_subdoc() {
None => JsValue::UNDEFINED,
Some(doc) => YDoc(doc).into(),
},
TypeRef::XmlHook | TypeRef::Undefined => JsValue::UNDEFINED,
},
})
}

/// Triggers a post-update series of operations without `free`ing the transaction. This includes
/// compaction and optimization of internal representation of updates, triggering events etc.
/// ywasm transactions are auto-committed when they are `free`d.
Expand Down
8 changes: 8 additions & 0 deletions ywasm/src/weak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ impl YWeakLink {
TYPE_REFS_WEAK
}

/// Gets unique logical identifier of this type, shared across peers collaborating on the same
/// document.
#[wasm_bindgen(getter, js_name = id)]
#[inline]
pub fn id(&self) -> crate::Result<JsValue> {
self.0.id()
}

/// Checks if current YWeakLink reference is alive and has not been deleted by its parent collection.
/// This method only works on already integrated shared types and will return false is current
/// type is preliminary (has not been integrated into document).
Expand Down
8 changes: 8 additions & 0 deletions ywasm/src/xml_elem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ impl YXmlElement {
TYPE_REFS_XML_ELEMENT
}

/// Gets unique logical identifier of this type, shared across peers collaborating on the same
/// document.
#[wasm_bindgen(getter, js_name = id)]
#[inline]
pub fn id(&self) -> crate::Result<JsValue> {
self.0.id()
}

/// Returns true if this is a preliminary instance of `YXmlElement`.
///
/// Preliminary instances can be nested into other shared data types.
Expand Down
8 changes: 8 additions & 0 deletions ywasm/src/xml_frag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ impl YXmlFragment {
TYPE_REFS_XML_FRAGMENT
}

/// Gets unique logical identifier of this type, shared across peers collaborating on the same
/// document.
#[wasm_bindgen(getter, js_name = id)]
#[inline]
pub fn id(&self) -> crate::Result<JsValue> {
self.0.id()
}

/// Returns true if this is a preliminary instance of `YXmlFragment`.
///
/// Preliminary instances can be nested into other shared data types.
Expand Down
8 changes: 8 additions & 0 deletions ywasm/src/xml_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ impl YXmlText {
TYPE_REFS_XML_TEXT
}

/// Gets unique logical identifier of this type, shared across peers collaborating on the same
/// document.
#[wasm_bindgen(getter, js_name = id)]
#[inline]
pub fn id(&self) -> crate::Result<JsValue> {
self.0.id()
}

/// Returns true if this is a preliminary instance of `YXmlText`.
///
/// Preliminary instances can be nested into other shared data types.
Expand Down