|
| 1 | +<script> |
| 2 | + import { onMount, tick } from 'svelte'; |
| 3 | + import * as wurbo from 'wurbo'; |
| 4 | +
|
| 5 | + // Import wasm component bytes as a url |
| 6 | + import wasmURL from '../../../aggregate.wasm?url'; |
| 7 | +
|
| 8 | + // get imports from +page.svelte |
| 9 | + export let importables; |
| 10 | + export let load; |
| 11 | +
|
| 12 | + /** |
| 13 | + * The rendered component as a string of HTML |
| 14 | + * @type {string | null} |
| 15 | + */ |
| 16 | + let renderedHTML; |
| 17 | + /** |
| 18 | + * The module that loads the WebAssembly component |
| 19 | + * |
| 20 | + * @type {{ render: (arg0: string) => string | null; listen: () => void; }} |
| 21 | + */ |
| 22 | + let mod; |
| 23 | +
|
| 24 | + onMount(async () => { |
| 25 | + // ensure you are in the Browser environment to rollup your WIT Component |
| 26 | + // const { load } = await import('rollup-plugin-wit-component'); |
| 27 | +
|
| 28 | + let listener = new wurbo.Listener(); |
| 29 | +
|
| 30 | + // get your wasm bytes from your storage source |
| 31 | + let wasmBytes = await fetch(wasmURL).then((res) => res.arrayBuffer()); |
| 32 | +
|
| 33 | + // define the import handles you are giving to your component |
| 34 | + let all_importables = [ |
| 35 | + { 'component:wurbo/wurbo-in': importables.buildCodeString(listener.namespace) }, |
| 36 | + { |
| 37 | + 'component:wallet/[email protected]': importables.buildGetSeedFunc( |
| 38 | + 'build a test seed from this string' |
| 39 | + ) |
| 40 | + } |
| 41 | + ]; |
| 42 | +
|
| 43 | + // load the import handles into the Wasm component and get the ES module returned |
| 44 | + mod = await load(wasmBytes, all_importables); |
| 45 | +
|
| 46 | + // call `render` with your inputs for the component |
| 47 | + let data = { |
| 48 | + tag: 'all-content', |
| 49 | + val: { |
| 50 | + page: { title: 'Use that seed to sign something.' }, |
| 51 | + input: { placeholder: 'Enter a Signable Message here.' } |
| 52 | + } |
| 53 | + }; |
| 54 | + renderedHTML = mod.wurboOut.render(data); |
| 55 | +
|
| 56 | + // lisen for events from the component |
| 57 | + listener.listen(mod); |
| 58 | + }); |
| 59 | +
|
| 60 | + // Once the HTML is rendered and the module is loaded, we can activate the event emitters |
| 61 | + $: if (renderedHTML && mod) |
| 62 | + (async () => { |
| 63 | + // wait for the DOM to reflect the updates first |
| 64 | + await tick(); |
| 65 | + // once the DOM has our elements loaded, we can activate the event emitters |
| 66 | + mod.wurboOut.activate(); |
| 67 | + })(); |
| 68 | +</script> |
| 69 | + |
| 70 | +<svelte:head> |
| 71 | + <title>Seed Keeper</title> |
| 72 | + <script src="https://cdn.tailwindcss.com"></script> |
| 73 | +</svelte:head> |
| 74 | +<div> |
| 75 | + <h1>Aggregate User Interfaces</h1> |
| 76 | + {#if renderedHTML} |
| 77 | + {@html renderedHTML} |
| 78 | + {/if} |
| 79 | +</div> |
0 commit comments