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: prepared WeakLink
  • Loading branch information
Horusiath committed Feb 24, 2024
commit 6c38c3daa0e318ebc731624506fd5fa7e705b045
10 changes: 10 additions & 0 deletions yrs/src/types/weak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ use thiserror::Error;
pub struct WeakRef<P>(P);

impl<P: SharedRef> SharedRef for WeakRef<P> {}
impl SharedRef for WeakRef<BranchPtr> {}
impl<P: SharedRef> From<WeakRef<BranchPtr>> for WeakRef<P> {
fn from(value: WeakRef<BranchPtr>) -> Self {
WeakRef(P::from(value.0))
Expand Down Expand Up @@ -402,6 +403,15 @@ impl<P: AsRef<Branch>> From<WeakRef<P>> for WeakPrelim<P> {
}
}

impl<P: AsRef<Branch>> WeakPrelim<P> {
pub fn upcast(self) -> WeakPrelim<BranchPtr> {
WeakPrelim {
source: self.source,
_marker: Default::default(),
}
}
}

impl<P: TryFrom<ItemPtr>> Prelim for WeakPrelim<P> {
type Return = WeakRef<P>;

Expand Down
48 changes: 23 additions & 25 deletions ywasm/src/array.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::collection::SharedCollection;
use crate::js::{Js, ValueRef};
use crate::js::{Js, ValueRef, YRange};
use crate::transaction::{ImplicitTransaction, YTransaction};
use crate::weak::YWeakLink;
use crate::{Observer, Result};
use gloo_utils::format::JsValueSerdeExt;
use std::iter::FromIterator;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use yrs::types::array::ArrayEvent;
use yrs::types::{ToJson, TYPE_REFS_ARRAY};
use yrs::{Array, ArrayRef, DeepObservable, Observable, SharedRef, TransactionMut};
use yrs::{Array, ArrayRef, DeepObservable, Observable, Quotable, SharedRef, TransactionMut};

/// A collection used to store data in an indexed sequence structure. This type is internally
/// implemented as a double linked list, which may squash values inserted directly one after another
Expand Down Expand Up @@ -73,7 +74,7 @@ impl YArray {

/// Returns a number of elements stored within this instance of `YArray`.
#[wasm_bindgen(js_name = length)]
pub fn length(&self, txn: ImplicitTransaction) -> Result<u32> {
pub fn length(&self, txn: &ImplicitTransaction) -> Result<u32> {
match &self.0 {
SharedCollection::Prelim(c) => Ok(c.len() as u32),
SharedCollection::Integrated(c) => c.readonly(txn, |c, txn| Ok(c.len(txn))),
Expand All @@ -82,7 +83,7 @@ impl YArray {

/// Converts an underlying contents of this `YArray` instance into their JSON representation.
#[wasm_bindgen(js_name = toJson)]
pub fn to_json(&self, txn: ImplicitTransaction) -> Result<JsValue> {
pub fn to_json(&self, txn: &ImplicitTransaction) -> Result<JsValue> {
match &self.0 {
SharedCollection::Prelim(c) => {
let a = js_sys::Array::new();
Expand Down Expand Up @@ -183,7 +184,7 @@ impl YArray {

/// Returns an element stored under given `index`.
#[wasm_bindgen(js_name = get)]
pub fn get(&self, index: u32, txn: ImplicitTransaction) -> Result<JsValue> {
pub fn get(&self, index: u32, txn: &ImplicitTransaction) -> Result<JsValue> {
match &self.0 {
SharedCollection::Prelim(c) => match c.get(index as usize) {
Some(item) => Ok(item.clone()),
Expand All @@ -203,23 +204,20 @@ impl YArray {
upper: u32,
lower_open: Option<bool>,
upper_open: Option<bool>,
txn: ImplicitTransaction,
) -> Result<JsValue> {
todo!()
//match &*self.0.borrow() {
// SharedType::Integrated(v) => {
// let range = YRange::new(lower, upper, lower_open, upper_open);
// let value = if let Some(txn) = get_txn(txn) {
// v.quote(&*txn, range)
// } else {
// v.quote(&v.transact(), range)
// };
// Ok(value.map(|v| YWeakLink::from(v).into()).unwrap_or_default())
// }
// SharedType::Prelim(_) => Err(JsValue::from_str(
// "YArray.quote cannot be used over object not integrated into YDoc",
// )),
//}
txn: &ImplicitTransaction,
) -> Result<YWeakLink> {
match &self.0 {
SharedCollection::Prelim(_) => {
Err(JsValue::from_str(crate::js::errors::INVALID_PRELIM_OP))
}
SharedCollection::Integrated(c) => c.readonly(txn, |c, txn| {
let range = YRange::new(lower, upper, lower_open, upper_open);
let quote = c
.quote(txn, range)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
Ok(YWeakLink::from_prelim(quote, txn.doc().clone()))
}),
}
}

/// Returns an iterator that can be used to traverse over the values stored withing this
Expand All @@ -244,7 +242,7 @@ impl YArray {
/// }
/// ```
#[wasm_bindgen(js_name = values)]
pub fn values(&self, txn: ImplicitTransaction) -> Result<JsValue> {
pub fn values(&self, txn: &ImplicitTransaction) -> Result<JsValue> {
match &self.0 {
SharedCollection::Prelim(c) => Ok(js_sys::Array::from_iter(c).into()),
SharedCollection::Integrated(c) => c.readonly(txn, |c, txn| {
Expand All @@ -269,7 +267,7 @@ impl YArray {
}
SharedCollection::Integrated(c) => {
let txn = c.transact()?;
let array = c.unpack(&txn)?;
let array = c.resolve(&txn)?;
Ok(Observer(array.observe(move |txn, e| {
let e = YArrayEvent::new(e, txn);
let txn = YTransaction::from_ref(txn);
Expand All @@ -292,7 +290,7 @@ impl YArray {
}
SharedCollection::Integrated(c) => {
let txn = c.transact()?;
let array = c.unpack(&txn)?;
let array = c.resolve(&txn)?;
Ok(Observer(array.observe_deep(move |txn, e| {
let e = crate::js::convert::events_into_js(txn, e);
let txn = YTransaction::from_ref(txn);
Expand Down
14 changes: 7 additions & 7 deletions ywasm/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ impl<S: SharedRef + 'static> Integrated<S> {
Integrated { hook: desc, doc }
}

pub fn readonly<F, T>(&self, txn: ImplicitTransaction, f: F) -> Result<T>
pub fn readonly<F, T>(&self, txn: &ImplicitTransaction, f: F) -> Result<T>
where
F: FnOnce(&S, &TransactionMut<'_>) -> Result<T>,
{
match YTransaction::from_implicit(&txn)? {
match YTransaction::from_implicit(txn)? {
Some(txn) => {
let txn: &TransactionMut = &*txn;
let shared_ref = self.unpack(txn)?;
let shared_ref = self.resolve(txn)?;
f(&shared_ref, txn)
}
None => {
let txn = self.transact_mut()?;
let shared_ref = self.unpack(&txn)?;
let shared_ref = self.resolve(&txn)?;
f(&shared_ref, &txn)
}
}
Expand All @@ -83,18 +83,18 @@ impl<S: SharedRef + 'static> Integrated<S> {
match YTransaction::from_implicit_mut(&mut txn)? {
Some(mut txn) => {
let txn = txn.as_mut()?;
let shared_ref = self.unpack(txn)?;
let shared_ref = self.resolve(txn)?;
f(&shared_ref, txn)
}
None => {
let mut txn = self.transact_mut()?;
let shared_ref = self.unpack(&mut txn)?;
let shared_ref = self.resolve(&mut txn)?;
f(&shared_ref, &mut txn)
}
}
}

pub fn unpack<T: ReadTxn>(&self, txn: &T) -> Result<S> {
pub fn resolve<T: ReadTxn>(&self, txn: &T) -> Result<S> {
match self.hook.get(txn) {
Some(shared_ref) => Ok(shared_ref),
None => Err(JsValue::from_str(crate::js::errors::REF_DISPOSED)),
Expand Down
41 changes: 39 additions & 2 deletions ywasm/src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::array::{ArrayExt, YArray};
use crate::collection::{Integrated, SharedCollection};
use crate::Result;
use js_sys::Uint8Array;
use std::collections::HashMap;
use std::collections::{Bound, HashMap};
use std::convert::TryInto;
use std::ops::Deref;
use std::ops::{Deref, RangeBounds};
use std::sync::Arc;
use wasm_bindgen::__rt::RefMut;
use wasm_bindgen::convert::{FromWasmAbi, IntoWasmAbi};
Expand Down Expand Up @@ -294,6 +294,43 @@ impl Prelim for Shared {
}
}

pub(crate) struct YRange {
lower: u32,
upper: u32,
lower_open: bool,
upper_open: bool,
}

impl YRange {
#[inline]
pub fn new(lower: u32, upper: u32, lower_open: Option<bool>, upper_open: Option<bool>) -> Self {
YRange {
lower,
upper,
lower_open: lower_open.unwrap_or(false),
upper_open: upper_open.unwrap_or(false),
}
}
}

impl RangeBounds<u32> for YRange {
fn start_bound(&self) -> Bound<&u32> {
if self.lower_open {
Bound::Excluded(&self.lower)
} else {
Bound::Included(&self.lower)
}
}

fn end_bound(&self) -> Bound<&u32> {
if self.upper_open {
Bound::Excluded(&self.upper)
} else {
Bound::Included(&self.upper)
}
}
}

pub(crate) const JS_PTR: &'static str = "__wbg_ptr";

pub(crate) mod convert {
Expand Down
10 changes: 7 additions & 3 deletions ywasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ mod doc;
mod js;
mod transaction;
mod undo;
mod weak;

type Result<T> = std::result::Result<T, JsValue>;

pub use crate::array::YArray as Array;
pub use crate::array::YArrayEvent as ArrayEvent;
pub use crate::doc::YDoc as Doc;
use crate::js::Js;
pub use crate::transaction::ImplicitTransaction;
pub use crate::transaction::YTransaction as Transaction;
pub use crate::undo::YUndoEvent as UndoEvent;
pub use crate::undo::YUndoManager as UndoManager;
pub use crate::weak::YWeakLink as WeakLink;
pub use crate::weak::YWeakLinkEvent as WeakLinkEvent;

#[wasm_bindgen]
#[repr(transparent)]
Expand Down Expand Up @@ -170,7 +174,7 @@ pub fn encode_state_as_update_v2(
#[wasm_bindgen(js_name = applyUpdate)]
pub fn apply_update(doc: &mut Doc, update: js_sys::Uint8Array, origin: JsValue) -> Result<()> {
let txn = if origin.is_undefined() {
doc.0.try_transact_mut_with(Js::from(origin))
doc.0.try_transact_mut_with(js::Js::from(origin))
} else {
doc.0.try_transact_mut()
};
Expand Down Expand Up @@ -207,7 +211,7 @@ pub fn apply_update_v2(
origin: JsValue,
) -> std::result::Result<(), JsValue> {
let txn = if origin.is_undefined() {
doc.0.try_transact_mut_with(Js::from(origin))
doc.0.try_transact_mut_with(js::Js::from(origin))
} else {
doc.0.try_transact_mut()
};
Expand Down
Loading