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: YDoc and YTransaction methods
  • Loading branch information
Horusiath committed Feb 22, 2024
commit 7afa7b9aaadde42e8b12f990bfba3718090372d6
20 changes: 10 additions & 10 deletions tests-wasm/testHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import * as Y from 'ywasm'
/**
* @this {YDoc}
*/
Y.YDoc.prototype.transact = function(callback, origin) {
let txn = this.writeTransaction(origin)
try {
return callback(txn)
} finally {
txn.commit()
txn.free()
}
Y.YDoc.prototype.transact = function (callback, origin) {
let txn = this.transaction(origin)
try {
return callback(txn)
} finally {
txn.commit()
txn.free()
}
};

/**
* @param {Array<YDoc>} docs
*/
export const exchangeUpdates = docs => {
for(let d1 of docs) {
for(let d2 of docs) {
for (let d1 of docs) {
for (let d2 of docs) {
if (d1 !== d2) {
let stateVector = Y.encodeStateVector(d1)
let diff = Y.encodeStateAsUpdate(d2, stateVector)
Expand Down
26 changes: 13 additions & 13 deletions tests-wasm/y-array.tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exchangeUpdates } from './testHelper.js' // eslint-disable-line
import {exchangeUpdates} from './testHelper.js' // eslint-disable-line

import * as Y from 'ywasm'
import * as t from 'lib0/testing'
Expand All @@ -7,19 +7,19 @@ import * as t from 'lib0/testing'
* @param {t.TestCase} tc
*/
export const testInserts = tc => {
const d1 = new Y.YDoc({clientID:1})
const d1 = new Y.YDoc({clientID: 1})
t.compare(d1.id, 1)
var x = d1.getArray('test');

x.insert(0, [1, 2.5, 'hello', ['world'], true])
x.push( [{key:'value'}])
x.push([{key: 'value'}])

const expected = [1, 2.5, 'hello', ['world'], true, {key:'value'}]
const expected = [1, 2.5, 'hello', ['world'], true, {key: 'value'}]

var value = x.toJson()
t.compare(value, expected)

const d2 = new Y.YDoc({clientID:2})
const d2 = new Y.YDoc({clientID: 2})
x = d2.getArray('test');

exchangeUpdates([d1, d2])
Expand Down Expand Up @@ -58,7 +58,7 @@ export const testInsertsNested = tc => {
* @param {t.TestCase} tc
*/
export const testDelete = tc => {
const d1 = new Y.YDoc({clientID:1})
const d1 = new Y.YDoc({clientID: 1})
t.compare(d1.id, 1)
var x = d1.getArray('test')

Expand All @@ -70,7 +70,7 @@ export const testDelete = tc => {
var value = x.toJson()
t.compare(value, expected)

const d2 = new Y.YDoc({clientID:2})
const d2 = new Y.YDoc({clientID: 2})
x = d2.getArray('test')

exchangeUpdates([d1, d2])
Expand Down Expand Up @@ -118,7 +118,7 @@ export const testIterator = tc => {
t.compare(x.length(), 3)

let i = 1;
let txn = d1.readTransaction()
let txn = d1.transaction()
for (let v of x.values(txn)) {
t.compare(v, i)
i++
Expand Down Expand Up @@ -146,26 +146,26 @@ export const testObserver = tc => {

// insert initial data to an empty YArray
d1.transact((txn) => {
x.insert(0, [1,2,3,4], txn)
x.insert(0, [1, 2, 3, 4], txn)
}, 'TEST_ORIGIN')
t.compare(target.toJson(), x.toJson())
t.compare(delta, [{insert: [1,2,3,4]}])
t.compare(delta, [{insert: [1, 2, 3, 4]}])
t.compare(origin, 'TEST_ORIGIN')
target = null
delta = null

// remove 2 items from the middle
x.delete(1, 2)
t.compare(target.toJson(), x.toJson())
t.compare(delta, [{retain:1}, {delete: 2}])
t.compare(delta, [{retain: 1}, {delete: 2}])
t.compare(origin, undefined)
target = null
delta = null

// insert new item in the middle
x.insert(1, [5])
t.compare(target.toJson(), x.toJson())
t.compare(delta, [{retain:1}, {insert: [5]}])
t.compare(delta, [{retain: 1}, {insert: [5]}])
target = null
delta = null

Expand Down Expand Up @@ -195,7 +195,7 @@ export const testObserveDeepEventOrder = tc => {
arr.get(0, txn).set('a', 'a', txn)
arr.insert(0, [0], txn)
})
t.compare(paths, [ [], [ 1 ] ])
t.compare(paths, [[], [1]])
subscription.free()
}

Expand Down
10 changes: 8 additions & 2 deletions yrs/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ pub trait ReadTxn: Sized {
encoder.to_vec()
}

fn encode_diff_v2(&self, state_vector: &StateVector) -> Vec<u8> {
let mut encoder = EncoderV2::new();
self.encode_diff(state_vector, &mut encoder);
encoder.to_vec()
}

fn encode_state_as_update<E: Encoder>(&self, sv: &StateVector, encoder: &mut E) {
let store = self.store();
store.write_blocks_from(sv, encoder);
Expand All @@ -85,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
10 changes: 5 additions & 5 deletions ywasm/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl YArray {

/// Deletes a range of items of given `length` from current `YArray` instance,
/// starting from given `index`.
#[wasm_bindgen(method, js_name = delete)]
#[wasm_bindgen(method, js_name = "delete")]
pub fn delete(
&mut self,
index: u32,
Expand Down Expand Up @@ -177,11 +177,11 @@ impl YArray {
match &self.0 {
SharedCollection::Prelim(c) => match c.get(index as usize) {
Some(item) => Ok(item.clone()),
None => Err(JsValue::from_str("index outside of the bounds of an array")),
None => Err(JsValue::from_str(crate::js::errors::OUT_OF_BOUNDS)),
},
SharedCollection::Integrated(c) => c.readonly(txn, |c, txn| match c.get(txn, index) {
Some(item) => Ok(Js::from_value(&item, txn.doc()).into()),
None => Err(JsValue::from_str("index outside of the bounds of an array")),
None => Err(JsValue::from_str(crate::js::errors::OUT_OF_BOUNDS)),
}),
}
}
Expand Down Expand Up @@ -255,7 +255,7 @@ impl YArray {
pub fn observe(&self, f: js_sys::Function) -> Result<Observer> {
match &self.0 {
SharedCollection::Prelim(_) => {
Err(JsValue::from_str("cannot observe preliminary type"))
Err(JsValue::from_str(crate::js::errors::INVALID_PRELIM_OP))
}
SharedCollection::Integrated(c) => {
let txn = c.transact()?;
Expand All @@ -278,7 +278,7 @@ impl YArray {
pub fn observe_deep(&self, f: js_sys::Function) -> Result<Observer> {
match &self.0 {
SharedCollection::Prelim(_) => {
Err(JsValue::from_str("cannot observe preliminary type"))
Err(JsValue::from_str(crate::js::errors::INVALID_PRELIM_OP))
}
SharedCollection::Integrated(c) => {
let txn = c.transact()?;
Expand Down
31 changes: 10 additions & 21 deletions ywasm/src/collection.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::js::Js;
use crate::transaction::{ImplicitTransaction, YTransaction};
use crate::Result;
use std::ops::Deref;
use wasm_bindgen::JsValue;
use yrs::{Desc, Doc, Origin, ReadTxn, SharedRef, Transact, Transaction, TransactionMut};
use yrs::{Desc, Doc, ReadTxn, SharedRef, Transact, Transaction, TransactionMut};

pub enum SharedCollection<P, S> {
Prelim(P),
Expand Down Expand Up @@ -52,10 +51,10 @@ impl<S: SharedRef + 'static> Integrated<S> {
}

pub fn readonly<F, T>(&self, txn: ImplicitTransaction, f: F) -> Result<T>
where
F: FnOnce(&S, &TransactionMut<'_>) -> 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)?;
Expand All @@ -70,10 +69,10 @@ impl<S: SharedRef + 'static> Integrated<S> {
}

pub fn mutably<F, T>(&self, mut txn: ImplicitTransaction, f: F) -> Result<T>
where
F: FnOnce(&S, &mut TransactionMut<'_>) -> Result<T>,
where
F: FnOnce(&S, &mut TransactionMut<'_>) -> Result<T>,
{
match YTransaction::from_implicit_mut(&mut txn) {
match YTransaction::from_implicit_mut(&mut txn)? {
Some(mut txn) => {
let txn = txn.as_mut()?;
let shared_ref = self.unpack(txn)?;
Expand All @@ -90,31 +89,21 @@ impl<S: SharedRef + 'static> Integrated<S> {
pub fn unpack<T: ReadTxn>(&self, txn: &T) -> Result<S> {
match self.desc.get(txn) {
Some(shared_ref) => Ok(shared_ref),
None => Err(JsValue::from_str("shared collection has been destroyed")),
None => Err(JsValue::from_str(crate::js::errors::REF_DISPOSED)),
}
}

pub fn transact(&self) -> Result<Transaction> {
match self.doc.try_transact() {
Ok(tx) => Ok(tx),
Err(_) => Err(JsValue::from_str(
"another read-write transaction is in progress",
)),
Err(_) => Err(JsValue::from_str(crate::js::errors::ANOTHER_RW_TX)),
}
}

pub fn transact_mut(&self) -> Result<TransactionMut> {
match self.doc.try_transact_mut() {
Ok(tx) => Ok(tx),
Err(_) => Err(JsValue::from_str("another transaction is in progress")),
}
}

pub fn transact_mut_with(&self, js: &JsValue) -> Result<TransactionMut> {
let origin: Origin = Js::from(js.clone()).into();
match self.doc.try_transact_mut_with(origin) {
Ok(tx) => Ok(tx),
Err(_) => Err(JsValue::from_str("another transaction is in progress")),
Err(_) => Err(JsValue::from_str(crate::js::errors::ANOTHER_TX)),
}
}
}
Loading