y-rb 0.3.2-x86_64-linux → 0.4.1-x86_64-linux

Sign up to get free protection for your applications and to get access to all the features.
data/ext/yrb/src/ydoc.rs CHANGED
@@ -1,35 +1,78 @@
1
+ use crate::yarray::YArray;
2
+ use crate::ymap::YMap;
3
+ use crate::ytext::YText;
4
+ use crate::yxml_element::YXmlElement;
5
+ use crate::yxml_fragment::YXmlFragment;
6
+ use crate::yxml_text::YXmlText;
1
7
  use crate::YTransaction;
2
- use magnus::{Error, Integer, Value};
8
+ use magnus::{exception, Error, Integer, Value};
9
+ use std::borrow::Borrow;
3
10
  use std::cell::RefCell;
4
11
  use yrs::updates::decoder::Decode;
5
- use yrs::{Doc, OffsetKind, Options, StateVector};
12
+ use yrs::{Doc, OffsetKind, Options, ReadTxn, StateVector, Transact};
6
13
 
7
14
  #[magnus::wrap(class = "Y::Doc")]
8
15
  pub(crate) struct YDoc(pub(crate) RefCell<Doc>);
9
16
 
17
+ unsafe impl Send for YDoc {}
18
+
10
19
  impl YDoc {
11
20
  pub(crate) fn ydoc_new(client_id: &[Value]) -> Self {
12
21
  let mut options = Options::default();
13
-
14
22
  if client_id.len() == 1 {
15
23
  let value = client_id.first().unwrap();
16
24
  options.client_id = Integer::from_value(*value).unwrap().to_u64().unwrap();
17
25
  }
18
-
19
26
  options.offset_kind = OffsetKind::Utf32;
20
27
 
21
28
  let doc = Doc::with_options(options);
22
29
  Self(RefCell::new(doc))
23
30
  }
24
31
 
25
- pub(crate) fn ydoc_transact(&self) -> YTransaction {
26
- let transaction = self.0.borrow().transact();
32
+ pub(crate) fn ydoc_encode_diff_v1(
33
+ &self,
34
+ transaction: &YTransaction,
35
+ state_vector: Vec<u8>,
36
+ ) -> Result<Vec<u8>, Error> {
37
+ let mut tx = transaction.transaction();
38
+ let tx = tx.as_mut().unwrap();
39
+
40
+ StateVector::decode_v1(state_vector.borrow())
41
+ .map(|sv| tx.encode_diff_v1(&sv))
42
+ .map_err(|_e| Error::new(exception::runtime_error(), "cannot encode diff"))
43
+ }
44
+
45
+ pub(crate) fn ydoc_get_or_insert_array(&self, name: String) -> YArray {
46
+ self.0.borrow().get_or_insert_array(name.as_str()).into()
47
+ }
48
+
49
+ pub(crate) fn ydoc_get_or_insert_map(&self, name: String) -> YMap {
50
+ self.0.borrow().get_or_insert_map(name.as_str()).into()
51
+ }
52
+
53
+ pub(crate) fn ydoc_get_or_insert_text(&self, name: String) -> YText {
54
+ self.0.borrow().get_or_insert_text(name.as_str()).into()
55
+ }
56
+
57
+ pub(crate) fn ydoc_get_or_insert_xml_element(&self, name: String) -> YXmlElement {
58
+ let xml_element_ref = self.0.borrow_mut().get_or_insert_xml_element(name.as_str());
59
+ YXmlElement::from(xml_element_ref) // ::into() maps to YXmlFragment instead of YXmlElement :-(
60
+ }
61
+
62
+ pub(crate) fn ydoc_get_or_insert_xml_fragment(&self, name: String) -> YXmlFragment {
63
+ self.0
64
+ .borrow()
65
+ .get_or_insert_xml_fragment(name.as_str())
66
+ .into()
67
+ }
27
68
 
28
- YTransaction(RefCell::new(transaction))
69
+ pub(crate) fn ydoc_get_or_insert_xml_text(&self, name: String) -> YXmlText {
70
+ self.0.borrow().get_or_insert_xml_text(name.as_str()).into()
29
71
  }
30
- pub(crate) fn ydoc_encode_diff_v1(&self, state_vector: Vec<u8>) -> Result<Vec<u8>, Error> {
31
- StateVector::decode_v1(&*state_vector)
32
- .map(|sv| self.0.borrow().encode_state_as_update_v1(&sv))
33
- .map_err(|_e| Error::runtime_error("cannot encode diff"))
72
+
73
+ pub(crate) fn ydoc_transact<'doc>(&self) -> YTransaction {
74
+ let doc = self.0.borrow();
75
+ let transaction = doc.transact_mut();
76
+ YTransaction::from(transaction)
34
77
  }
35
78
  }
data/ext/yrb/src/ymap.rs CHANGED
@@ -3,38 +3,50 @@ use crate::yvalue::YValue;
3
3
  use crate::YTransaction;
4
4
  use lib0::any::Any;
5
5
  use magnus::block::Proc;
6
- use magnus::{Error, RArray, RHash, Symbol, Value};
6
+ use magnus::{exception, Error, RArray, RHash, Symbol, Value};
7
7
  use std::cell::RefCell;
8
8
  use yrs::types::{EntryChange, Value as YrsValue};
9
- use yrs::Map;
9
+ use yrs::{Map, MapRef, Observable};
10
10
 
11
11
  #[magnus::wrap(class = "Y::Map")]
12
- pub(crate) struct YMap(pub(crate) RefCell<Map>);
12
+ pub(crate) struct YMap(pub(crate) RefCell<MapRef>);
13
13
 
14
14
  /// SAFETY: This is safe because we only access this data when the GVL is held.
15
15
  unsafe impl Send for YMap {}
16
16
 
17
17
  impl YMap {
18
18
  pub(crate) fn ymap_clear(&self, transaction: &YTransaction) {
19
- self.0.borrow_mut().clear(&mut *transaction.0.borrow_mut());
19
+ let mut tx = transaction.transaction();
20
+ let tx = tx.as_mut().unwrap();
21
+
22
+ self.0.borrow_mut().clear(tx)
20
23
  }
21
- pub(crate) fn ymap_contains(&self, key: Value) -> bool {
24
+ pub(crate) fn ymap_contains(&self, transaction: &YTransaction, key: Value) -> bool {
25
+ let tx = transaction.transaction();
26
+ let tx = tx.as_ref().unwrap();
27
+
22
28
  match indifferent_hash_key(key) {
23
29
  None => false,
24
- Some(k) => self.0.borrow().contains(&*k),
30
+ Some(k) => self.0.borrow().contains(tx, k.as_str()),
25
31
  }
26
32
  }
27
- pub(crate) fn ymap_each(&self, proc: Proc) {
28
- self.0.borrow().iter().for_each(|(key, val)| {
33
+ pub(crate) fn ymap_each(&self, transaction: &YTransaction, proc: Proc) {
34
+ let tx = transaction.transaction();
35
+ let tx = tx.as_ref().unwrap();
36
+
37
+ self.0.borrow().iter(tx).for_each(|(key, val)| {
29
38
  let k = key.to_string();
30
39
  let v = *YValue::from(val).0.borrow();
31
40
  proc.call::<(String, Value), Value>((k, v))
32
41
  .expect("cannot iterate map");
33
- });
42
+ })
34
43
  }
35
- pub(crate) fn ymap_get(&self, key: Value) -> Option<Value> {
44
+ pub(crate) fn ymap_get(&self, transaction: &YTransaction, key: Value) -> Option<Value> {
45
+ let tx = transaction.transaction();
46
+ let tx = tx.as_ref().unwrap();
47
+
36
48
  indifferent_hash_key(key)
37
- .map(|k| self.0.borrow().get(&*k))
49
+ .map(|k| self.0.borrow().get(tx, k.as_str()))
38
50
  .map(|v| v.unwrap_or(YrsValue::Any(Any::Undefined)))
39
51
  .map(|v| *YValue::from(v).0.borrow())
40
52
  }
@@ -44,15 +56,17 @@ impl YMap {
44
56
  key: Value,
45
57
  value: Value,
46
58
  ) -> Result<(), Error> {
59
+ let mut tx = transaction.transaction();
60
+ let tx = tx.as_mut().unwrap();
61
+
47
62
  match indifferent_hash_key(key) {
48
- None => Err(Error::runtime_error(
49
- "invalid key type, make sure it is either a Symbol or a String",
63
+ None => Err(Error::new(
64
+ exception::runtime_error(),
65
+ "invalid key type, make sure it is either of type Symbol or String",
50
66
  )),
51
67
  Some(k) => {
52
68
  let v = Any::from(YValue::from(value));
53
- self.0
54
- .borrow_mut()
55
- .insert(&mut *transaction.0.borrow_mut(), k, v);
69
+ self.0.borrow_mut().insert(tx, k, v);
56
70
 
57
71
  Ok(())
58
72
  }
@@ -62,7 +76,6 @@ impl YMap {
62
76
  let change_inserted = Symbol::new("inserted").as_static();
63
77
  let change_updated = Symbol::new("updated").as_static();
64
78
  let change_removed = Symbol::new("removed").as_static();
65
-
66
79
  self.0
67
80
  .borrow_mut()
68
81
  .observe(move |transaction, map_event| {
@@ -125,23 +138,28 @@ impl YMap {
125
138
  .into()
126
139
  }
127
140
  pub(crate) fn ymap_remove(&self, transaction: &YTransaction, key: Value) -> Option<Value> {
141
+ let mut tx = transaction.transaction();
142
+ let tx = tx.as_mut().unwrap();
143
+
128
144
  indifferent_hash_key(key)
129
- .map(|k| {
130
- self.0
131
- .borrow()
132
- .remove(&mut *transaction.0.borrow_mut(), &*k)
133
- })
145
+ .map(|k| self.0.borrow().remove(tx, k.as_str()))
134
146
  .map(|v| v.unwrap_or(YrsValue::Any(Any::Undefined)))
135
147
  .map(|v| *YValue::from(v).0.borrow())
136
148
  }
137
- pub(crate) fn ymap_size(&self) -> u32 {
138
- self.0.borrow().len()
149
+ pub(crate) fn ymap_size(&self, transaction: &YTransaction) -> u32 {
150
+ let tx = transaction.transaction();
151
+ let tx = tx.as_ref().unwrap();
152
+
153
+ self.0.borrow().len(tx)
139
154
  }
140
- pub(crate) fn ymap_to_h(&self) -> RHash {
155
+ pub(crate) fn ymap_to_h(&self, transaction: &YTransaction) -> RHash {
156
+ let tx = transaction.transaction();
157
+ let tx = tx.as_ref().unwrap();
158
+
141
159
  RHash::from_iter(
142
160
  self.0
143
161
  .borrow()
144
- .iter()
162
+ .iter(tx)
145
163
  .map(move |(k, v)| (k.to_string(), *YValue::from(v).0.borrow())),
146
164
  )
147
165
  }
@@ -149,3 +167,9 @@ impl YMap {
149
167
  self.0.borrow_mut().unobserve(subscription_id);
150
168
  }
151
169
  }
170
+
171
+ impl From<MapRef> for YMap {
172
+ fn from(v: MapRef) -> Self {
173
+ YMap(RefCell::from(v))
174
+ }
175
+ }
data/ext/yrb/src/ytext.rs CHANGED
@@ -7,10 +7,10 @@ use magnus::value::Qnil;
7
7
  use magnus::{Error, RHash, Symbol, Value};
8
8
  use std::cell::RefCell;
9
9
  use yrs::types::Delta;
10
- use yrs::Text;
10
+ use yrs::{GetString, Observable, Text, TextRef};
11
11
 
12
12
  #[magnus::wrap(class = "Y::Text")]
13
- pub(crate) struct YText(pub(crate) RefCell<Text>);
13
+ pub(crate) struct YText(pub(crate) RefCell<TextRef>);
14
14
 
15
15
  /// SAFETY: This is safe because we only access this data when the GVL is held.
16
16
  unsafe impl Send for YText {}
@@ -22,40 +22,33 @@ impl YText {
22
22
  index: u32,
23
23
  length: u32,
24
24
  attrs: RHash,
25
- ) -> Result<(), Error> {
25
+ ) {
26
+ let mut tx = transaction.transaction();
27
+ let tx = tx.as_mut().unwrap();
28
+
26
29
  let a = YAttrs::from(attrs);
27
- self.0
28
- .borrow_mut()
29
- .format(&mut *transaction.0.borrow_mut(), index, length, a.0);
30
30
 
31
- Ok(())
31
+ self.0.borrow_mut().format(tx, index, length, a.0)
32
32
  }
33
- pub(crate) fn ytext_insert(
34
- &self,
35
- transaction: &YTransaction,
36
- index: u32,
37
- chunk: String,
38
- ) -> Result<(), Error> {
39
- self.0
40
- .borrow_mut()
41
- .insert(&mut *transaction.0.borrow_mut(), index, &*chunk);
33
+ pub(crate) fn ytext_insert(&self, transaction: &YTransaction, index: u32, chunk: String) {
34
+ let mut tx = transaction.transaction();
35
+ let tx = tx.as_mut().unwrap();
42
36
 
43
- Ok(())
37
+ self.0.borrow_mut().insert(tx, index, chunk.as_str())
44
38
  }
45
39
  pub(crate) fn ytext_insert_embed(
46
40
  &self,
47
41
  transaction: &YTransaction,
48
42
  index: u32,
49
43
  content: Value,
50
- ) -> Result<(), Error> {
44
+ ) {
45
+ let mut tx = transaction.transaction();
46
+ let tx = tx.as_mut().unwrap();
47
+
51
48
  let yvalue = YValue::from(content);
52
49
  let avalue = Any::from(yvalue);
53
50
 
54
- self.0
55
- .borrow_mut()
56
- .insert_embed(&mut *transaction.0.borrow_mut(), index, avalue);
57
-
58
- Ok(())
51
+ self.0.borrow_mut().insert_embed(tx, index, avalue)
59
52
  }
60
53
  pub(crate) fn ytext_insert_embed_with_attributes(
61
54
  &self,
@@ -63,20 +56,18 @@ impl YText {
63
56
  index: u32,
64
57
  embed: Value,
65
58
  attrs: RHash,
66
- ) -> Result<(), Error> {
59
+ ) {
60
+ let mut tx = transaction.transaction();
61
+ let tx = tx.as_mut().unwrap();
62
+
67
63
  let yvalue = YValue::from(embed);
68
64
  let avalue = Any::from(yvalue);
69
65
 
70
66
  let a = YAttrs::from(attrs);
71
67
 
72
- self.0.borrow_mut().insert_embed_with_attributes(
73
- &mut *transaction.0.borrow_mut(),
74
- index,
75
- avalue,
76
- a.0,
77
- );
78
-
79
- Ok(())
68
+ self.0
69
+ .borrow_mut()
70
+ .insert_embed_with_attributes(tx, index, avalue, a.0)
80
71
  }
81
72
  pub(crate) fn ytext_insert_with_attributes(
82
73
  &self,
@@ -84,20 +75,21 @@ impl YText {
84
75
  index: u32,
85
76
  chunk: String,
86
77
  attrs: RHash,
87
- ) -> Result<(), Error> {
88
- let a = YAttrs::from(attrs);
78
+ ) {
79
+ let mut tx = transaction.transaction();
80
+ let tx = tx.as_mut().unwrap();
89
81
 
90
- self.0.borrow_mut().insert_with_attributes(
91
- &mut *transaction.0.borrow_mut(),
92
- index,
93
- &*chunk,
94
- a.0,
95
- );
82
+ let a = YAttrs::from(attrs);
96
83
 
97
- Ok(())
84
+ self.0
85
+ .borrow_mut()
86
+ .insert_with_attributes(tx, index, chunk.as_str(), a.0)
98
87
  }
99
- pub(crate) fn ytext_length(&self) -> u32 {
100
- self.0.borrow().len()
88
+ pub(crate) fn ytext_length(&self, transaction: &YTransaction) -> u32 {
89
+ let tx = transaction.transaction();
90
+ let tx = tx.as_ref().unwrap();
91
+
92
+ self.0.borrow().len(tx)
101
93
  }
102
94
  pub(crate) fn ytext_observe(&self, block: Proc) -> Result<u32, Error> {
103
95
  let delta_insert = Symbol::new("insert").to_static();
@@ -106,7 +98,6 @@ impl YText {
106
98
  let attributes = Symbol::new("attributes").to_static();
107
99
 
108
100
  // let mut error: Option<Error> = None;
109
-
110
101
  let subscription_id = self
111
102
  .0
112
103
  .borrow_mut()
@@ -179,26 +170,30 @@ impl YText {
179
170
  Ok(subscription_id)
180
171
  }
181
172
  pub(crate) fn ytext_push(&self, transaction: &YTransaction, chunk: String) {
182
- self.0
183
- .borrow_mut()
184
- .push(&mut *transaction.0.borrow_mut(), &*chunk);
173
+ let mut tx = transaction.transaction();
174
+ let tx = tx.as_mut().unwrap();
175
+
176
+ self.0.borrow_mut().push(tx, chunk.as_str())
185
177
  }
186
- pub(crate) fn ytext_remove_range(
187
- &self,
188
- transaction: &YTransaction,
189
- start: u32,
190
- length: u32,
191
- ) -> Result<(), Error> {
192
- self.0
193
- .borrow_mut()
194
- .remove_range(&mut *transaction.0.borrow_mut(), start, length);
178
+ pub(crate) fn ytext_remove_range(&self, transaction: &YTransaction, start: u32, length: u32) {
179
+ let mut tx = transaction.transaction();
180
+ let tx = tx.as_mut().unwrap();
195
181
 
196
- Ok(())
182
+ self.0.borrow_mut().remove_range(tx, start, length)
197
183
  }
198
- pub(crate) fn ytext_to_s(&self) -> String {
199
- return self.0.borrow().to_string();
184
+ pub(crate) fn ytext_to_s(&self, transaction: &YTransaction) -> String {
185
+ let mut tx = transaction.transaction();
186
+ let tx = tx.as_mut().unwrap();
187
+
188
+ self.0.borrow().get_string(tx)
200
189
  }
201
190
  pub(crate) fn ytext_unobserve(&self, subscription_id: u32) {
202
- return self.0.borrow_mut().unobserve(subscription_id);
191
+ self.0.borrow_mut().unobserve(subscription_id);
192
+ }
193
+ }
194
+
195
+ impl From<TextRef> for YText {
196
+ fn from(v: TextRef) -> Self {
197
+ YText(RefCell::from(v))
203
198
  }
204
199
  }
@@ -2,54 +2,107 @@ use crate::yarray::YArray;
2
2
  use crate::ymap::YMap;
3
3
  use crate::ytext::YText;
4
4
  use crate::yxml_element::YXmlElement;
5
+ use crate::yxml_fragment::YXmlFragment;
5
6
  use crate::yxml_text::YXmlText;
6
- use magnus::Error;
7
- use std::cell::RefCell;
7
+ use magnus::{exception, Error};
8
+ use std::cell::{RefCell, RefMut};
8
9
  use yrs::updates::decoder::Decode;
9
10
  use yrs::updates::encoder::Encode;
10
- use yrs::{Transaction, Update};
11
+ use yrs::{ReadTxn, TransactionMut, Update};
11
12
 
12
13
  #[magnus::wrap(class = "Y::Transaction")]
13
- pub(crate) struct YTransaction(pub(crate) RefCell<Transaction>);
14
+ pub(crate) struct YTransaction(pub(crate) RefCell<Option<TransactionMut<'static>>>);
14
15
 
15
16
  /// SAFETY: This is safe because we only access this data when the GVL is held.
16
17
  unsafe impl Send for YTransaction {}
17
18
 
19
+ impl YTransaction {}
20
+
21
+ impl<'doc> From<TransactionMut<'doc>> for YTransaction {
22
+ fn from(txn: TransactionMut<'doc>) -> Self {
23
+ let txn: TransactionMut<'static> = unsafe { std::mem::transmute(txn) };
24
+ YTransaction(RefCell::from(Some(txn)))
25
+ }
26
+ }
27
+
28
+ // API which is eventually publicly exposed
18
29
  impl YTransaction {
19
30
  pub(crate) fn ytransaction_apply_update(&self, update: Vec<u8>) -> Result<(), Error> {
20
- return Update::decode_v1(update.as_slice())
21
- .map(|u| self.0.borrow_mut().apply_update(u))
22
- .map_err(|_e| Error::runtime_error("cannot apply update"));
31
+ Update::decode_v1(update.as_slice())
32
+ .map_err(|error| {
33
+ Error::new(
34
+ exception::runtime_error(),
35
+ format!("cannot decode update: {:?}", error),
36
+ )
37
+ })
38
+ .map(|u| self.transaction().as_mut().unwrap().apply_update(u))
23
39
  }
40
+
24
41
  pub(crate) fn ytransaction_commit(&self) {
25
- self.0.borrow_mut().commit();
42
+ self.transaction().as_mut().unwrap().commit();
43
+ }
44
+
45
+ pub(crate) fn ytransaction_get_array(&self, name: String) -> Option<YArray> {
46
+ self.transaction()
47
+ .as_ref()
48
+ .unwrap()
49
+ .get_array(name.as_str())
50
+ .map(YArray::from)
26
51
  }
27
- pub(crate) fn ytransaction_get_array(&self, name: String) -> YArray {
28
- let a = self.0.borrow_mut().get_array(&*name);
29
52
 
30
- YArray(RefCell::from(a))
53
+ pub(crate) fn ytransaction_get_map(&self, name: String) -> Option<YMap> {
54
+ self.transaction()
55
+ .as_ref()
56
+ .unwrap()
57
+ .get_map(name.as_str())
58
+ .map(YMap::from)
31
59
  }
32
- pub(crate) fn ytransaction_get_map(&self, name: String) -> YMap {
33
- let m = self.0.borrow_mut().get_map(&*name);
34
60
 
35
- YMap(RefCell::from(m))
61
+ pub(crate) fn ytransaction_get_text(&self, name: String) -> Option<YText> {
62
+ self.transaction()
63
+ .as_ref()
64
+ .unwrap()
65
+ .get_text(name.as_str())
66
+ .map(YText::from)
36
67
  }
37
- pub(crate) fn ytransaction_get_text(&self, name: String) -> YText {
38
- let t = self.0.borrow_mut().get_text(&*name);
39
68
 
40
- YText(RefCell::new(t))
69
+ pub(crate) fn ytransaction_get_xml_element(&self, name: String) -> Option<YXmlElement> {
70
+ self.transaction()
71
+ .as_ref()
72
+ .unwrap()
73
+ .get_xml_element(name.as_str())
74
+ .map(YXmlElement::from)
41
75
  }
42
- pub(crate) fn ytransaction_get_xml_element(&self, name: String) -> YXmlElement {
43
- let el = self.0.borrow_mut().get_xml_element(&*name);
44
76
 
45
- YXmlElement(RefCell::new(el))
77
+ pub(crate) fn ytransaction_get_xml_fragment(&self, name: String) -> Option<YXmlFragment> {
78
+ self.transaction()
79
+ .as_ref()
80
+ .unwrap()
81
+ .get_xml_fragment(name.as_str())
82
+ .map(YXmlFragment::from)
46
83
  }
47
- pub(crate) fn ytransaction_get_xml_text(&self, name: String) -> YXmlText {
48
- let t = self.0.borrow_mut().get_xml_text(&*name);
49
84
 
50
- YXmlText(RefCell::new(t))
85
+ pub(crate) fn ytransaction_get_xml_text(&self, name: String) -> Option<YXmlText> {
86
+ self.transaction()
87
+ .as_ref()
88
+ .unwrap()
89
+ .get_xml_text(name.as_str())
90
+ .map(YXmlText::from)
51
91
  }
92
+
52
93
  pub(crate) fn ytransaction_state_vector(&self) -> Vec<u8> {
53
- return self.0.borrow_mut().state_vector().encode_v1();
94
+ self.transaction()
95
+ .as_ref()
96
+ .unwrap()
97
+ .state_vector()
98
+ .encode_v1()
99
+ }
100
+
101
+ pub(crate) fn ytransaction_free(&self) {
102
+ self.0.replace(None);
103
+ }
104
+
105
+ pub(crate) fn transaction(&self) -> RefMut<'_, Option<TransactionMut<'static>>> {
106
+ self.0.borrow_mut()
54
107
  }
55
108
  }
@@ -6,7 +6,10 @@ use magnus::{class, Float, Integer, RArray, RHash, RString, Symbol, Value, QNIL}
6
6
  use std::cell::RefCell;
7
7
  use std::collections::HashMap;
8
8
  use yrs::types::Value as YrsValue;
9
- use yrs::{Text as YrsText, XmlElement as YrsXmlElement, XmlText as YrsXmlText};
9
+ use yrs::{
10
+ Array, Map, TextRef as YrsText, Transact, XmlElementRef as YrsXmlElement,
11
+ XmlTextRef as YrsXmlText,
12
+ };
10
13
 
11
14
  pub(crate) struct YValue(pub(crate) RefCell<Value>);
12
15
 
@@ -141,12 +144,28 @@ impl From<YrsValue> for YValue {
141
144
  YrsValue::YText(text) => YValue::from(text),
142
145
  YrsValue::YXmlElement(el) => YValue::from(el),
143
146
  YrsValue::YXmlText(text) => YValue::from(text),
144
- // YrsValue::YArray(val) => YValue::from(RArray::from_vec(val.iter().map(|item| {
145
- // let yvalue = YValue::from(item);
146
- // *yvalue.0
147
- // }))),
148
- // YrsValue::YMap(val) => YValue::from(RHash::from_iter(val.iter())),
149
- v => panic!("cannot map complex yrs values to yvalue: {}", v.to_string()),
147
+ YrsValue::YArray(val) => {
148
+ let tx = val.transact();
149
+ let arr = RArray::from_vec(
150
+ val.iter(&tx)
151
+ .map(|item| {
152
+ let yvalue = YValue::from(item);
153
+ yvalue.0.into_inner()
154
+ })
155
+ .collect(),
156
+ );
157
+ YValue::from(arr)
158
+ }
159
+ YrsValue::YMap(val) => {
160
+ let tx = val.transact();
161
+ let iter = val.iter(&tx).map(|(key, val)| {
162
+ let val = YValue::from(val);
163
+ let val = val.0.into_inner();
164
+ (key, val)
165
+ });
166
+ YValue::from(RHash::from_iter(iter))
167
+ }
168
+ v => panic!("cannot map complex yrs values to yvalue: {:?}", v),
150
169
  }
151
170
  }
152
171
  }