Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump wasm - #5

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/perfect-sheep-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fedimint/core-web': patch
---

Bump wasm - Adds unsubscribe to streams
5 changes: 5 additions & 0 deletions .changeset/red-parrots-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fedimint/core-web': patch
---

Implemented unsubscribe for streaming rpcs
4 changes: 4 additions & 0 deletions examples/vite-core/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@ const RedeemEcash = () => {
const res = await wallet.redeemEcash(ecashInput)
console.warn('redeem ecash res', res)
setRedeemResult('Redeemed!')
setRedeemError('')
} catch (e) {
console.log('Error redeeming ecash', e)
setRedeemError(e as string)
setRedeemResult('')
}
}

Expand Down Expand Up @@ -150,9 +152,11 @@ const SendLightning = () => {
try {
await wallet.payInvoice(lightningInput)
setLightningResult('Paid!')
setLightningError('')
} catch (e) {
console.log('Error paying lightning', e)
setLightningError(e as string)
setLightningResult('')
}
}

Expand Down
59 changes: 40 additions & 19 deletions packages/core-web/src/FedimintWallet.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import init, { WasmClient } from '../wasm/fedimint_client_wasm.js'
import init, { RpcHandle, WasmClient } from '../wasm/fedimint_client_wasm.js'

type StreamError = {
error: string
data: never
}

// type StreamSuccess = {
// data: {} | [] | null | undefined | number | string | boolean
// }

type StreamSuccess = any & { error: never }
type StreamSuccess = {
data: {} | [] | null | undefined | number | string | boolean
error: never
}

type StreamResult = StreamSuccess | StreamError

Expand All @@ -21,7 +21,6 @@ export class FedimintWallet {
private initPromise: Promise<void> | null = null
private openPromise: Promise<void> | null = null
private resolveOpen: () => void = () => {}
// private worker: Worker | null = null

constructor(lazy: boolean = false) {
if (lazy) return
Expand Down Expand Up @@ -65,23 +64,36 @@ export class FedimintWallet {
module: string,
method: string,
body: Body = {},
cb: (res: string) => void,
) {
onSuccess: (res: StreamSuccess['data']) => void,
onError: (res: StreamError['error']) => void,
): Promise<RpcHandle> {
await this.openPromise
if (!this._fed) throw new Error('FedimintWallet is not open')
await this._fed.rpc(module, method, JSON.stringify(body), cb)
const unsubscribe = await this._fed.rpc(
module,
method,
JSON.stringify(body),
(res: string) => {
const parsed = JSON.parse(res) as StreamResult
if (parsed.error) {
onError(parsed.error)
} else {
onSuccess(parsed.data)
}
},
)
return unsubscribe
}

private async _rpcSingle(module: string, method: string, body: Body = {}) {
// console.warn('RPC', module, method, body)
return new Promise((resolve, reject) => {
if (!this._fed) return reject('FedimintWallet is not open')
this._fed.rpc(module, method, JSON.stringify(body), (res: string) => {
const parsed = JSON.parse(res)
const parsed = JSON.parse(res) as StreamResult
if (parsed.error) {
reject(parsed.error)
} else {
resolve(parsed)
resolve(parsed.data)
}
})
})
Expand Down Expand Up @@ -129,14 +141,23 @@ export class FedimintWallet {

// Streaming

subscribeBalance(callback: (balance: number) => void) {
this._rpcStream('', 'subscribe_balance_changes', {}, (res: any) => {
callback(res)
})
subscribeBalance(
onSuccess: (balance: number) => void = () => {},
onError: (error: string) => void = () => {},
) {
const unsubscribe = this._rpcStream(
'',
'subscribe_balance_changes',
{},
(res) => onSuccess(parseInt(res as string)),
onError,
)

// TODO: implement unsubscribe on wasm side
return () => {
// no-op (fake unsubscribe)
unsubscribe.then((unsub) => {
unsub.cancel()
unsub.free()
})
}
}
}
169 changes: 0 additions & 169 deletions packages/core-web/wasm/README.md

This file was deleted.

20 changes: 15 additions & 5 deletions packages/core-web/wasm/fedimint_client_wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
/* eslint-disable */
/**
*/
export class RpcHandle {
free(): void;
/**
*/
cancel(): void;
}
/**
*/
export class WasmClient {
free(): void;
/**
Expand All @@ -26,22 +34,24 @@ export class WasmClient {
/**
* Call a fedimint client rpc the responses are returned using `cb`
* callback. Each rpc call *can* return multiple responses by calling
* `cb` multiple times. You should ignore the promise by this function
* because it has no significance.
* `cb` multiple times. The returned RpcHandle can be used to cancel the
* operation.
* @param {string} module
* @param {string} method
* @param {string} payload
* @param {Function} cb
* @returns {Promise<void>}
* @returns {RpcHandle}
*/
rpc(module: string, method: string, payload: string, cb: Function): Promise<void>;
rpc(module: string, method: string, payload: string, cb: Function): RpcHandle;
}

export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;

export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly __wbg_wasmclient_free: (a: number) => void;
readonly __wbg_rpchandle_free: (a: number) => void;
readonly rpchandle_cancel: (a: number) => void;
readonly wasmclient_open: (a: number, b: number) => number;
readonly wasmclient_join_federation: (a: number, b: number, c: number, d: number) => number;
readonly wasmclient_rpc: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => number;
Expand All @@ -60,7 +70,7 @@ export interface InitOutput {
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
readonly __wbindgen_export_2: WebAssembly.Table;
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
readonly _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h30ddf3bee31ebb33: (a: number, b: number, c: number, d: number) => void;
readonly _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h8697e16cd1c64495: (a: number, b: number, c: number, d: number) => void;
readonly _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h31a94cc9a05d5ca0: (a: number, b: number, c: number) => void;
readonly _dyn_core__ops__function__FnMut_____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h4d11ec113460b95d: (a: number, b: number) => void;
readonly _dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__he7056307c6986185: (a: number, b: number, c: number, d: number) => void;
Expand Down
Loading