1313 * See LICENSE file for details or contact [email protected] 1414 */
1515
16- import init , * as wasm from "@dev-utility/core" ;
16+ // #v-ifdef WASM
17+ import wasmFunctions from "./invoke.wasm" ;
18+ // #v-endif
19+
1720import { type InvokeOptions , invoke as invokeCore } from "@tauri-apps/api/core" ;
1821import useSWRMutation , {
1922 type SWRMutationConfiguration ,
@@ -30,9 +33,10 @@ import {
3033 type TotpSecret ,
3134 type TotpValidationResult ,
3235} from "./types" ;
36+
3337export const IS_TAURI = "__TAURI__" in window ;
3438
35- interface UtilitiesArgs {
39+ export interface UtilitiesArgs {
3640 [ InvokeFunction . GenerateUlid ] : { count : number } ;
3741 [ InvokeFunction . GenerateNanoid ] : { count : number } ;
3842 [ InvokeFunction . GenerateUuidV4 ] : { count : number } ;
@@ -70,7 +74,7 @@ interface UtilitiesArgs {
7074 } ;
7175}
7276
73- interface UtilitiesReturns {
77+ export interface UtilitiesReturns {
7478 [ InvokeFunction . GenerateUlid ] : string ;
7579 [ InvokeFunction . GenerateNanoid ] : string ;
7680 [ InvokeFunction . GenerateUuidV4 ] : string ;
@@ -87,31 +91,19 @@ interface UtilitiesReturns {
8791 [ InvokeFunction . ValidateTotpCode ] : TotpValidationResult ;
8892}
8993
90- type WasmFunctions = {
91- [ K in keyof UtilitiesArgs ] : ( args : UtilitiesArgs [ K ] ) => UtilitiesReturns [ K ] ;
92- } ;
93- const wasmFunctions : Partial < WasmFunctions > = {
94- [ InvokeFunction . GenerateUlid ] : ( args ) => wasm . generate_ulid ( args . count ) ,
95- // [InvokeFunction.GenerateNanoid]: (args) => wasm.generate_nanoid(args.count),
96- [ InvokeFunction . GenerateUuidV4 ] : ( args ) => wasm . generate_uuid_v4 ( args . count ) ,
97- [ InvokeFunction . GenerateUuidV7 ] : ( args ) => wasm . generate_uuid_v7 ( args . count ) ,
98- [ InvokeFunction . FormatJson ] : ( args ) => wasm . format_json ( args . input , args . style ) ,
99- [ InvokeFunction . FormatCss ] : ( args ) => wasm . format_css ( args . input ) ,
100- // [InvokeFunction.GenerateHashes]: (args) => wasm.generate_hashes(args.input),
101- [ InvokeFunction . EncodeBase64 ] : ( args ) => wasm . encode_base64 ( args . input ) ,
102- [ InvokeFunction . DecodeBase64 ] : ( args ) => wasm . decode_base64 ( args . input ) ,
103- } ;
104-
10594export async function utilityInvoke < T extends InvokeFunction > (
10695 cmd : T ,
10796 args : UtilitiesArgs [ T ] ,
10897 options ?: InvokeOptions
10998) : Promise < UtilitiesReturns [ T ] > {
11099 if ( IS_TAURI ) {
111100 return invokeCore ( cmd , args , options ) ;
112- } else if ( cmd in wasmFunctions ) {
101+ }
102+ // #v-ifdef WASM
103+ else if ( cmd in wasmFunctions ) {
113104 return wasmFunctions [ cmd ] ! ( args ) ;
114105 }
106+ // #v-endif
115107 throw new Error ( `Function ${ cmd } not found` ) ;
116108}
117109
0 commit comments