-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into feature/presentation
- Loading branch information
Showing
24 changed files
with
311 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Utils [Don't waste time on implementing these yourself.] | ||
|
||
We've implemented some utilities that you might find useful. You can find them in the `@mina-js/utils` package. | ||
|
||
## Units | ||
|
||
Utils library provides functions for unit conversion. | ||
|
||
### formatMina | ||
|
||
Formats micro-Mina to human-readable Mina value. | ||
|
||
```typescript twoslash | ||
import { formatMina } from '@mina-js/utils' | ||
|
||
const mina = formatMina(5_000_000_000n) | ||
// -> "5" | ||
``` | ||
|
||
### parseMina | ||
|
||
Parses human-readable Mina value to micro-Mina. | ||
|
||
```typescript twoslash | ||
import { parseMina } from '@mina-js/utils' | ||
|
||
const mina = parseMina('5') | ||
// -> 5_000_000_000n | ||
``` | ||
|
||
### formatUnit | ||
|
||
```typescript twoslash | ||
import { formatUnits } from '@mina-js/utils' | ||
|
||
const formatted = formatUnits(4200000000000n, 10) | ||
// -> "420" | ||
``` | ||
|
||
### parseUnits | ||
|
||
```typescript twoslash | ||
import { parseUnits } from '@mina-js/utils' | ||
|
||
const parsed = parseUnits("420", 10) | ||
// -> 4200000000000n | ||
``` | ||
|
||
## Web Workers | ||
|
||
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. | ||
|
||
```typescript twoslash | ||
// @filename: worker.ts | ||
import { createRpcHandler } from "@mina-js/utils"; | ||
|
||
const { messageHandler } = createRpcHandler({ | ||
methods: { | ||
ping: async () => 'pong', | ||
} | ||
}) | ||
|
||
self.onmessage = messageHandler | ||
|
||
// @filename: index.ts | ||
import { createRpc } from "@mina-js/utils"; | ||
|
||
const worker = new Worker(new URL('./worker.ts', import.meta.url)) | ||
const rpc = createRpc({ worker }) | ||
const response = await rpc.request({ | ||
method: 'ping', | ||
params: [], | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
export * from "./types"; | ||
export * from "./validation"; | ||
export { formatMina } from "./src/format-mina"; | ||
export { formatUnits } from "./src/format-units"; | ||
export { parseMina } from "./src/parse-mina"; | ||
export { parseUnits } from "./src/parse-units"; | ||
export { formatMina } from "./format-mina"; | ||
export { formatUnits } from "./format-units"; | ||
export { parseMina } from "./parse-mina"; | ||
export { parseUnits } from "./parse-units"; | ||
export { createRpc, createRpcHandler } from "./worker-rpc"; | ||
export * as Test from "./test/constants"; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createRpcHandler } from "../worker-rpc"; | ||
|
||
const { messageHandler } = createRpcHandler({ | ||
methods: { | ||
ping: async () => "pong", | ||
}, | ||
}); | ||
|
||
self.onmessage = messageHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { beforeAll, describe, expect, it, mock } from "bun:test"; | ||
import { createRpc, createRpcHandler } from "./worker-rpc"; | ||
|
||
describe("Worker RPC", () => { | ||
let worker: Worker; | ||
|
||
beforeAll(() => { | ||
worker = new Worker(new URL("./test/worker.ts", import.meta.url)); | ||
}); | ||
|
||
it("creates RPC handler", async () => { | ||
const mockedHandler = mock(async () => "pong"); | ||
const { messageHandler } = createRpcHandler({ | ||
methods: { | ||
ping: mockedHandler, | ||
}, | ||
}); | ||
await messageHandler( | ||
new MessageEvent("message", { data: { method: "ping", params: [] } }), | ||
); | ||
expect(mockedHandler).toHaveBeenCalled(); | ||
}); | ||
|
||
it("exchanges messages with Web Worker", async () => { | ||
const rpc = createRpc({ worker }); | ||
const response = await rpc.request({ method: "ping", params: [] }); | ||
expect(response.result).toBe("pong"); | ||
}); | ||
|
||
it("calls non-existing method", async () => { | ||
const rpc = createRpc({ worker }); | ||
expect(rpc.request({ method: "pang", params: [] })).rejects.toThrow(); | ||
}); | ||
}); |
Oops, something went wrong.