/ minisign / useRust / useRust.js
useRust.js
 1  import { createSignal, batch } from "solid-js";
 2  import * as rustAll from "./wasm/wasm.js";
 3  
 4  // Get 'rustRest', which does not have default() and initSync()
 5  // eslint-disable-next-line @typescript-eslint/no-unused-vars
 6  const { default: _, initSync: __, ...rustRest } = rustAll;
 7  
 8  const useRust = (config = { autoInit: true }) => {
 9    const [rust, setRust] = createSignal(undefined);
10    const [error, setError] = createSignal(undefined);
11    const [isLoading, setIsLoading] = createSignal(config.autoInit);
12  
13    // eslint-disable-next-line camelcase
14    const init = async () => {
15      setIsLoading(true);
16      let returnValue;
17      try {
18        returnValue = await rustAll.default();
19      } catch (e) {
20        batch(() => {
21          setRust(undefined);
22          setError(e);
23          setIsLoading(false);
24        });
25        return;
26      }
27      batch(() => {
28        setRust(rustRest);
29        setError(undefined);
30        setIsLoading(false);
31      });
32      return returnValue;
33    };
34  
35    if (config.autoInit) init();
36  
37    return { rust, error, isLoading, init };
38  };
39  
40  export default useRust;