mirror of
https://github.com/loro-dev/loro.git
synced 2025-01-22 21:07:43 +00:00
perf: avoid a few clones in wasm (#288)
This commit is contained in:
parent
1db510104e
commit
a5fce60883
2 changed files with 9 additions and 18 deletions
|
@ -333,10 +333,10 @@ pub mod wasm {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<TreeDiff> for JsValue {
|
impl From<&TreeDiff> for JsValue {
|
||||||
fn from(value: TreeDiff) -> Self {
|
fn from(value: &TreeDiff) -> Self {
|
||||||
let array = Array::new();
|
let array = Array::new();
|
||||||
for diff in value.diff.into_iter() {
|
for diff in value.diff.iter() {
|
||||||
let obj = Object::new();
|
let obj = Object::new();
|
||||||
js_sys::Reflect::set(&obj, &"target".into(), &diff.target.into()).unwrap();
|
js_sys::Reflect::set(&obj, &"target".into(), &diff.target.into()).unwrap();
|
||||||
match diff.action {
|
match diff.action {
|
||||||
|
@ -358,8 +358,8 @@ pub mod wasm {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Delta<StringSlice, StyleMeta>> for JsValue {
|
impl From<&Delta<StringSlice, StyleMeta>> for JsValue {
|
||||||
fn from(value: Delta<StringSlice, StyleMeta>) -> Self {
|
fn from(value: &Delta<StringSlice, StyleMeta>) -> Self {
|
||||||
let arr = Array::new_with_length(value.len() as u32);
|
let arr = Array::new_with_length(value.len() as u32);
|
||||||
for (i, v) in value.iter().enumerate() {
|
for (i, v) in value.iter().enumerate() {
|
||||||
arr.set(i as u32, JsValue::from(v.clone()));
|
arr.set(i as u32, JsValue::from(v.clone()));
|
||||||
|
|
|
@ -71,9 +71,7 @@ pub(crate) fn resolved_diff_to_js(value: &Diff, doc: &Arc<LoroDoc>) -> JsValue {
|
||||||
Diff::Tree(tree) => {
|
Diff::Tree(tree) => {
|
||||||
js_sys::Reflect::set(&obj, &JsValue::from_str("type"), &JsValue::from_str("tree"))
|
js_sys::Reflect::set(&obj, &JsValue::from_str("type"), &JsValue::from_str("tree"))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
js_sys::Reflect::set(&obj, &JsValue::from_str("diff"), &tree.into()).unwrap();
|
||||||
// TODO: PERF Avoid clone
|
|
||||||
js_sys::Reflect::set(&obj, &JsValue::from_str("diff"), &tree.clone().into()).unwrap();
|
|
||||||
}
|
}
|
||||||
Diff::List(list) => {
|
Diff::List(list) => {
|
||||||
// set type as "list"
|
// set type as "list"
|
||||||
|
@ -96,13 +94,7 @@ pub(crate) fn resolved_diff_to_js(value: &Diff, doc: &Arc<LoroDoc>) -> JsValue {
|
||||||
js_sys::Reflect::set(&obj, &JsValue::from_str("type"), &JsValue::from_str("text"))
|
js_sys::Reflect::set(&obj, &JsValue::from_str("type"), &JsValue::from_str("text"))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
// set diff as array
|
// set diff as array
|
||||||
// TODO: PERF Avoid clone
|
js_sys::Reflect::set(&obj, &JsValue::from_str("diff"), &JsValue::from(text)).unwrap();
|
||||||
js_sys::Reflect::set(
|
|
||||||
&obj,
|
|
||||||
&JsValue::from_str("diff"),
|
|
||||||
&JsValue::from(text.clone()),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
Diff::Map(map) => {
|
Diff::Map(map) => {
|
||||||
js_sys::Reflect::set(&obj, &JsValue::from_str("type"), &JsValue::from_str("map"))
|
js_sys::Reflect::set(&obj, &JsValue::from_str("type"), &JsValue::from_str("map"))
|
||||||
|
@ -111,8 +103,7 @@ pub(crate) fn resolved_diff_to_js(value: &Diff, doc: &Arc<LoroDoc>) -> JsValue {
|
||||||
js_sys::Reflect::set(
|
js_sys::Reflect::set(
|
||||||
&obj,
|
&obj,
|
||||||
&JsValue::from_str("updated"),
|
&JsValue::from_str("updated"),
|
||||||
// TODO: PERF Avoid clone
|
&map_delta_to_js(map, doc),
|
||||||
&map_delta_to_js(map.clone(), doc),
|
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
@ -201,7 +192,7 @@ pub fn convert(value: LoroValue) -> JsValue {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map_delta_to_js(value: ResolvedMapDelta, doc: &Arc<LoroDoc>) -> JsValue {
|
fn map_delta_to_js(value: &ResolvedMapDelta, doc: &Arc<LoroDoc>) -> JsValue {
|
||||||
let obj = Object::new();
|
let obj = Object::new();
|
||||||
for (key, value) in value.updated.iter() {
|
for (key, value) in value.updated.iter() {
|
||||||
let value = if let Some(value) = value.value.clone() {
|
let value = if let Some(value) = value.value.clone() {
|
||||||
|
|
Loading…
Reference in a new issue