(fix): wasm error
This commit is contained in:
parent
1efcb8837c
commit
8ae8377662
5 changed files with 28 additions and 19 deletions
|
|
@ -62,9 +62,7 @@ const client = await MTPClient.create({
|
||||||
|
|
||||||
client.subscribe("SomeType", (message) => console.log(message));
|
client.subscribe("SomeType", (message) => console.log(message));
|
||||||
|
|
||||||
const clientId = client.credentials?.clientId == null
|
const clientId = await client.auth();
|
||||||
? await client.register()
|
|
||||||
: (await client.connect(), client.credentials.clientId);
|
|
||||||
|
|
||||||
await client.send("SomeType", { value: "hello" });
|
await client.send("SomeType", { value: "hello" });
|
||||||
console.log("Connected MTP client", clientId, client.state);
|
console.log("Connected MTP client", clientId, client.state);
|
||||||
|
|
|
||||||
|
|
@ -199,10 +199,7 @@ async function connect() {
|
||||||
log(`Subscribed Pong: ${formatParsedFrame(frame)}`, "received");
|
log(`Subscribed Pong: ${formatParsedFrame(frame)}`, "received");
|
||||||
});
|
});
|
||||||
|
|
||||||
const existingClientId = client.credentials?.clientId;
|
const activeClientId = await client.auth();
|
||||||
const activeClientId = existingClientId == null
|
|
||||||
? await client.register()
|
|
||||||
: (await client.connect(), BigInt(existingClientId));
|
|
||||||
clientId = activeClientId;
|
clientId = activeClientId;
|
||||||
loadKeys();
|
loadKeys();
|
||||||
log(`Connected as client ${activeClientId}`);
|
log(`Connected as client ${activeClientId}`);
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,7 @@ type NormalizedMTPClientOptions = Omit<MTPClientOptions, "hostPublicKey"> & {
|
||||||
};
|
};
|
||||||
|
|
||||||
const DEFAULT_CREDENTIALS_KEY = "mtp:credentials";
|
const DEFAULT_CREDENTIALS_KEY = "mtp:credentials";
|
||||||
|
let wasmInitPromise: Promise<Awaited<ReturnType<typeof initWasm>>> | undefined;
|
||||||
|
|
||||||
function emit(logger, event) {
|
function emit(logger, event) {
|
||||||
if (typeof logger === "function") {
|
if (typeof logger === "function") {
|
||||||
|
|
@ -394,7 +395,8 @@ export class MTPClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
static async init(wasm?: MTPClientOptions["wasm"]): Promise<Awaited<ReturnType<typeof initWasm>>> {
|
static async init(wasm?: MTPClientOptions["wasm"]): Promise<Awaited<ReturnType<typeof initWasm>>> {
|
||||||
return await initWasm(wasm);
|
wasmInitPromise ??= initWasm(wasm);
|
||||||
|
return await wasmInitPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
get credentials(): MTPClientCredentials | null {
|
get credentials(): MTPClientCredentials | null {
|
||||||
|
|
@ -430,7 +432,7 @@ export class MTPClient {
|
||||||
|
|
||||||
async connect(): Promise<void> {
|
async connect(): Promise<void> {
|
||||||
if (this.#credentials?.clientId != null && this.#options.hostPublicKey) {
|
if (this.#credentials?.clientId != null && this.#options.hostPublicKey) {
|
||||||
await this.#connectAuthenticated();
|
await this.auth();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -447,6 +449,15 @@ export class MTPClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async auth(): Promise<bigint> {
|
||||||
|
if (!this.#options.hostPublicKey) {
|
||||||
|
throw new Error("MTPClient.auth requires hostPublicKey");
|
||||||
|
}
|
||||||
|
return this.#credentials?.clientId == null
|
||||||
|
? await this.register()
|
||||||
|
: await this.#connectAuthenticated();
|
||||||
|
}
|
||||||
|
|
||||||
async #connectAuthenticated() {
|
async #connectAuthenticated() {
|
||||||
if (!this.#options.hostPublicKey) {
|
if (!this.#options.hostPublicKey) {
|
||||||
throw new Error("MTPClient.connect requires hostPublicKey for authenticated connections");
|
throw new Error("MTPClient.connect requires hostPublicKey for authenticated connections");
|
||||||
|
|
|
||||||
|
|
@ -705,19 +705,19 @@ impl WasmClient {
|
||||||
let loop_pending_requests = pending_requests.clone();
|
let loop_pending_requests = pending_requests.clone();
|
||||||
let ping_timer = self.ping_timer.clone();
|
let ping_timer = self.ping_timer.clone();
|
||||||
wasm_bindgen_futures::spawn_local(async move {
|
wasm_bindgen_futures::spawn_local(async move {
|
||||||
let route_frame = Closure::wrap(Box::new(move |frame: JsValue| {
|
|
||||||
route_incoming_frame(&frame, &on_msg, &subscriptions, &loop_pending_requests);
|
|
||||||
}) as Box<dyn FnMut(JsValue)>);
|
|
||||||
loop_transport
|
loop_transport
|
||||||
.receive_loop(
|
.receive_loop(
|
||||||
route_frame
|
move |frame: JsValue| {
|
||||||
.as_ref()
|
route_incoming_frame(
|
||||||
.unchecked_ref::<js_sys::Function>()
|
&frame,
|
||||||
.clone(),
|
&on_msg,
|
||||||
|
&subscriptions,
|
||||||
|
&loop_pending_requests,
|
||||||
|
);
|
||||||
|
},
|
||||||
on_err.clone(),
|
on_err.clone(),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
drop(route_frame);
|
|
||||||
state.set(ConnectionState::Disconnected);
|
state.set(ConnectionState::Disconnected);
|
||||||
stop_ping_timer(&ping_timer);
|
stop_ping_timer(&ping_timer);
|
||||||
reject_pending_requests(&pending_requests, "disconnected");
|
reject_pending_requests(&pending_requests, "disconnected");
|
||||||
|
|
|
||||||
|
|
@ -347,12 +347,15 @@ impl WasmTransport {
|
||||||
/// Background loop: deliver every incoming frame to `on_message` until the
|
/// Background loop: deliver every incoming frame to `on_message` until the
|
||||||
/// connection closes. Shares reader state with `read_one_frame`, so frames
|
/// connection closes. Shares reader state with `read_one_frame`, so frames
|
||||||
/// buffered during the handshake are not lost.
|
/// buffered during the handshake are not lost.
|
||||||
pub async fn receive_loop(&self, on_message: js_sys::Function, on_error: js_sys::Function) {
|
pub async fn receive_loop<F>(&self, mut on_message: F, on_error: js_sys::Function)
|
||||||
|
where
|
||||||
|
F: FnMut(JsValue),
|
||||||
|
{
|
||||||
loop {
|
loop {
|
||||||
match self.next_frame().await {
|
match self.next_frame().await {
|
||||||
Ok(FrameOutcome::Frame(frame)) => match parse_frame_value(&frame) {
|
Ok(FrameOutcome::Frame(frame)) => match parse_frame_value(&frame) {
|
||||||
Ok(parsed) => {
|
Ok(parsed) => {
|
||||||
let _ = on_message.call1(&JsValue::NULL, &parsed);
|
on_message(parsed);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let message = e.as_string().unwrap_or_else(|| format!("{:?}", e));
|
let message = e.as_string().unwrap_or_else(|| format!("{:?}", e));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue