y-rb 0.5.6 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/yrb/Cargo.toml +5 -5
- data/ext/yrb/src/lib.rs +14 -14
- data/ext/yrb/src/utils.rs +12 -8
- data/ext/yrb/src/yany.rs +14 -12
- data/ext/yrb/src/yarray.rs +57 -55
- data/ext/yrb/src/yawareness.rs +12 -5
- data/ext/yrb/src/ydiff.rs +5 -2
- data/ext/yrb/src/ydoc.rs +9 -5
- data/ext/yrb/src/ymap.rs +27 -22
- data/ext/yrb/src/ytext.rs +53 -50
- data/ext/yrb/src/ytransaction.rs +5 -3
- data/ext/yrb/src/yvalue.rs +66 -43
- data/ext/yrb/src/yxml_element.rs +44 -29
- data/ext/yrb/src/yxml_fragment.rs +24 -17
- data/ext/yrb/src/yxml_text.rs +25 -11
- data/lib/y/version.rb +1 -1
- metadata +16 -16
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 45cb8a4398cd5e9bcfb0cd8e3006a31e7a92212866018ef2426717ee35b27330
|
|
4
|
+
data.tar.gz: 1fbf970ce3a9353afb725e24a2d004e2715ccf13a7e02d3bd54dac9e54b6776d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: de3a6e0735230fd9027ae7a0219938079b6d20bb23ffffdc30da59f2f50ef1ef4226958d4669e9af5f621e5e1c638b9267bdeef3bc14c771af617e389904396e
|
|
7
|
+
data.tar.gz: b59edafb9fba969b166a9c29d47f1bc7394c58c320b5b1a25eb5e10db189c7ae0c8ab3ee662f981b1a8905d208fab5dfa1fb1aff362a11a01bfc1e838f535396
|
data/ext/yrb/Cargo.toml
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "yrb"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.7.0"
|
|
4
4
|
authors = ["Hannes Moser <box@hannesmoser.at>"]
|
|
5
5
|
edition = "2021"
|
|
6
6
|
homepage = "https://github.com/y-crdt/yrb"
|
|
7
7
|
repository = "https://github.com/y-crdt/yrb"
|
|
8
8
|
|
|
9
9
|
[dependencies]
|
|
10
|
-
magnus = "
|
|
11
|
-
thiserror = "
|
|
10
|
+
magnus = "0.8"
|
|
11
|
+
thiserror = "2.0.17"
|
|
12
12
|
yrs = "=0.17.4"
|
|
13
13
|
y-sync = "=0.4.0"
|
|
14
|
-
rb-sys = "0.9
|
|
14
|
+
rb-sys = { version = "0.9", features = ["stable-api-compiled-fallback"] }
|
|
15
15
|
|
|
16
16
|
[dev-dependencies]
|
|
17
|
-
magnus = { version = "0.
|
|
17
|
+
magnus = { version = "0.8", features = ["embed"] }
|
|
18
18
|
|
|
19
19
|
[lib]
|
|
20
20
|
name = "yrb"
|
data/ext/yrb/src/lib.rs
CHANGED
|
@@ -11,7 +11,7 @@ use crate::yxml_element::YXmlElement;
|
|
|
11
11
|
use crate::yxml_fragment::YXmlFragment;
|
|
12
12
|
use crate::yxml_text::YXmlText;
|
|
13
13
|
|
|
14
|
-
use magnus::{
|
|
14
|
+
use magnus::{function, method, Error, Module, Object, Ruby};
|
|
15
15
|
|
|
16
16
|
mod utils;
|
|
17
17
|
mod yany;
|
|
@@ -29,11 +29,11 @@ mod yxml_fragment;
|
|
|
29
29
|
mod yxml_text;
|
|
30
30
|
|
|
31
31
|
#[magnus::init]
|
|
32
|
-
fn init() -> Result<(), Error> {
|
|
33
|
-
let module = define_module("Y").expect("cannot define ::Y module");
|
|
32
|
+
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
33
|
+
let module = ruby.define_module("Y").expect("cannot define ::Y module");
|
|
34
34
|
|
|
35
35
|
let yarray = module
|
|
36
|
-
.define_class("Array",
|
|
36
|
+
.define_class("Array", ruby.class_object())
|
|
37
37
|
.expect("cannot find class Y::Array");
|
|
38
38
|
|
|
39
39
|
yarray
|
|
@@ -80,7 +80,7 @@ fn init() -> Result<(), Error> {
|
|
|
80
80
|
.expect("cannot define private method: yarray_unobserve");
|
|
81
81
|
|
|
82
82
|
let ydoc = module
|
|
83
|
-
.define_class("Doc",
|
|
83
|
+
.define_class("Doc", ruby.class_object())
|
|
84
84
|
.expect("cannot define class Y::Doc");
|
|
85
85
|
ydoc.define_singleton_method("new", function!(YDoc::ydoc_new, -1))
|
|
86
86
|
.expect("cannot define singleton method: ydoc_new");
|
|
@@ -125,7 +125,7 @@ fn init() -> Result<(), Error> {
|
|
|
125
125
|
.expect("cannot define private method: ydoc_observe_update");
|
|
126
126
|
|
|
127
127
|
let ymap = module
|
|
128
|
-
.define_class("Map",
|
|
128
|
+
.define_class("Map", ruby.class_object())
|
|
129
129
|
.expect("cannot define class Y::Map");
|
|
130
130
|
|
|
131
131
|
ymap.define_private_method("ymap_clear", method!(YMap::ymap_clear, 1))
|
|
@@ -150,7 +150,7 @@ fn init() -> Result<(), Error> {
|
|
|
150
150
|
.expect("cannot define private method: ymap_unobserve");
|
|
151
151
|
|
|
152
152
|
let ytransaction = module
|
|
153
|
-
.define_class("Transaction",
|
|
153
|
+
.define_class("Transaction", ruby.class_object())
|
|
154
154
|
.expect("cannot define class Y::Transaction");
|
|
155
155
|
|
|
156
156
|
ytransaction
|
|
@@ -224,7 +224,7 @@ fn init() -> Result<(), Error> {
|
|
|
224
224
|
.expect("cannot define private method: ytransaction_state_vector_v2");
|
|
225
225
|
|
|
226
226
|
let ytext = module
|
|
227
|
-
.define_class("Text",
|
|
227
|
+
.define_class("Text", ruby.class_object())
|
|
228
228
|
.expect("cannot define class Y::Text");
|
|
229
229
|
|
|
230
230
|
ytext
|
|
@@ -271,7 +271,7 @@ fn init() -> Result<(), Error> {
|
|
|
271
271
|
.expect("cannot define private method: ytext_unobserve");
|
|
272
272
|
|
|
273
273
|
let yxml_element = module
|
|
274
|
-
.define_class("XMLElement",
|
|
274
|
+
.define_class("XMLElement", ruby.class_object())
|
|
275
275
|
.expect("cannot define class Y::XMLElement");
|
|
276
276
|
|
|
277
277
|
yxml_element
|
|
@@ -414,7 +414,7 @@ fn init() -> Result<(), Error> {
|
|
|
414
414
|
.expect("cannot define private method: yxml_element_unobserve");
|
|
415
415
|
|
|
416
416
|
let yxml_fragment = module
|
|
417
|
-
.define_class("XMLFragment",
|
|
417
|
+
.define_class("XMLFragment", ruby.class_object())
|
|
418
418
|
.expect("cannot define class: Y::XMLFragment");
|
|
419
419
|
|
|
420
420
|
yxml_fragment
|
|
@@ -479,7 +479,7 @@ fn init() -> Result<(), Error> {
|
|
|
479
479
|
.expect("cannot define private method: yxml_fragment_to_s");
|
|
480
480
|
|
|
481
481
|
let yxml_text = module
|
|
482
|
-
.define_class("XMLText",
|
|
482
|
+
.define_class("XMLText", ruby.class_object())
|
|
483
483
|
.expect("cannot define class Y::XMLText");
|
|
484
484
|
|
|
485
485
|
yxml_text
|
|
@@ -556,7 +556,7 @@ fn init() -> Result<(), Error> {
|
|
|
556
556
|
.expect("cannot define private method: yxml_text_to_s");
|
|
557
557
|
|
|
558
558
|
let yawareness = module
|
|
559
|
-
.define_class("Awareness",
|
|
559
|
+
.define_class("Awareness", ruby.class_object())
|
|
560
560
|
.expect("cannot define class Y::Awareness");
|
|
561
561
|
yawareness
|
|
562
562
|
.define_singleton_method("new", function!(YAwareness::yawareness_new, 0))
|
|
@@ -623,7 +623,7 @@ fn init() -> Result<(), Error> {
|
|
|
623
623
|
.expect("cannot define private method: yawareness_update_with_clients");
|
|
624
624
|
|
|
625
625
|
let yawareness_event = module
|
|
626
|
-
.define_class("AwarenessEvent",
|
|
626
|
+
.define_class("AwarenessEvent", ruby.class_object())
|
|
627
627
|
.expect("cannot define class Y::AwarenessEvent");
|
|
628
628
|
yawareness_event
|
|
629
629
|
.define_method("added", method!(YAwarenessEvent::added, 0))
|
|
@@ -636,7 +636,7 @@ fn init() -> Result<(), Error> {
|
|
|
636
636
|
.expect("cannot define private method: removed");
|
|
637
637
|
|
|
638
638
|
let ydiff = module
|
|
639
|
-
.define_class("Diff",
|
|
639
|
+
.define_class("Diff", ruby.class_object())
|
|
640
640
|
.expect("cannot define class Y::Diff");
|
|
641
641
|
ydiff
|
|
642
642
|
.define_private_method("ydiff_insert", method!(YDiff::ydiff_insert, 0))
|
data/ext/yrb/src/utils.rs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
use crate::yvalue::YValue;
|
|
2
2
|
use magnus::r_hash::ForEach::Continue;
|
|
3
|
-
use magnus::{
|
|
3
|
+
use magnus::{Error, RHash, RString, Ruby, Symbol, Value};
|
|
4
4
|
use std::sync::Arc;
|
|
5
5
|
use yrs::types::{Attrs, Value as YrsValue};
|
|
6
6
|
use yrs::{Any, Array, Map, TransactionMut};
|
|
7
7
|
|
|
8
|
+
#[allow(dead_code)]
|
|
8
9
|
#[derive(Debug, Clone)]
|
|
9
10
|
pub(crate) struct TypeConversionError;
|
|
10
11
|
|
|
@@ -27,8 +28,9 @@ pub(crate) fn map_rhash_to_attrs(hash: RHash) -> Result<Attrs, Error> {
|
|
|
27
28
|
});
|
|
28
29
|
|
|
29
30
|
if result.is_err() {
|
|
31
|
+
let ruby = Ruby::get().unwrap();
|
|
30
32
|
return Err(Error::new(
|
|
31
|
-
|
|
33
|
+
ruby.exception_runtime_error(),
|
|
32
34
|
"could not map hash to attrs",
|
|
33
35
|
));
|
|
34
36
|
}
|
|
@@ -37,13 +39,14 @@ pub(crate) fn map_rhash_to_attrs(hash: RHash) -> Result<Attrs, Error> {
|
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
pub(crate) fn convert_yvalue_to_ruby_value(value: YrsValue, tx: &TransactionMut) -> YValue {
|
|
42
|
+
let ruby = unsafe { Ruby::get_unchecked() };
|
|
40
43
|
match value {
|
|
41
44
|
YrsValue::Any(val) => YValue::from(val),
|
|
42
45
|
YrsValue::YText(text) => YValue::from(text),
|
|
43
46
|
YrsValue::YXmlElement(el) => YValue::from(el),
|
|
44
47
|
YrsValue::YXmlText(text) => YValue::from(text),
|
|
45
48
|
YrsValue::YArray(val) => {
|
|
46
|
-
let arr =
|
|
49
|
+
let arr = ruby.ary_new();
|
|
47
50
|
for item in val.iter(tx) {
|
|
48
51
|
let val = convert_yvalue_to_ruby_value(item.clone(), tx);
|
|
49
52
|
let val = *val.0.borrow();
|
|
@@ -52,12 +55,13 @@ pub(crate) fn convert_yvalue_to_ruby_value(value: YrsValue, tx: &TransactionMut)
|
|
|
52
55
|
YValue::from(arr)
|
|
53
56
|
}
|
|
54
57
|
YrsValue::YMap(val) => {
|
|
55
|
-
let
|
|
56
|
-
|
|
58
|
+
let hash = ruby.hash_new();
|
|
59
|
+
for (key, value) in val.iter(tx) {
|
|
60
|
+
let val = convert_yvalue_to_ruby_value(value.clone(), tx);
|
|
57
61
|
let val = val.0.into_inner();
|
|
58
|
-
(key, val)
|
|
59
|
-
}
|
|
60
|
-
YValue::from(
|
|
62
|
+
hash.aset(key, val).expect("cannot insert into hash");
|
|
63
|
+
}
|
|
64
|
+
YValue::from(hash)
|
|
61
65
|
}
|
|
62
66
|
v => panic!("cannot map given yrs values to yvalue: {:?}", v),
|
|
63
67
|
}
|
data/ext/yrb/src/yany.rs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
use magnus::r_string::IntoRString;
|
|
2
2
|
use magnus::value::ReprValue;
|
|
3
|
-
use magnus::{
|
|
3
|
+
use magnus::{IntoValue, Ruby, Value};
|
|
4
4
|
use std::borrow::Borrow;
|
|
5
5
|
use std::ops::{Deref, DerefMut};
|
|
6
6
|
use yrs::Any;
|
|
7
7
|
|
|
8
|
+
#[allow(dead_code)]
|
|
8
9
|
pub(crate) struct YAny(pub(crate) Any);
|
|
9
10
|
|
|
10
11
|
impl Deref for YAny {
|
|
@@ -25,22 +26,23 @@ impl TryInto<Value> for YAny {
|
|
|
25
26
|
type Error = ();
|
|
26
27
|
|
|
27
28
|
fn try_into(self) -> Result<Value, Self::Error> {
|
|
28
|
-
|
|
29
|
+
let ruby = Ruby::get().unwrap();
|
|
30
|
+
match self.0 {
|
|
29
31
|
Any::Array(_v) => {
|
|
30
|
-
let arr =
|
|
32
|
+
let arr = ruby.ary_new();
|
|
31
33
|
Ok(arr.as_value())
|
|
32
34
|
}
|
|
33
35
|
Any::Map(_v) => {
|
|
34
|
-
let hash =
|
|
36
|
+
let hash = ruby.hash_new();
|
|
35
37
|
Ok(hash.as_value())
|
|
36
38
|
}
|
|
37
|
-
Any::Null => Ok(
|
|
38
|
-
Any::Undefined => Ok(
|
|
39
|
-
Any::Bool(v) => Ok(v.
|
|
40
|
-
Any::Number(v) => Ok(v.
|
|
41
|
-
Any::BigInt(v) => Ok(v.
|
|
42
|
-
Any::String(v) => Ok(v.
|
|
43
|
-
Any::Buffer(v) => Ok(
|
|
44
|
-
}
|
|
39
|
+
Any::Null => Ok(ruby.qnil().as_value()),
|
|
40
|
+
Any::Undefined => Ok(ruby.qnil().as_value()),
|
|
41
|
+
Any::Bool(v) => Ok(v.into_value_with(&ruby)),
|
|
42
|
+
Any::Number(v) => Ok(v.into_value_with(&ruby)),
|
|
43
|
+
Any::BigInt(v) => Ok(v.into_value_with(&ruby)),
|
|
44
|
+
Any::String(v) => Ok(v.into_r_string_with(&ruby).as_value()),
|
|
45
|
+
Any::Buffer(v) => Ok(ruby.str_from_slice(v.borrow()).as_value()),
|
|
46
|
+
}
|
|
45
47
|
}
|
|
46
48
|
}
|
data/ext/yrb/src/yarray.rs
CHANGED
|
@@ -3,7 +3,7 @@ use crate::ytransaction::YTransaction;
|
|
|
3
3
|
use crate::yvalue::YValue;
|
|
4
4
|
use magnus::block::Proc;
|
|
5
5
|
use magnus::value::Qnil;
|
|
6
|
-
use magnus::{Error, IntoValue, RArray,
|
|
6
|
+
use magnus::{Error, IntoValue, RArray, Ruby, Value};
|
|
7
7
|
use std::cell::RefCell;
|
|
8
8
|
use yrs::types::Change;
|
|
9
9
|
use yrs::{Any, Array, ArrayRef, Observable};
|
|
@@ -58,9 +58,8 @@ impl YArray {
|
|
|
58
58
|
let tx = tx.as_mut().unwrap();
|
|
59
59
|
|
|
60
60
|
let add_values: Vec<Any> = values
|
|
61
|
-
.each()
|
|
62
61
|
.into_iter()
|
|
63
|
-
.map(|value| YValue::from(value
|
|
62
|
+
.map(|value| YValue::from(value).into())
|
|
64
63
|
.collect();
|
|
65
64
|
|
|
66
65
|
arr.insert_range(tx, index, add_values)
|
|
@@ -73,64 +72,66 @@ impl YArray {
|
|
|
73
72
|
arr.len(tx)
|
|
74
73
|
}
|
|
75
74
|
pub(crate) fn yarray_observe(&self, block: Proc) -> Result<u32, Error> {
|
|
76
|
-
let
|
|
77
|
-
let
|
|
78
|
-
let
|
|
75
|
+
let ruby = unsafe { Ruby::get_unchecked() };
|
|
76
|
+
let change_added = ruby.to_symbol("added").to_static();
|
|
77
|
+
let change_retain = ruby.to_symbol("retain").to_static();
|
|
78
|
+
let change_removed = ruby.to_symbol("removed").to_static();
|
|
79
79
|
|
|
80
80
|
// let mut error: Option<Error> = None;
|
|
81
81
|
|
|
82
|
-
let subscription_id =
|
|
83
|
-
.0
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
82
|
+
let subscription_id =
|
|
83
|
+
self.0
|
|
84
|
+
.borrow_mut()
|
|
85
|
+
.observe(move |transaction, array_event| {
|
|
86
|
+
let ruby = unsafe { Ruby::get_unchecked() };
|
|
87
|
+
let delta = array_event.delta(transaction);
|
|
88
|
+
// let mut changes = RArray::with_capacity(delta.len());
|
|
89
|
+
let (changes, errors): (Vec<_>, Vec<_>) = delta
|
|
90
|
+
.iter()
|
|
91
|
+
.map(|change| {
|
|
92
|
+
let payload = ruby.hash_new();
|
|
93
|
+
let result =
|
|
94
|
+
match change {
|
|
95
|
+
Change::Added(v) => {
|
|
96
|
+
let values = ruby.ary_new();
|
|
97
|
+
for val in v.iter() {
|
|
98
|
+
let value: Value = YValue::from(val.clone()).into();
|
|
99
|
+
values.push(value).expect("cannot push value to array");
|
|
100
|
+
}
|
|
101
|
+
payload.aset(change_added, values)
|
|
102
|
+
}
|
|
103
|
+
Change::Retain(position) => payload
|
|
104
|
+
.aset(change_retain, (*position).into_value_with(&ruby)),
|
|
105
|
+
Change::Removed(position) => payload
|
|
106
|
+
.aset(change_removed, (*position).into_value_with(&ruby)),
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
match result {
|
|
110
|
+
Ok(()) => Ok(payload),
|
|
111
|
+
Err(e) => Err(e),
|
|
99
112
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
Err(e) => Err(e),
|
|
113
|
+
})
|
|
114
|
+
.partition(Result::is_ok);
|
|
115
|
+
|
|
116
|
+
if errors.is_empty() {
|
|
117
|
+
let args_changes = ruby.ary_new();
|
|
118
|
+
for change in changes.iter() {
|
|
119
|
+
let c = *change.as_ref().unwrap();
|
|
120
|
+
args_changes
|
|
121
|
+
.push(c)
|
|
122
|
+
.expect("cannot push change event to args");
|
|
111
123
|
}
|
|
112
|
-
})
|
|
113
|
-
.partition(Result::is_ok);
|
|
114
|
-
|
|
115
|
-
if errors.is_empty() {
|
|
116
|
-
let args_changes = RArray::new();
|
|
117
|
-
for change in changes.iter() {
|
|
118
|
-
let c = *change.as_ref().unwrap();
|
|
119
|
-
args_changes
|
|
120
|
-
.push(c)
|
|
121
|
-
.expect("cannot push change event to args");
|
|
122
|
-
}
|
|
123
124
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
125
|
+
let args = (args_changes,);
|
|
126
|
+
let _ = block.call::<(RArray,), Qnil>(args);
|
|
127
|
+
// todo: make sure we respect the result and bubble up the
|
|
128
|
+
// error so that we can return as part of the Result
|
|
129
|
+
}
|
|
129
130
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
131
|
+
// todo: make sure we respect errors and let the method fail by
|
|
132
|
+
// by returning a Result containing an Error
|
|
133
|
+
})
|
|
134
|
+
.into();
|
|
134
135
|
|
|
135
136
|
Ok(subscription_id)
|
|
136
137
|
}
|
|
@@ -167,11 +168,12 @@ impl YArray {
|
|
|
167
168
|
arr.remove_range(tx, index, len)
|
|
168
169
|
}
|
|
169
170
|
pub(crate) fn yarray_to_a(&self, transaction: &YTransaction) -> RArray {
|
|
171
|
+
let ruby = unsafe { Ruby::get_unchecked() };
|
|
170
172
|
let arr = self.0.borrow();
|
|
171
173
|
let tx = transaction.transaction();
|
|
172
174
|
let tx = tx.as_ref().unwrap();
|
|
173
175
|
|
|
174
|
-
let r_arr =
|
|
176
|
+
let r_arr = ruby.ary_new();
|
|
175
177
|
for item in arr.iter(tx) {
|
|
176
178
|
let r_val = YValue::from(item);
|
|
177
179
|
let r_val = *r_val.0.borrow();
|
data/ext/yrb/src/yawareness.rs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
use magnus::{block::Proc,
|
|
1
|
+
use magnus::{block::Proc, Error, Ruby, Value};
|
|
2
2
|
use std::borrow::Borrow;
|
|
3
3
|
use std::cell::RefCell;
|
|
4
4
|
use std::collections::HashMap;
|
|
@@ -30,11 +30,15 @@ impl YAwareness {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
pub(crate) fn yawareness_apply_update(&self, update: Vec<u8>) -> Result<(), Error> {
|
|
33
|
+
let ruby = Ruby::get().unwrap();
|
|
33
34
|
AwarenessUpdate::decode_v1(update.as_slice())
|
|
34
|
-
.map_err(|_error| Error::new(
|
|
35
|
+
.map_err(|_error| Error::new(ruby.exception_runtime_error(), "cannot decode update"))
|
|
35
36
|
.and_then(|value| {
|
|
36
37
|
self.0.borrow_mut().apply_update(value).map_err(|_error| {
|
|
37
|
-
Error::new(
|
|
38
|
+
Error::new(
|
|
39
|
+
ruby.exception_runtime_error(),
|
|
40
|
+
"cannot apply awareness update",
|
|
41
|
+
)
|
|
38
42
|
})
|
|
39
43
|
})
|
|
40
44
|
}
|
|
@@ -78,13 +82,14 @@ impl YAwareness {
|
|
|
78
82
|
}
|
|
79
83
|
|
|
80
84
|
pub(crate) fn yawareness_update(&self) -> Result<Vec<u8>, Error> {
|
|
85
|
+
let ruby = Ruby::get().unwrap();
|
|
81
86
|
self.0
|
|
82
87
|
.borrow_mut()
|
|
83
88
|
.update()
|
|
84
89
|
.map(|update| update.encode_v1())
|
|
85
90
|
.map_err(|_error| {
|
|
86
91
|
Error::new(
|
|
87
|
-
|
|
92
|
+
ruby.exception_runtime_error(),
|
|
88
93
|
"cannot create update for current state",
|
|
89
94
|
)
|
|
90
95
|
})
|
|
@@ -94,13 +99,14 @@ impl YAwareness {
|
|
|
94
99
|
&self,
|
|
95
100
|
clients: Vec<ClientID>,
|
|
96
101
|
) -> Result<Vec<u8>, Error> {
|
|
102
|
+
let ruby = Ruby::get().unwrap();
|
|
97
103
|
self.0
|
|
98
104
|
.borrow_mut()
|
|
99
105
|
.update_with_clients(clients)
|
|
100
106
|
.map(|update| update.encode_v1())
|
|
101
107
|
.map_err(|_error| {
|
|
102
108
|
Error::new(
|
|
103
|
-
|
|
109
|
+
ruby.exception_runtime_error(),
|
|
104
110
|
"cannot create update for current state and given clients",
|
|
105
111
|
)
|
|
106
112
|
})
|
|
@@ -137,6 +143,7 @@ impl From<&Event> for YAwarenessEvent {
|
|
|
137
143
|
}
|
|
138
144
|
}
|
|
139
145
|
|
|
146
|
+
#[allow(dead_code)]
|
|
140
147
|
#[magnus::wrap(class = "Y::AwarenessEvent")]
|
|
141
148
|
pub(crate) struct YAwarenessSubscription(UpdateSubscription);
|
|
142
149
|
|
data/ext/yrb/src/ydiff.rs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
use magnus::{IntoValue, RHash, Value};
|
|
1
|
+
use magnus::{IntoValue, RHash, Ruby, Value};
|
|
2
2
|
|
|
3
3
|
unsafe impl Send for YDiff {}
|
|
4
4
|
|
|
@@ -14,6 +14,9 @@ impl YDiff {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
pub(crate) fn ydiff_attrs(&self) -> Option<Value> {
|
|
17
|
-
|
|
17
|
+
let ruby = unsafe { Ruby::get_unchecked() };
|
|
18
|
+
self.ydiff_attrs
|
|
19
|
+
.as_ref()
|
|
20
|
+
.map(|value| value.into_value_with(&ruby))
|
|
18
21
|
}
|
|
19
22
|
}
|
data/ext/yrb/src/ydoc.rs
CHANGED
|
@@ -6,7 +6,7 @@ use crate::yxml_fragment::YXmlFragment;
|
|
|
6
6
|
use crate::yxml_text::YXmlText;
|
|
7
7
|
use crate::YTransaction;
|
|
8
8
|
use magnus::block::Proc;
|
|
9
|
-
use magnus::{
|
|
9
|
+
use magnus::{Error, Integer, RArray, Ruby, Value};
|
|
10
10
|
use std::borrow::Borrow;
|
|
11
11
|
use std::cell::RefCell;
|
|
12
12
|
use yrs::updates::decoder::Decode;
|
|
@@ -36,12 +36,13 @@ impl YDoc {
|
|
|
36
36
|
transaction: &YTransaction,
|
|
37
37
|
state_vector: Vec<u8>,
|
|
38
38
|
) -> Result<Vec<u8>, Error> {
|
|
39
|
+
let ruby = Ruby::get().unwrap();
|
|
39
40
|
let mut tx = transaction.transaction();
|
|
40
41
|
let tx = tx.as_mut().unwrap();
|
|
41
42
|
|
|
42
43
|
StateVector::decode_v1(state_vector.borrow())
|
|
43
44
|
.map(|sv| tx.encode_diff_v1(&sv))
|
|
44
|
-
.map_err(|_e| Error::new(
|
|
45
|
+
.map_err(|_e| Error::new(ruby.exception_runtime_error(), "cannot encode diff"))
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
pub(crate) fn ydoc_encode_diff_v2(
|
|
@@ -49,6 +50,7 @@ impl YDoc {
|
|
|
49
50
|
transaction: &YTransaction,
|
|
50
51
|
state_vector: Vec<u8>,
|
|
51
52
|
) -> Result<Vec<u8>, Error> {
|
|
53
|
+
let ruby = Ruby::get().unwrap();
|
|
52
54
|
let mut tx = transaction.transaction();
|
|
53
55
|
let tx = tx.as_mut().unwrap();
|
|
54
56
|
let mut encoder = EncoderV2::new();
|
|
@@ -56,7 +58,7 @@ impl YDoc {
|
|
|
56
58
|
StateVector::decode_v2(state_vector.borrow())
|
|
57
59
|
.map(|sv| tx.encode_diff(&sv, &mut encoder))
|
|
58
60
|
.map(|_| encoder.to_vec())
|
|
59
|
-
.map_err(|_e| Error::new(
|
|
61
|
+
.map_err(|_e| Error::new(ruby.exception_runtime_error(), "cannot encode diff"))
|
|
60
62
|
}
|
|
61
63
|
|
|
62
64
|
pub(crate) fn ydoc_get_or_insert_array(&self, name: String) -> YArray {
|
|
@@ -96,11 +98,13 @@ impl YDoc {
|
|
|
96
98
|
}
|
|
97
99
|
|
|
98
100
|
pub(crate) fn ydoc_observe_update(&self, block: Proc) -> Result<SubscriptionId, Error> {
|
|
101
|
+
let ruby = Ruby::get().unwrap();
|
|
99
102
|
self.0
|
|
100
103
|
.borrow()
|
|
101
104
|
.observe_update_v1(move |_tx, update_event| {
|
|
105
|
+
let ruby = unsafe { Ruby::get_unchecked() };
|
|
102
106
|
let update = update_event.update.to_vec();
|
|
103
|
-
let update =
|
|
107
|
+
let update = ruby.ary_from_vec(update);
|
|
104
108
|
|
|
105
109
|
let args: (RArray,) = (update,);
|
|
106
110
|
block
|
|
@@ -108,6 +112,6 @@ impl YDoc {
|
|
|
108
112
|
.expect("cannot call update block");
|
|
109
113
|
})
|
|
110
114
|
.map(|v| v.into())
|
|
111
|
-
.map_err(|err| Error::new(
|
|
115
|
+
.map_err(|err| Error::new(ruby.exception_runtime_error(), err.to_string()))
|
|
112
116
|
}
|
|
113
117
|
}
|