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: support YWeakLink
  • Loading branch information
Horusiath committed Feb 29, 2024
commit 064444c1b6ae5baeee673b79b544d1221d884963
25 changes: 20 additions & 5 deletions ywasm/src/js.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::array::{ArrayExt, YArray};
use crate::collection::{Integrated, SharedCollection};
use crate::doc::YDoc;
use crate::weak::YWeakLink;
use crate::Result;
use js_sys::Uint8Array;
use std::collections::{Bound, HashMap};
Expand All @@ -16,7 +17,7 @@ use yrs::types::{
TypeRef, TYPE_REFS_ARRAY, TYPE_REFS_DOC, TYPE_REFS_MAP, TYPE_REFS_TEXT, TYPE_REFS_WEAK,
TYPE_REFS_XML_ELEMENT, TYPE_REFS_XML_FRAGMENT, TYPE_REFS_XML_TEXT,
};
use yrs::{Any, ArrayRef, BranchID, Doc, Origin, TransactionMut, Value};
use yrs::{Any, ArrayRef, BranchID, Doc, Origin, TransactionMut, Value, WeakRef};

#[repr(transparent)]
pub struct Js(JsValue);
Expand Down Expand Up @@ -227,6 +228,7 @@ impl ValueRef {

pub enum Shared {
Array(RefMut<'static, YArray>),
Weak(RefMut<'static, YWeakLink>),
Doc(RefMut<'static, YDoc>),
}

Expand All @@ -236,13 +238,13 @@ impl Shared {
if let Some(tag) = tag.as_f64() {
match tag as u8 {
TYPE_REFS_ARRAY => Ok(Shared::Array(convert::mut_from_js::<YArray>(js)?)),
TYPE_REFS_WEAK => Ok(Shared::Weak(convert::mut_from_js::<YWeakLink>(js)?)),
TYPE_REFS_DOC => Ok(Shared::Doc(convert::mut_from_js::<YDoc>(js)?)),
TYPE_REFS_TEXT
| TYPE_REFS_MAP
| TYPE_REFS_XML_TEXT
| TYPE_REFS_XML_ELEMENT
| TYPE_REFS_XML_FRAGMENT
| TYPE_REFS_WEAK
| _ => Err(js.clone()),
}
} else {
Expand All @@ -254,20 +256,23 @@ impl Shared {
match self {
Shared::Array(v) => v.prelim(),
Shared::Doc(v) => v.prelim(),
Shared::Weak(v) => v.prelim(),
}
}

pub fn branch_id(&self) -> Option<BranchID> {
pub fn branch_id(&self) -> Option<&BranchID> {
match self {
Shared::Array(v) => v.0.branch_id().cloned(),
Shared::Doc(v) => v.0.branch_id(),
Shared::Array(v) => v.0.branch_id(),
Shared::Weak(v) => v.0.branch_id(),
Shared::Doc(_) => panic!("don't use YDoc in this context"),
}
}

fn type_ref(&self) -> TypeRef {
match self {
Shared::Array(_) => TypeRef::Array,
Shared::Doc(_) => TypeRef::SubDoc,
Shared::Weak(v) => TypeRef::WeakLink(v.source()),
}
}
}
Expand Down Expand Up @@ -296,6 +301,16 @@ impl Prelim for Shared {
array.insert_at(txn, 0, raw).unwrap();
}
}
Shared::Weak(mut cell) => {
let weak_link: WeakRef<BranchPtr> = WeakRef::from(inner_ref);
let _ = std::mem::replace(
&mut *cell,
YWeakLink(SharedCollection::Integrated(Integrated::new(
weak_link.clone(),
doc,
))),
);
}
Shared::Doc(_) => { /* do nothing */ }
}
}
Expand Down
14 changes: 11 additions & 3 deletions ywasm/src/weak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,38 @@ use crate::collection::SharedCollection;
use crate::js::Js;
use crate::transaction::YTransaction;
use crate::{ImplicitTransaction, Observer, Result};
use std::sync::Arc;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use yrs::branch::BranchPtr;
use yrs::types::weak::WeakEvent;
use yrs::types::weak::{LinkSource, WeakEvent};
use yrs::types::TYPE_REFS_WEAK;
use yrs::{
DeepObservable, Doc, GetString, Observable, SharedRef, Transact, TransactionMut, WeakPrelim,
WeakRef,
};

struct PrelimWrapper {
pub(crate) struct PrelimWrapper {
prelim: WeakPrelim<BranchPtr>,
doc: Doc,
}

#[wasm_bindgen]
#[repr(transparent)]
pub struct YWeakLink(SharedCollection<PrelimWrapper, WeakRef<BranchPtr>>);
pub struct YWeakLink(pub(crate) SharedCollection<PrelimWrapper, WeakRef<BranchPtr>>);

impl YWeakLink {
pub(crate) fn from_prelim<S: SharedRef>(prelim: WeakPrelim<S>, doc: Doc) -> Self {
let prelim = prelim.upcast();
YWeakLink(SharedCollection::Prelim(PrelimWrapper { prelim, doc }))
}

pub(crate) fn source(&self) -> Arc<LinkSource> {
match &self.0 {
SharedCollection::Integrated(_) => panic!("{}", crate::js::errors::NOT_PRELIM),
SharedCollection::Prelim(v) => v.prelim.source().clone(),
}
}
}

#[wasm_bindgen]
Expand Down