Skip to content

Commit

Permalink
Fix wasm initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
printfn committed May 2, 2024
1 parent 32d877a commit d426848
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 76 deletions.
120 changes: 44 additions & 76 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
import {
evaluateFendWithTimeout,
evaluateFendWithVariablesJson,
default as initWasm,
initialiseWithHandlers,
} from 'fend-wasm';
import {
type FormEvent,
type KeyboardEvent,
type ReactNode,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { getExchangeRates } from './lib/exchange-rates';
import { type FormEvent, type KeyboardEvent, type ReactNode, useCallback, useEffect, useRef, useState } from 'react';
import { fend } from './lib/wasm';

const examples = `
> 5'10" to cm
Expand Down Expand Up @@ -46,36 +31,22 @@ const exampleContent = (
</p>
);

async function load() {
try {
await initWasm();
initialiseWithHandlers(await getExchangeRates());

const result = evaluateFendWithTimeout('1 + 2', 500);
if (result !== '3') {
alert('Failed to initialise WebAssembly');
return;
}
} catch (e) {
console.error(e);
alert('Failed to initialise WebAssembly');
return;
}
}
await load();

export default function App({ widget = false }: { widget?: boolean }) {
const [currentInput, setCurrentInput] = useState('');
const [output, setOutput] = useState<ReactNode>(widget ? <></> : exampleContent);
const [history, setHistory] = useState<string[]>([]);
const [variables, setVariables] = useState('');
const [navigation, setNavigation] = useState(0);
const hint = useMemo<string>(() => {
const result = JSON.parse(evaluateFendWithVariablesJson(currentInput, 100, variables));
if (!result.ok) {
return '';
}
return result.result;
const [hint, setHint] = useState('');
useEffect(() => {
(async () => {
const result = await fend(currentInput, 100, variables);
if (!result.ok) {
setHint('');
} else {
setHint(result.result);
}
})();
}, [currentInput, variables]);
const inputText = useRef<HTMLTextAreaElement>(null);
const inputHint = useRef<HTMLParagraphElement>(null);
Expand All @@ -93,37 +64,39 @@ export default function App({ widget = false }: { widget?: boolean }) {
}, []);
const evaluate = useCallback(
(event: KeyboardEvent<HTMLTextAreaElement>) => {
// allow multiple lines to be entered if shift, ctrl
// or meta is held, otherwise evaluate the expression
if (!(event.key === 'Enter' && !event.shiftKey && !event.ctrlKey && !event.metaKey)) {
return;
}
event.preventDefault();
if (currentInput.trim() === 'clear') {
(async () => {
// allow multiple lines to be entered if shift, ctrl
// or meta is held, otherwise evaluate the expression
if (!(event.key === 'Enter' && !event.shiftKey && !event.ctrlKey && !event.metaKey)) {
return;
}
event.preventDefault();
if (currentInput.trim() === 'clear') {
setCurrentInput('');
setOutput(null);
return;
}
const request = <p>{`> ${currentInput}`}</p>;
if (currentInput.trim().length > 0) {
setHistory(h => [...h, currentInput]);
}
setNavigation(0);
const fendResult = await fend(currentInput, 500, variables);
setCurrentInput('');
setOutput(null);
return;
}
const request = <p>{`> ${currentInput}`}</p>;
if (currentInput.trim().length > 0) {
setHistory(h => [...h, currentInput]);
}
setNavigation(0);
const fendResult = JSON.parse(evaluateFendWithVariablesJson(currentInput, 500, variables));
setCurrentInput('');
console.log(fendResult);
const result = <p>{fendResult.ok ? fendResult.result : fendResult.message}</p>;
if (fendResult.ok && fendResult.variables.length > 0) {
setVariables(fendResult.variables);
}
setOutput(o => (
<>
{o}
{request}
{result}
</>
));
inputHint.current?.scrollIntoView();
console.log(fendResult);
const result = <p>{fendResult.ok ? fendResult.result : fendResult.message}</p>;
if (fendResult.ok && fendResult.variables.length > 0) {
setVariables(fendResult.variables);
}
setOutput(o => (
<>
{o}
{request}
{result}
</>
));
inputHint.current?.scrollIntoView();
})();
},
[currentInput, variables],
);
Expand All @@ -147,11 +120,6 @@ export default function App({ widget = false }: { widget?: boolean }) {
},
[currentInput, navigation, history],
);
useEffect(() => {
(async () => {
await load();
})();
}, []);
useEffect(() => {
document.addEventListener('click', focus);
return () => {
Expand Down
32 changes: 32 additions & 0 deletions web/src/lib/wasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
evaluateFendWithTimeout,
evaluateFendWithVariablesJson,
default as initWasm,
initialiseWithHandlers,
} from 'fend-wasm';
import { getExchangeRates } from './exchange-rates';

async function load() {
try {
const [, exchangeRates] = await Promise.all([initWasm(), getExchangeRates()]);
initialiseWithHandlers(exchangeRates);

const result = evaluateFendWithTimeout('1 + 2', 500);
if (result !== '3') {
alert('Failed to initialise WebAssembly');
return;
}
} catch (e) {
console.error(e);
alert('Failed to initialise WebAssembly');
return;
}
}
await load();

type FendResult = { ok: true; result: string; variables: string } | { ok: false; message: string };

export async function fend(input: string, timeout: number, variables: string) {
const res: FendResult = JSON.parse(evaluateFendWithVariablesJson(input, timeout, variables));
return res;
}

0 comments on commit d426848

Please sign in to comment.