diff --git a/Cargo.lock b/Cargo.lock index 060f54e..23c99c9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -75,6 +75,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "anyhow" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9a8f622bcf6ff3df478e9deba3e03e4e04b300f8e6a139e192c05fa3490afc7" + [[package]] name = "async-stream" version = "0.3.3" @@ -1102,6 +1108,8 @@ dependencies = [ name = "lan_party_web" version = "0.1.0" dependencies = [ + "anyhow", + "js-sys", "lan_party_core", "serde", "serde_json", @@ -1110,6 +1118,7 @@ dependencies = [ "wasm-bindgen-futures", "web-sys", "yew", + "yew-hooks", "yew-router", ] @@ -3107,6 +3116,22 @@ dependencies = [ "yew-macro", ] +[[package]] +name = "yew-hooks" +version = "0.1.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1807cd5bf44c34255534fc8a824b0d6c1022147affecbb7f10c6753f336e7309" +dependencies = [ + "gloo", + "js-sys", + "log", + "serde", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "yew", +] + [[package]] name = "yew-macro" version = "0.19.3" diff --git a/web/Cargo.toml b/web/Cargo.toml index 7f3a270..d458cca 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -15,3 +15,6 @@ wasm-bindgen-futures = "0.4" serde = "1" serde_json = "1" thiserror = "1" +yew-hooks = "0.1" +anyhow = "1.0" +js-sys = "0.3" diff --git a/web/dist/index-8b6dadb5b2545aea.js b/web/dist/index-8b6dadb5b2545aea.js deleted file mode 100644 index ce79a6b..0000000 --- a/web/dist/index-8b6dadb5b2545aea.js +++ /dev/null @@ -1,693 +0,0 @@ - -let wasm; - -const cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); - -cachedTextDecoder.decode(); - -let cachedUint8Memory0 = new Uint8Array(); - -function getUint8Memory0() { - if (cachedUint8Memory0.byteLength === 0) { - cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); - } - return cachedUint8Memory0; -} - -function getStringFromWasm0(ptr, len) { - return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); -} - -const heap = new Array(32).fill(undefined); - -heap.push(undefined, null, true, false); - -let heap_next = heap.length; - -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 getObject(idx) { return heap[idx]; } - -let WASM_VECTOR_LEN = 0; - -const cachedTextEncoder = new TextEncoder('utf-8'); - -const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' - ? function (arg, view) { - return cachedTextEncoder.encodeInto(arg, view); -} - : function (arg, view) { - const buf = cachedTextEncoder.encode(arg); - view.set(buf); - return { - read: arg.length, - written: buf.length - }; -}); - -function passStringToWasm0(arg, malloc, realloc) { - - if (realloc === undefined) { - const buf = cachedTextEncoder.encode(arg); - const ptr = malloc(buf.length); - getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf); - WASM_VECTOR_LEN = buf.length; - return ptr; - } - - let len = arg.length; - let ptr = malloc(len); - - const mem = getUint8Memory0(); - - let offset = 0; - - for (; offset < len; offset++) { - const code = arg.charCodeAt(offset); - if (code > 0x7F) break; - mem[ptr + offset] = code; - } - - if (offset !== len) { - if (offset !== 0) { - arg = arg.slice(offset); - } - ptr = realloc(ptr, len, len = offset + arg.length * 3); - const view = getUint8Memory0().subarray(ptr + offset, ptr + len); - const ret = encodeString(arg, view); - - offset += ret.written; - } - - WASM_VECTOR_LEN = offset; - return ptr; -} - -let cachedInt32Memory0 = new Int32Array(); - -function getInt32Memory0() { - if (cachedInt32Memory0.byteLength === 0) { - cachedInt32Memory0 = new Int32Array(wasm.memory.buffer); - } - return cachedInt32Memory0; -} - -function isLikeNone(x) { - return x === undefined || x === null; -} - -let cachedFloat64Memory0 = new Float64Array(); - -function getFloat64Memory0() { - if (cachedFloat64Memory0.byteLength === 0) { - cachedFloat64Memory0 = new Float64Array(wasm.memory.buffer); - } - return cachedFloat64Memory0; -} - -function dropObject(idx) { - if (idx < 36) return; - heap[idx] = heap_next; - heap_next = idx; -} - -function takeObject(idx) { - const ret = getObject(idx); - dropObject(idx); - return ret; -} - -function debugString(val) { - // primitive types - const type = typeof val; - if (type == 'number' || type == 'boolean' || val == null) { - return `${val}`; - } - if (type == 'string') { - return `"${val}"`; - } - if (type == 'symbol') { - const description = val.description; - if (description == null) { - return 'Symbol'; - } else { - return `Symbol(${description})`; - } - } - if (type == 'function') { - const name = val.name; - if (typeof name == 'string' && name.length > 0) { - return `Function(${name})`; - } else { - return 'Function'; - } - } - // objects - if (Array.isArray(val)) { - const length = val.length; - let debug = '['; - if (length > 0) { - debug += debugString(val[0]); - } - for(let i = 1; i < length; i++) { - debug += ', ' + debugString(val[i]); - } - debug += ']'; - return debug; - } - // Test for built-in - const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val)); - let className; - if (builtInMatches.length > 1) { - className = builtInMatches[1]; - } else { - // Failed to match the standard '[object ClassName]' - return toString.call(val); - } - if (className == 'Object') { - // we're a user defined class or Object - // JSON.stringify avoids problems with cycles, and is generally much - // easier than looping through ownProperties of `val`. - try { - return 'Object(' + JSON.stringify(val) + ')'; - } catch (_) { - return 'Object'; - } - } - // errors - if (val instanceof Error) { - return `${val.name}: ${val.message}\n${val.stack}`; - } - // TODO we could test for more things here, like `Set`s and `Map`s. - return className; -} - -function makeClosure(arg0, arg1, dtor, f) { - const state = { a: arg0, b: arg1, cnt: 1, dtor }; - const real = (...args) => { - // First up with a closure we increment the internal reference - // count. This ensures that the Rust closure environment won't - // be deallocated while we're invoking it. - state.cnt++; - try { - return f(state.a, state.b, ...args); - } finally { - if (--state.cnt === 0) { - wasm.__wbindgen_export_2.get(state.dtor)(state.a, state.b); - state.a = 0; - - } - } - }; - real.original = state; - - return real; -} -function __wbg_adapter_24(arg0, arg1, arg2) { - wasm._dyn_core__ops__function__Fn__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__hf2a95eaa6a1fe8eb(arg0, arg1, addHeapObject(arg2)); -} - -function makeMutClosure(arg0, arg1, dtor, f) { - const state = { a: arg0, b: arg1, cnt: 1, dtor }; - const real = (...args) => { - // First up with a closure we increment the internal reference - // count. This ensures that the Rust closure environment won't - // be deallocated while we're invoking it. - state.cnt++; - const a = state.a; - state.a = 0; - try { - return f(a, state.b, ...args); - } finally { - if (--state.cnt === 0) { - wasm.__wbindgen_export_2.get(state.dtor)(a, state.b); - - } else { - state.a = a; - } - } - }; - real.original = state; - - return real; -} - -let stack_pointer = 32; - -function addBorrowedObject(obj) { - if (stack_pointer == 1) throw new Error('out of js stack'); - heap[--stack_pointer] = obj; - return stack_pointer; -} -function __wbg_adapter_27(arg0, arg1, arg2) { - try { - wasm._dyn_core__ops__function__FnMut___A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h3700fed383a0f072(arg0, arg1, addBorrowedObject(arg2)); - } finally { - heap[stack_pointer++] = undefined; - } -} - -function __wbg_adapter_30(arg0, arg1, arg2) { - wasm._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h0ff5f5f93b8ace87(arg0, arg1, addHeapObject(arg2)); -} - -let cachedUint32Memory0 = new Uint32Array(); - -function getUint32Memory0() { - if (cachedUint32Memory0.byteLength === 0) { - cachedUint32Memory0 = new Uint32Array(wasm.memory.buffer); - } - return cachedUint32Memory0; -} - -function getArrayJsValueFromWasm0(ptr, len) { - const mem = getUint32Memory0(); - const slice = mem.subarray(ptr / 4, ptr / 4 + len); - const result = []; - for (let i = 0; i < slice.length; i++) { - result.push(takeObject(slice[i])); - } - return result; -} - -function handleError(f, args) { - try { - return f.apply(this, args); - } catch (e) { - wasm.__wbindgen_exn_store(addHeapObject(e)); - } -} - -async function load(module, imports) { - if (typeof Response === 'function' && module instanceof Response) { - if (typeof WebAssembly.instantiateStreaming === 'function') { - try { - return await WebAssembly.instantiateStreaming(module, imports); - - } catch (e) { - if (module.headers.get('Content-Type') != 'application/wasm') { - console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); - - } else { - throw e; - } - } - } - - const bytes = await module.arrayBuffer(); - return await WebAssembly.instantiate(bytes, imports); - - } else { - const instance = await WebAssembly.instantiate(module, imports); - - if (instance instanceof WebAssembly.Instance) { - return { instance, module }; - - } else { - return instance; - } - } -} - -function getImports() { - const imports = {}; - imports.wbg = {}; - imports.wbg.__wbindgen_string_new = function(arg0, arg1) { - const ret = getStringFromWasm0(arg0, arg1); - return addHeapObject(ret); - }; - imports.wbg.__wbindgen_object_clone_ref = function(arg0) { - const ret = getObject(arg0); - return addHeapObject(ret); - }; - imports.wbg.__wbindgen_json_parse = function(arg0, arg1) { - const ret = JSON.parse(getStringFromWasm0(arg0, arg1)); - return addHeapObject(ret); - }; - imports.wbg.__wbindgen_json_serialize = function(arg0, arg1) { - const obj = getObject(arg1); - const ret = JSON.stringify(obj === undefined ? null : obj); - const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - getInt32Memory0()[arg0 / 4 + 1] = len0; - getInt32Memory0()[arg0 / 4 + 0] = ptr0; - }; - imports.wbg.__wbindgen_is_undefined = function(arg0) { - const ret = getObject(arg0) === undefined; - 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_number_new = function(arg0) { - const ret = arg0; - return addHeapObject(ret); - }; - imports.wbg.__wbg_error_09919627ac0992f5 = function(arg0, arg1) { - try { - console.error(getStringFromWasm0(arg0, arg1)); - } finally { - wasm.__wbindgen_free(arg0, arg1); - } - }; - imports.wbg.__wbg_new_693216e109162396 = function() { - const ret = new Error(); - return addHeapObject(ret); - }; - imports.wbg.__wbg_stack_0ddaca5d1abfb52f = function(arg0, arg1) { - const ret = getObject(arg1).stack; - const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - getInt32Memory0()[arg0 / 4 + 1] = len0; - getInt32Memory0()[arg0 / 4 + 0] = ptr0; - }; - imports.wbg.__wbindgen_object_drop_ref = function(arg0) { - takeObject(arg0); - }; - 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_warn_921059440157e870 = function(arg0, arg1) { - var v0 = getArrayJsValueFromWasm0(arg0, arg1).slice(); - wasm.__wbindgen_free(arg0, arg1 * 4); - console.warn(...v0); - }; - imports.wbg.__wbg_instanceof_Window_42f092928baaee84 = function(arg0) { - const ret = getObject(arg0) instanceof Window; - return ret; - }; - imports.wbg.__wbg_document_15b2e504fb1556d6 = function(arg0) { - const ret = getObject(arg0).document; - return isLikeNone(ret) ? 0 : addHeapObject(ret); - }; - imports.wbg.__wbg_location_312161fbd0cf64f0 = function(arg0) { - const ret = getObject(arg0).location; - return addHeapObject(ret); - }; - imports.wbg.__wbg_history_cb2cdfbe20fef7ad = function() { return handleError(function (arg0) { - const ret = getObject(arg0).history; - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_fetch_9a5cb9d8a96004d0 = function(arg0, arg1) { - const ret = getObject(arg0).fetch(getObject(arg1)); - return addHeapObject(ret); - }; - imports.wbg.__wbg_body_5e6efc7a3c1b65f3 = function(arg0) { - const ret = getObject(arg0).body; - return isLikeNone(ret) ? 0 : addHeapObject(ret); - }; - imports.wbg.__wbg_createElement_28fc3740fb11defb = function() { return handleError(function (arg0, arg1, arg2) { - const ret = getObject(arg0).createElement(getStringFromWasm0(arg1, arg2)); - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_createElementNS_dd6cca2457c8c16c = function() { return handleError(function (arg0, arg1, arg2, arg3, arg4) { - const ret = getObject(arg0).createElementNS(arg1 === 0 ? undefined : getStringFromWasm0(arg1, arg2), getStringFromWasm0(arg3, arg4)); - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_createTextNode_2ab1e3ebc34e2641 = function(arg0, arg1, arg2) { - const ret = getObject(arg0).createTextNode(getStringFromWasm0(arg1, arg2)); - return addHeapObject(ret); - }; - imports.wbg.__wbg_querySelector_73feab41810011dc = function() { return handleError(function (arg0, arg1, arg2) { - const ret = getObject(arg0).querySelector(getStringFromWasm0(arg1, arg2)); - return isLikeNone(ret) ? 0 : addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_instanceof_HtmlInputElement_3fad42774bc62388 = function(arg0) { - const ret = getObject(arg0) instanceof HTMLInputElement; - return ret; - }; - imports.wbg.__wbg_setchecked_a450b330df6b3fa5 = function(arg0, arg1) { - getObject(arg0).checked = arg1 !== 0; - }; - imports.wbg.__wbg_value_30770021ca38e0db = function(arg0, arg1) { - const ret = getObject(arg1).value; - const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - getInt32Memory0()[arg0 / 4 + 1] = len0; - getInt32Memory0()[arg0 / 4 + 0] = ptr0; - }; - imports.wbg.__wbg_setvalue_7b7950dacc5eb607 = function(arg0, arg1, arg2) { - getObject(arg0).value = getStringFromWasm0(arg1, arg2); - }; - imports.wbg.__wbg_addEventListener_ec92ea1297eefdfc = function() { return handleError(function (arg0, arg1, arg2, arg3, arg4) { - getObject(arg0).addEventListener(getStringFromWasm0(arg1, arg2), getObject(arg3), getObject(arg4)); - }, arguments) }; - imports.wbg.__wbg_removeEventListener_bfe676215a590711 = function() { return handleError(function (arg0, arg1, arg2, arg3, arg4) { - getObject(arg0).removeEventListener(getStringFromWasm0(arg1, arg2), getObject(arg3), arg4 !== 0); - }, arguments) }; - imports.wbg.__wbg_pushState_65b5fb0f30ca9d61 = function() { return handleError(function (arg0, arg1, arg2, arg3, arg4, arg5) { - getObject(arg0).pushState(getObject(arg1), getStringFromWasm0(arg2, arg3), arg4 === 0 ? undefined : getStringFromWasm0(arg4, arg5)); - }, arguments) }; - imports.wbg.__wbg_parentElement_14138ef2ff0b9c88 = function(arg0) { - const ret = getObject(arg0).parentElement; - return isLikeNone(ret) ? 0 : addHeapObject(ret); - }; - imports.wbg.__wbg_lastChild_2d1fa5efd0e0edcc = function(arg0) { - const ret = getObject(arg0).lastChild; - return isLikeNone(ret) ? 0 : addHeapObject(ret); - }; - imports.wbg.__wbg_setnodeValue_59d46f408f89fd0b = function(arg0, arg1, arg2) { - getObject(arg0).nodeValue = arg1 === 0 ? undefined : getStringFromWasm0(arg1, arg2); - }; - imports.wbg.__wbg_appendChild_d21bac021b5bbfde = function() { return handleError(function (arg0, arg1) { - const ret = getObject(arg0).appendChild(getObject(arg1)); - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_insertBefore_26dfd5eb687a3438 = function() { return handleError(function (arg0, arg1, arg2) { - const ret = getObject(arg0).insertBefore(getObject(arg1), getObject(arg2)); - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_removeChild_94b0c126b878241b = function() { return handleError(function (arg0, arg1) { - const ret = getObject(arg0).removeChild(getObject(arg1)); - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_new_4cba26249c1686cd = function() { return handleError(function () { - const ret = new Headers(); - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_append_9c6d4d7f71076e48 = function() { return handleError(function (arg0, arg1, arg2, arg3, arg4) { - getObject(arg0).append(getStringFromWasm0(arg1, arg2), getStringFromWasm0(arg3, arg4)); - }, arguments) }; - imports.wbg.__wbg_pathname_c08f1ef51f6ebba9 = function() { return handleError(function (arg0, arg1) { - const ret = getObject(arg1).pathname; - const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - getInt32Memory0()[arg0 / 4 + 1] = len0; - getInt32Memory0()[arg0 / 4 + 0] = ptr0; - }, arguments) }; - imports.wbg.__wbg_instanceof_Element_1714e50f9bda1d15 = function(arg0) { - const ret = getObject(arg0) instanceof Element; - return ret; - }; - imports.wbg.__wbg_namespaceURI_b343a4afa454dd59 = function(arg0, arg1) { - const ret = getObject(arg1).namespaceURI; - var ptr0 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - var len0 = WASM_VECTOR_LEN; - getInt32Memory0()[arg0 / 4 + 1] = len0; - getInt32Memory0()[arg0 / 4 + 0] = ptr0; - }; - imports.wbg.__wbg_removeAttribute_2d6e56b2f03aa57e = function() { return handleError(function (arg0, arg1, arg2) { - getObject(arg0).removeAttribute(getStringFromWasm0(arg1, arg2)); - }, arguments) }; - imports.wbg.__wbg_setAttribute_8cfc462c0dedd03b = function() { return handleError(function (arg0, arg1, arg2, arg3, arg4) { - getObject(arg0).setAttribute(getStringFromWasm0(arg1, arg2), getStringFromWasm0(arg3, arg4)); - }, arguments) }; - imports.wbg.__wbg_log_17733ab6fa45831d = function(arg0) { - console.log(getObject(arg0)); - }; - imports.wbg.__wbg_instanceof_Response_240e67e5796c3c6b = function(arg0) { - const ret = getObject(arg0) instanceof Response; - return ret; - }; - imports.wbg.__wbg_json_30d916f3f34485ab = function() { return handleError(function (arg0) { - const ret = getObject(arg0).json(); - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_pathname_8ed2fc02f98aeaaf = function(arg0, arg1) { - const ret = getObject(arg1).pathname; - const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - getInt32Memory0()[arg0 / 4 + 1] = len0; - getInt32Memory0()[arg0 / 4 + 0] = ptr0; - }; - imports.wbg.__wbg_new_d1d1300265e34170 = function() { return handleError(function (arg0, arg1) { - const ret = new URL(getStringFromWasm0(arg0, arg1)); - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_value_eb32f706ae6bfab2 = function(arg0, arg1) { - const ret = getObject(arg1).value; - const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - getInt32Memory0()[arg0 / 4 + 1] = len0; - getInt32Memory0()[arg0 / 4 + 0] = ptr0; - }; - imports.wbg.__wbg_setvalue_3dd349be116107ce = function(arg0, arg1, arg2) { - getObject(arg0).value = getStringFromWasm0(arg1, arg2); - }; - imports.wbg.__wbg_instanceof_Event_8c74064684d03e14 = function(arg0) { - const ret = getObject(arg0) instanceof Event; - return ret; - }; - imports.wbg.__wbg_target_68a5c10e2732a79e = function(arg0) { - const ret = getObject(arg0).target; - return isLikeNone(ret) ? 0 : addHeapObject(ret); - }; - imports.wbg.__wbg_cancelBubble_aa216b328c490cb1 = function(arg0) { - const ret = getObject(arg0).cancelBubble; - return ret; - }; - imports.wbg.__wbg_href_cae04ee9562fc683 = function(arg0, arg1) { - const ret = getObject(arg1).href; - const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - getInt32Memory0()[arg0 / 4 + 1] = len0; - getInt32Memory0()[arg0 / 4 + 0] = ptr0; - }; - imports.wbg.__wbg_newwithstrandinit_de7c409ec8538105 = function() { return handleError(function (arg0, arg1, arg2) { - const ret = new Request(getStringFromWasm0(arg0, arg1), getObject(arg2)); - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_newnoargs_971e9a5abe185139 = function(arg0, arg1) { - const ret = new Function(getStringFromWasm0(arg0, arg1)); - return addHeapObject(ret); - }; - imports.wbg.__wbg_call_33d7bcddbbfa394a = function() { return handleError(function (arg0, arg1) { - const ret = getObject(arg0).call(getObject(arg1)); - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_valueOf_f83bee79f23e7b05 = function(arg0) { - const ret = getObject(arg0).valueOf(); - return ret; - }; - imports.wbg.__wbg_is_43eb2f9708e964a9 = function(arg0, arg1) { - const ret = Object.is(getObject(arg0), getObject(arg1)); - return ret; - }; - imports.wbg.__wbg_new_e6a9fecc2bf26696 = function() { - const ret = new Object(); - return addHeapObject(ret); - }; - imports.wbg.__wbg_resolve_0107b3a501450ba0 = function(arg0) { - const ret = Promise.resolve(getObject(arg0)); - return addHeapObject(ret); - }; - imports.wbg.__wbg_then_18da6e5453572fc8 = function(arg0, arg1) { - const ret = getObject(arg0).then(getObject(arg1)); - return addHeapObject(ret); - }; - imports.wbg.__wbg_then_e5489f796341454b = function(arg0, arg1, arg2) { - const ret = getObject(arg0).then(getObject(arg1), getObject(arg2)); - return addHeapObject(ret); - }; - imports.wbg.__wbg_globalThis_3348936ac49df00a = function() { return handleError(function () { - const ret = globalThis.globalThis; - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_self_fd00a1ef86d1b2ed = function() { return handleError(function () { - const ret = self.self; - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_window_6f6e346d8bbd61d7 = function() { return handleError(function () { - const ret = window.window; - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_global_67175caf56f55ca9 = function() { return handleError(function () { - const ret = global.global; - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_get_72332cd2bc57924c = function() { return handleError(function (arg0, arg1) { - const ret = Reflect.get(getObject(arg0), getObject(arg1)); - return addHeapObject(ret); - }, arguments) }; - imports.wbg.__wbg_set_2762e698c2f5b7e0 = function() { return handleError(function (arg0, arg1, arg2) { - const ret = Reflect.set(getObject(arg0), getObject(arg1), getObject(arg2)); - return ret; - }, arguments) }; - imports.wbg.__wbindgen_debug_string = function(arg0, arg1) { - const ret = debugString(getObject(arg1)); - const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); - const len0 = WASM_VECTOR_LEN; - getInt32Memory0()[arg0 / 4 + 1] = len0; - getInt32Memory0()[arg0 / 4 + 0] = ptr0; - }; - imports.wbg.__wbindgen_throw = function(arg0, arg1) { - throw new Error(getStringFromWasm0(arg0, arg1)); - }; - imports.wbg.__wbindgen_closure_wrapper7414 = function(arg0, arg1, arg2) { - const ret = makeClosure(arg0, arg1, 466, __wbg_adapter_24); - return addHeapObject(ret); - }; - imports.wbg.__wbindgen_closure_wrapper8075 = function(arg0, arg1, arg2) { - const ret = makeMutClosure(arg0, arg1, 520, __wbg_adapter_27); - return addHeapObject(ret); - }; - imports.wbg.__wbindgen_closure_wrapper8354 = function(arg0, arg1, arg2) { - const ret = makeMutClosure(arg0, arg1, 525, __wbg_adapter_30); - return addHeapObject(ret); - }; - - return imports; -} - -function initMemory(imports, maybe_memory) { - -} - -function finalizeInit(instance, module) { - wasm = instance.exports; - init.__wbindgen_wasm_module = module; - cachedFloat64Memory0 = new Float64Array(); - cachedInt32Memory0 = new Int32Array(); - cachedUint32Memory0 = new Uint32Array(); - cachedUint8Memory0 = new Uint8Array(); - - wasm.__wbindgen_start(); - return wasm; -} - -function initSync(bytes) { - const imports = getImports(); - - initMemory(imports); - - const module = new WebAssembly.Module(bytes); - const instance = new WebAssembly.Instance(module, imports); - - return finalizeInit(instance, module); -} - -async function init(input) { - if (typeof input === 'undefined') { - input = new URL('index-8b6dadb5b2545aea_bg.wasm', import.meta.url); - } - const imports = getImports(); - - if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) { - input = fetch(input); - } - - initMemory(imports); - - const { instance, module } = await load(await input, imports); - - return finalizeInit(instance, module); -} - -export { initSync } -export default init; diff --git a/web/dist/index-8b6dadb5b2545aea_bg.wasm b/web/dist/index-8b6dadb5b2545aea_bg.wasm deleted file mode 100644 index 4c3f993..0000000 Binary files a/web/dist/index-8b6dadb5b2545aea_bg.wasm and /dev/null differ diff --git a/web/dist/index.html b/web/dist/index.html index 8feba5e..3b2d162 100644 --- a/web/dist/index.html +++ b/web/dist/index.html @@ -4,9 +4,9 @@ Yew App - - + + - \ No newline at end of file + \ No newline at end of file diff --git a/web/src/components/mod.rs b/web/src/components/mod.rs index e22494a..4be5f03 100644 --- a/web/src/components/mod.rs +++ b/web/src/components/mod.rs @@ -12,7 +12,7 @@ use yew::prelude::*; #[derive(Clone, PartialEq, Properties)] pub struct InputProps { pub value: String, - pub on_change: Callback, + pub onchange: Callback, } fn get_value_from_input_event(e: InputEvent) -> String { @@ -26,10 +26,10 @@ fn get_value_from_input_event(e: InputEvent) -> String { /// Controlled Text Input Component #[function_component(TextInput)] pub fn text_input(props: &InputProps) -> Html { - let InputProps { value, on_change } = props.clone(); + let InputProps { value, onchange } = props.clone(); let oninput = Callback::from(move |input_event: InputEvent| { - on_change.emit(get_value_from_input_event(input_event)); + onchange.emit(get_value_from_input_event(input_event)); }); html! { diff --git a/web/src/pages/users.rs b/web/src/pages/users.rs index 96887de..8a8db17 100644 --- a/web/src/pages/users.rs +++ b/web/src/pages/users.rs @@ -1,107 +1,107 @@ -use crate::components::{Button, Page, Table, TextInput}; -use lan_party_core::user::User; +use crate::{ + clone, clone_cb, + components::{Button, Page, Table, TextInput}, + util::{api_request, AutoCopy}, +}; +use lan_party_core::{user::User, PartyError}; use wasm_bindgen_futures::spawn_local; use yew::prelude::*; +use yew_hooks::*; -#[derive(Debug, Default)] -pub struct UsersPage { - headers: Vec, - users: Vec, - score_edit: Option, - current_score: String, -} +#[function_component(UsersPage)] +pub fn users_page() -> Html { + let headers = vec!["Username".into(), "Score".into(), "".into()]; -pub enum Msg { - UpdateList(Vec), - UpdateScoreInput(String), - UpdateScore(usize), - EditScore(usize), - DeleteUser(usize), -} + let score_edit: UseStateHandle> = use_state(|| Option::None); + let current_score = use_state(|| String::new()); + let users: UseAsyncHandle, _> = use_async(async move { + api_request::<_, Vec>("GET", "/user", Option::<()>::None) + .await + .map(|inner| inner.unwrap()) + .map_err(|_| "failed to load users") + }); -impl Component for UsersPage { - type Message = Msg; - type Properties = (); - - fn create(ctx: &Context) -> Self { - Self { - headers: vec!["Username".into(), "Score".into(), "".into()], - ..Default::default() + clone!(users; use_effect_with_deps(move |_| { + if users.data.is_none() { + users.run(); } - } + || () + }, ())); - fn rendered(&mut self, ctx: &Context, first_render: bool) { - let link = ctx.link().clone(); + let onchange = clone_cb!(current_score; move |value| current_score.set(value)); - if first_render { + let oncheck = clone_cb!(score_edit, current_score, users; move |_| { + clone!(score_edit, users, current_score; { spawn_local(async move { - let res: Result, _> = - crate::util::api_request("GET", "/user", Option::<()>::None).await; - link.send_message(Msg::UpdateList(res.unwrap())) + if let (Some(score_edit), Some(users_inner)) = (*score_edit, &users.data) { + if let Ok(score) = current_score.parse() { + let user: &User = &users_inner[score_edit]; + api_request::<_, ()>("POST", &format!("/user/{}/score", user.name), Some(score)).await.unwrap(); + let mut cloned = users_inner.clone(); + cloned[score_edit].score = score; + users.update(cloned); + } + } + score_edit.set(None); }); + }); + }); + + let onedit = clone_cb!(current_score, score_edit, users; i => move |_| { + if let Some(users) = &users.data { + let user: &User = &users[i]; + current_score.set(user.score.to_string()); + score_edit.set(Some(i)); } - } + }); - fn update(&mut self, _ctx: &Context, msg: Self::Message) -> bool { - match msg { - Msg::UpdateList(list) => { - self.users = list; - true - } - Msg::UpdateScoreInput(value) => { - self.current_score = value; - true - } - Msg::EditScore(i) => { - self.current_score = self.users[i].score.to_string(); - self.score_edit = Some(i); - true - } - Msg::UpdateScore(i) => { - self.score_edit = None; - true - } - Msg::DeleteUser(i) => true, - _ => todo!(), - } - } + let ondelete = clone_cb!(users; i => move |_| { + clone!(users; { + spawn_local(async move { + if let Some(users_inner) = &users.data { + let user: &User = &users_inner[i]; + api_request::<_, ()>("DELETE", &format!("/user/{}", user.name), Option::<()>::None).await.unwrap(); + let cloned = users_inner.iter().cloned().filter(|u| u.name != user.name).collect(); + users.update(cloned); + } + }); + }); + }); - fn view(&self, ctx: &Context) -> Html { - let link = ctx.link().clone(); - - html! { - - - {self.users.iter().enumerate().map(|(i, user)| html! { + html! { + +
+ { if let Some(users) = &users.data { + {users.iter().enumerate().map(move |(i, user)| html! { }).collect::()} -
{&user.name} - {if Some(i) == self.score_edit { html! { + {if Some(i) == *score_edit.clone() { html! { <> - -
-
- } + } else { html! { "Loading..." }}} + + } } diff --git a/web/src/util.rs b/web/src/util.rs index f1952fa..5beb1d7 100644 --- a/web/src/util.rs +++ b/web/src/util.rs @@ -1,23 +1,44 @@ +use std::ops::Deref; + +use anyhow::anyhow; use serde::{Deserialize, Serialize}; use thiserror::Error; use wasm_bindgen::{prelude::*, JsCast}; use wasm_bindgen_futures::JsFuture; use web_sys::{Headers, Request, RequestInit, RequestMode, Response}; +use yew::html::IntoPropValue; -#[derive(Error, Debug)] -pub enum FetchError { - #[error("javascript error")] - JsError(JsValue), - #[error("failed to serialize json: {source:?}")] - SerializationError { - #[from] - source: serde_json::Error, - }, +#[derive(Clone, Copy)] +pub struct AutoCopy(pub T); + +impl Deref for AutoCopy { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 + } } -impl From for FetchError { +impl + Clone> IntoPropValue for AutoCopy { + fn into_prop_value(self) -> T { + self.0.into_prop_value() + } +} + +#[derive(Error, Debug, Clone)] +pub enum JsError { + #[error("javascript error: {0}")] + JsError(String), +} + +impl From for JsError { fn from(value: JsValue) -> Self { - Self::JsError(value) + Self::JsError( + js_sys::JSON::stringify(&value) + .unwrap() + .as_string() + .unwrap(), + ) } } @@ -25,14 +46,16 @@ pub async fn api_request Deserialize<'a>>( method: &str, endpoint: &str, body: Option, -) -> Result { +) -> Result, anyhow::Error> { let api_key = "7de10bf6-278d-11ed-ad60-a8a15919d1b3"; let mut req_opts = RequestInit::new(); req_opts.method(method); - let headers = Headers::new()?; - headers.append("X-API-Key", api_key)?; + let headers = Headers::new().map_err(JsError::from)?; + headers + .append("X-API-Key", api_key) + .map_err(JsError::from)?; req_opts.headers(&headers); if let Some(body) = body { @@ -43,11 +66,46 @@ pub async fn api_request Deserialize<'a>>( let request = Request::new_with_str_and_init( &format!("http://localhost:8000/api/{}", endpoint), &req_opts, - )?; + ) + .map_err(JsError::from)?; let window = web_sys::window().unwrap(); - let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?; + let resp_value = JsFuture::from(window.fetch_with_request(&request)) + .await + .map_err(JsError::from)?; let resp: Response = resp_value.dyn_into().unwrap(); - Ok(JsFuture::from(resp.json()?).await?.into_serde()?) + let json = resp.json().map_err(JsError::from)?; + + if let Ok(data) = JsFuture::from(json).await.map_err(JsError::from) { + if let Ok(data) = data.into_serde() { + return Ok(Some(data)); + } + } + + Ok(None) +} + +#[macro_export] +macro_rules! clone { + ($($var:ident),* $(,)? ; $body:expr) => {{ + $(let $var = $var.clone();)* + $body + }}; +} + +#[macro_export] +macro_rules! clone_cb { + ($($var:ident),* ; $body:expr) => { + clone!($($var,)* ; { + Callback::from($body) + }) + }; + ($($var:ident),* ; $($param:ident),* => $body:expr) => { + clone!($($var,)* ; { + move |$($param,)*| { + Callback::from($body) + } + }) + }; }