Skip to content

Commit bba0a04

Browse files
committed
feat(utils): add web worker rpc
1 parent df00082 commit bba0a04

16 files changed

+219
-5
lines changed

apps/docs/src/pages/utils.mdx

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Utils [Don't waste time on implementing these yourself.]
2+
3+
We've implemented some utilities that you might find useful. You can find them in the `@mina-js/utils` package.
4+
5+
## Units
6+
7+
Utils library provides functions for unit conversion.
8+
9+
### formatMina
10+
11+
Formats micro-Mina to human-readable Mina value.
12+
13+
```typescript twoslash
14+
import { formatMina } from '@mina-js/utils'
15+
16+
const mina = formatMina(5_000_000_000n)
17+
// -> "5"
18+
```
19+
20+
### parseMina
21+
22+
Parses human-readable Mina value to micro-Mina.
23+
24+
```typescript twoslash
25+
import { parseMina } from '@mina-js/utils'
26+
27+
const mina = parseMina('5')
28+
// -> 5_000_000_000n
29+
```
30+
31+
### formatUnit
32+
33+
```typescript twoslash
34+
import { formatUnits } from '@mina-js/utils'
35+
36+
const formatted = formatUnits(4200000000000n, 10)
37+
// -> "420"
38+
```
39+
40+
### parseUnits
41+
42+
```typescript twoslash
43+
import { parseUnits } from '@mina-js/utils'
44+
45+
const parsed = parseUnits("420", 10)
46+
// -> 4200000000000n
47+
```
48+
49+
## Web Workers
50+
51+
Proof related computations can be heavy and can block the main thread. To avoid this, you can use Web Workers to run these computations in a separate thread. We've prepared a JSON-RPC protocol to easily connect the dots.
52+
53+
```typescript twoslash
54+
// @filename: worker.ts
55+
import { createRpcHandler } from "@mina-js/utils";
56+
57+
const { messageHandler } = createRpcHandler({
58+
methods: {
59+
ping: async () => 'pong',
60+
}
61+
})
62+
63+
self.onmessage = messageHandler
64+
65+
// @filename: index.ts
66+
import { createRpc } from "@mina-js/utils";
67+
68+
const worker = new Worker(new URL('./worker.ts', import.meta.url))
69+
const rpc = createRpc({ worker })
70+
const response = await rpc.request({
71+
method: 'ping',
72+
params: [],
73+
})
74+
```

apps/docs/vocs.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ export default defineConfig({
8181
text: "Roadmap",
8282
link: "/roadmap",
8383
},
84+
{
85+
text: "Utils",
86+
link: "/utils",
87+
},
8488
{
8589
text: "MinaJS Connect",
8690
link: "/connect",

bun.lockb

1.03 KB
Binary file not shown.

packages/utils/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"cleanup": "rimraf dist .turbo"
1818
},
1919
"dependencies": {
20-
"mina-signer": "3.0.7"
20+
"mina-signer": "3.0.7",
21+
"superjson": "2.2.1"
2122
},
2223
"devDependencies": {
2324
"zod": "3.23.8"

packages/utils/src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export * from "./types";
22
export * from "./validation";
3-
export { formatMina } from "./src/format-mina";
4-
export { formatUnits } from "./src/format-units";
5-
export { parseMina } from "./src/parse-mina";
6-
export { parseUnits } from "./src/parse-units";
3+
export { formatMina } from "./format-mina";
4+
export { formatUnits } from "./format-units";
5+
export { parseMina } from "./parse-mina";
6+
export { parseUnits } from "./parse-units";
7+
export { createRpc, createRpcHandler } from "./worker-rpc";
78
export * as Test from "./test/constants";

0 commit comments

Comments
 (0)