wip
This commit is contained in:
74
playground/src/lib/wasm.ts
Normal file
74
playground/src/lib/wasm.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { readable } from 'svelte/store';
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
Go: {
|
||||
new (): {
|
||||
run: (inst: WebAssembly.Instance) => Promise<void>;
|
||||
importObject: WebAssembly.Imports;
|
||||
};
|
||||
};
|
||||
markaParseFile: (input: string) => string;
|
||||
markaParseFileWithTemplate: (markdown: string, template: string) => string;
|
||||
markaListTemplates: () => string;
|
||||
markaGetTemplate: (name: string) => string;
|
||||
}
|
||||
}
|
||||
|
||||
export const wasmReady = readable(false, (set) => {
|
||||
if (typeof window === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
const loadWasm = async () => {
|
||||
const go = new window.Go();
|
||||
try {
|
||||
const result = await WebAssembly.instantiateStreaming(fetch('/main.wasm'), go.importObject);
|
||||
go.run(result.instance);
|
||||
set(true);
|
||||
} catch (error) {
|
||||
console.error('Error loading wasm module:', error);
|
||||
}
|
||||
};
|
||||
|
||||
if (document.readyState === 'complete') {
|
||||
loadWasm();
|
||||
} else {
|
||||
window.addEventListener('load', loadWasm);
|
||||
}
|
||||
});
|
||||
|
||||
export interface ParseResult {
|
||||
data: unknown;
|
||||
timings: { [key: string]: number };
|
||||
}
|
||||
export function parseMarkdown(markdown: string): ParseResult {
|
||||
if (typeof window.markaParseFile !== 'function') {
|
||||
throw new Error('Wasm module not ready');
|
||||
}
|
||||
const result = window.markaParseFile(markdown);
|
||||
return JSON.parse(result);
|
||||
}
|
||||
|
||||
export function parseMarkdownWithTemplate(markdown: string, template: string): ParseResult {
|
||||
if (typeof window.markaParseFileWithTemplate !== 'function') {
|
||||
throw new Error('Wasm module not ready');
|
||||
}
|
||||
const result = window.markaParseFileWithTemplate(markdown, template);
|
||||
return JSON.parse(result);
|
||||
}
|
||||
|
||||
export function listTemplates(): string[] {
|
||||
if (typeof window.markaListTemplates !== 'function') {
|
||||
throw new Error('Wasm module not ready');
|
||||
}
|
||||
const result = window.markaListTemplates();
|
||||
return JSON.parse(result);
|
||||
}
|
||||
|
||||
export function getTemplate(name: string): string {
|
||||
if (typeof window.markaGetTemplate !== 'function') {
|
||||
throw new Error('Wasm module not ready');
|
||||
}
|
||||
return window.markaGetTemplate(name);
|
||||
}
|
Reference in New Issue
Block a user