Skip to content

Commit

Permalink
Fixes message send from wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
codenikel committed Dec 16, 2023
1 parent 8aaa3ae commit 56383e6
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 93 deletions.
3 changes: 2 additions & 1 deletion crates/collaboration/src/collaborator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl<'a, N: 'static + RadiantNode + serde::de::DeserializeOwned> Collaborator<N>
event.keys(txn).iter().for_each(|(key, change)| {
match change {
EntryChange::Inserted(val) => {
log::error!("inserted");
let id = Uuid::parse_str(key).unwrap();
let node: String = val.clone().cast().unwrap();
let mut node: N = serde_json::from_str(&node).unwrap();
Expand All @@ -46,7 +47,7 @@ impl<'a, N: 'static + RadiantNode + serde::de::DeserializeOwned> Collaborator<N>

},
EntryChange::Updated(_old, _new) => {

log::error!("updated");
}
}
});
Expand Down
25 changes: 25 additions & 0 deletions crates/collaboration/src/wasm_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use yrs::encoding::read::Cursor;
use web_sys::{MessageEvent, WebSocket};
use wasm_bindgen::prelude::*;
use wasm_bindgen::closure::Closure;
use yrs::UpdateSubscription;

#[derive(Debug)]
pub struct Connection {
Expand Down Expand Up @@ -192,14 +193,36 @@ pub fn handle_msg<P: Protocol>(
pub struct WasmConnection {
awareness: Arc<RwLock<Awareness>>,
connection: Option<Connection>,
_sub: UpdateSubscription,
}

impl WasmConnection {
pub fn new(awareness: Arc<RwLock<Awareness>>, url: &str) -> Result<Arc<RwLock<Self>>, ()> {
if let Ok(ws) = WebSocket::new(url) {
let sub = {
let a = awareness.write().unwrap();
let doc = a.doc();
let cloned_ws = ws.clone();
doc.observe_update_v1(move |_txn, e| {
log::error!("sending update");
let update = e.update.to_owned();
let msg =
y_sync::sync::Message::Sync(y_sync::sync::SyncMessage::Update(update))
.encode_v1();
if let Err(e) = cloned_ws.send_with_u8_array(&msg) {
log::error!("connection failed to send back the reply {:?}", e);
} else {
// console_log!("connection send back the reply");
// return Err(Error::Unsupported(2)); // parent ConnHandler has been dropped
}
})
.unwrap()
};

let wasm_connection = Arc::new(RwLock::new(WasmConnection {
awareness: awareness.clone(),
connection: None,
_sub: sub,
}));

let cloned_wrapper = wasm_connection.clone();
Expand All @@ -216,6 +239,8 @@ impl WasmConnection {
ws.set_onopen(Some(onopen_callback.as_ref().unchecked_ref()));
onopen_callback.forget();



Ok(wasm_connection)
} else {
return Err(());
Expand Down
4 changes: 2 additions & 2 deletions runtime/pkg/radiantkit.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ export interface InitOutput {
readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
readonly __wbindgen_export_2: WebAssembly.Table;
readonly _dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h20324fb4a9880f17: (a: number, b: number) => void;
readonly _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h0077550e1d4ebdc0: (a: number, b: number, c: number) => void;
readonly wasm_bindgen__convert__closures__invoke0_mut__h0b630077be010e86: (a: number, b: number) => void;
readonly wasm_bindgen__convert__closures__invoke1_mut__h00644ecee464bd99: (a: number, b: number, c: number) => void;
readonly wasm_bindgen__convert__closures__invoke1_mut__hb42503db276de2ca: (a: number, b: number, c: number) => void;
readonly wasm_bindgen__convert__closures__invoke0_mut__h259ebfa3249daa59: (a: number, b: number) => void;
readonly _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h5234d6fbfa4fe934: (a: number, b: number, c: number) => void;
Expand Down
176 changes: 88 additions & 88 deletions runtime/pkg/radiantkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,6 @@ function takeObject(idx) {
return ret;
}

function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];

heap[idx] = obj;
return idx;
}

function isLikeNone(x) {
return x === undefined || x === null;
}

let cachedFloat64Memory0 = null;

function getFloat64Memory0() {
if (cachedFloat64Memory0 === null || cachedFloat64Memory0.byteLength === 0) {
cachedFloat64Memory0 = new Float64Array(wasm.memory.buffer);
}
return cachedFloat64Memory0;
}

let cachedInt32Memory0 = null;

function getInt32Memory0() {
if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
}

let WASM_VECTOR_LEN = 0;

let cachedUint8Memory0 = null;
Expand Down Expand Up @@ -115,6 +84,37 @@ function passStringToWasm0(arg, malloc, realloc) {
return ptr;
}

function isLikeNone(x) {
return x === undefined || x === null;
}

let cachedInt32Memory0 = null;

function getInt32Memory0() {
if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
}

let cachedFloat64Memory0 = null;

function getFloat64Memory0() {
if (cachedFloat64Memory0 === null || cachedFloat64Memory0.byteLength === 0) {
cachedFloat64Memory0 = new Float64Array(wasm.memory.buffer);
}
return cachedFloat64Memory0;
}

function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];

heap[idx] = obj;
return idx;
}

const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } );

if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); };
Expand Down Expand Up @@ -214,11 +214,11 @@ function makeMutClosure(arg0, arg1, dtor, f) {
return real;
}
function __wbg_adapter_38(arg0, arg1) {
wasm._dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h20324fb4a9880f17(arg0, arg1);
wasm.wasm_bindgen__convert__closures__invoke0_mut__h0b630077be010e86(arg0, arg1);
}

function __wbg_adapter_41(arg0, arg1, arg2) {
wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h0077550e1d4ebdc0(arg0, arg1, addHeapObject(arg2));
wasm.wasm_bindgen__convert__closures__invoke1_mut__h00644ecee464bd99(arg0, arg1, addHeapObject(arg2));
}

function __wbg_adapter_58(arg0, arg1, arg2) {
Expand Down Expand Up @@ -605,29 +605,6 @@ function __wbg_get_imports() {
imports.wbg.__wbindgen_object_drop_ref = function(arg0) {
takeObject(arg0);
};
imports.wbg.__wbindgen_object_clone_ref = function(arg0) {
const ret = getObject(arg0);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_cb_drop = function(arg0) {
const obj = takeObject(arg0).original;
if (obj.cnt-- == 1) {
obj.a = 0;
return true;
}
const ret = false;
return ret;
};
imports.wbg.__wbindgen_is_string = function(arg0) {
const ret = typeof(getObject(arg0)) === 'string';
return ret;
};
imports.wbg.__wbindgen_number_get = function(arg0, arg1) {
const obj = getObject(arg1);
const ret = typeof(obj) === 'number' ? obj : undefined;
getFloat64Memory0()[arg0 / 8 + 1] = isLikeNone(ret) ? 0 : ret;
getInt32Memory0()[arg0 / 4 + 0] = !isLikeNone(ret);
};
imports.wbg.__wbindgen_string_get = function(arg0, arg1) {
const obj = getObject(arg1);
const ret = typeof(obj) === 'string' ? obj : undefined;
Expand All @@ -636,6 +613,16 @@ function __wbg_get_imports() {
getInt32Memory0()[arg0 / 4 + 1] = len1;
getInt32Memory0()[arg0 / 4 + 0] = ptr1;
};
imports.wbg.__wbindgen_number_get = function(arg0, arg1) {
const obj = getObject(arg1);
const ret = typeof(obj) === 'number' ? obj : undefined;
getFloat64Memory0()[arg0 / 8 + 1] = isLikeNone(ret) ? 0 : ret;
getInt32Memory0()[arg0 / 4 + 0] = !isLikeNone(ret);
};
imports.wbg.__wbindgen_is_string = function(arg0) {
const ret = typeof(getObject(arg0)) === 'string';
return ret;
};
imports.wbg.__wbindgen_is_object = function(arg0) {
const val = getObject(arg0);
const ret = typeof(val) === 'object' && val !== null;
Expand All @@ -649,6 +636,23 @@ function __wbg_get_imports() {
const ret = getObject(arg0) in getObject(arg1);
return ret;
};
imports.wbg.__wbindgen_object_clone_ref = function(arg0) {
const ret = getObject(arg0);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_error_new = function(arg0, arg1) {
const ret = new Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
};
imports.wbg.__wbindgen_cb_drop = function(arg0) {
const obj = takeObject(arg0).original;
if (obj.cnt-- == 1) {
obj.a = 0;
return true;
}
const ret = false;
return ret;
};
imports.wbg.__wbg_radiantkitappcontroller_new = function(arg0) {
const ret = RadiantKitAppController.__wrap(arg0);
return addHeapObject(ret);
Expand All @@ -657,10 +661,6 @@ function __wbg_get_imports() {
const ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_error_new = function(arg0, arg1) {
const ret = new Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
};
imports.wbg.__wbg_new_abda76e883ba8a5f = function() {
const ret = new Error();
return addHeapObject(ret);
Expand Down Expand Up @@ -2162,60 +2162,60 @@ function __wbg_get_imports() {
const ret = wasm.memory;
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper902 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 207, __wbg_adapter_38);
imports.wbg.__wbindgen_closure_wrapper671 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 91, __wbg_adapter_38);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper904 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 207, __wbg_adapter_41);
imports.wbg.__wbindgen_closure_wrapper673 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 91, __wbg_adapter_41);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper906 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 207, __wbg_adapter_41);
imports.wbg.__wbindgen_closure_wrapper675 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 91, __wbg_adapter_41);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper908 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 207, __wbg_adapter_41);
imports.wbg.__wbindgen_closure_wrapper677 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 91, __wbg_adapter_41);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper910 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 207, __wbg_adapter_41);
imports.wbg.__wbindgen_closure_wrapper679 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 91, __wbg_adapter_41);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper912 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 207, __wbg_adapter_41);
imports.wbg.__wbindgen_closure_wrapper681 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 91, __wbg_adapter_41);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper914 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 207, __wbg_adapter_41);
imports.wbg.__wbindgen_closure_wrapper683 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 91, __wbg_adapter_41);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper916 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 207, __wbg_adapter_41);
imports.wbg.__wbindgen_closure_wrapper685 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 91, __wbg_adapter_41);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper918 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 207, __wbg_adapter_41);
imports.wbg.__wbindgen_closure_wrapper687 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 91, __wbg_adapter_41);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper1127 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 296, __wbg_adapter_58);
imports.wbg.__wbindgen_closure_wrapper1117 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 302, __wbg_adapter_58);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper1128 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 296, __wbg_adapter_58);
imports.wbg.__wbindgen_closure_wrapper1118 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 302, __wbg_adapter_58);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper1129 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 296, __wbg_adapter_63);
imports.wbg.__wbindgen_closure_wrapper1119 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 302, __wbg_adapter_63);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper1892 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 549, __wbg_adapter_66);
imports.wbg.__wbindgen_closure_wrapper1896 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 559, __wbg_adapter_66);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_closure_wrapper3455 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 1150, __wbg_adapter_69);
imports.wbg.__wbindgen_closure_wrapper3458 = function(arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 1160, __wbg_adapter_69);
return addHeapObject(ret);
};

Expand Down
Binary file modified runtime/pkg/radiantkit_bg.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions runtime/pkg/radiantkit_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export function wgpu_render_pass_set_index_buffer(a: number, b: number, c: numbe
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
export const __wbindgen_export_2: WebAssembly.Table;
export function _dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h20324fb4a9880f17(a: number, b: number): void;
export function _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h0077550e1d4ebdc0(a: number, b: number, c: number): void;
export function wasm_bindgen__convert__closures__invoke0_mut__h0b630077be010e86(a: number, b: number): void;
export function wasm_bindgen__convert__closures__invoke1_mut__h00644ecee464bd99(a: number, b: number, c: number): void;
export function wasm_bindgen__convert__closures__invoke1_mut__hb42503db276de2ca(a: number, b: number, c: number): void;
export function wasm_bindgen__convert__closures__invoke0_mut__h259ebfa3249daa59(a: number, b: number): void;
export function _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h5234d6fbfa4fe934(a: number, b: number, c: number): void;
Expand Down

0 comments on commit 56383e6

Please sign in to comment.