1.2.3 • Published 11 months ago

use-wasm-ts v1.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

use-wasm-ts

Version Build Size GitHub Workflow Status (with branch) GitHub

Use WebAssembly in React in a simple way with hooks and Typescript!

npm install use-wasm-ts

This package allows you to load and use your WebAssembly functions inside React easily.

Usage

import { FC } from 'react';
import { useWasm } from 'use-wasm-ts';

const MyComponent: FC = () => {
    const {
        isLoading,
        fn: { compute },
    } = useWasm<{ compute: (n: number) => number }>('compute.wasm');

	return (
        <>
            <p>{isLoading ? "Loading..." : `Loaded with ${compute(10)}`}</p>
        </>
    );
};

export default MyComponent;

Show a live demo on CodeSandbox.

API

useWasm

useWasm<T>(url: string, options?: useWasmOptions): {
    isLoading: boolean;
    fn: T;
    module: WebAssembly.Module;
    instance: WebAssembly.Instance;
    memory: WebAssembly.Memory;
};

url

Type: string

The url of the WebAssembly file.

options

Type: useWasmOptions

streaming: boolean (default: false)

The streaming option allows you to load the WebAssembly file in streaming mode. Before using it, make sure that your server serves the file with the correct MIME type.

fetchOptions: RequestInit (default: {})

The fetchOptions option allows you to pass options to the fetch function.

memory: WebAssembly.Memory (default: { initial: 256, maximum: 512 })

The memory option allows you to pass a custom memory to the WebAssembly file.

env: (memory: WebAssembly.Memory) => { [key: string]: any } (default: {})

The env option allows you to pass a custom environment to the WebAssembly file. The function takes the default / custom memory as an optionnal parameter and must return an object.

License

MIT

1.2.3

11 months ago

1.2.2

12 months ago

1.2.0

1 year ago

1.1.3

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago