mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-22 08:18:13 +08:00
Add frontend file structure docs and some related cleanup
This commit is contained in:
@@ -25,16 +25,18 @@ export async function initWasm(): Promise<void> {
|
||||
// Should be called after running `initWasm()` and its promise resolving
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
||||
export function createEditor() {
|
||||
// Functions from `api.rs` defined directly on the WASM module, not the editor instance (generated by wasm-bindgen)
|
||||
const raw: WasmRawInstance = getWasmInstance();
|
||||
// Raw: Object containing several callable functions from `editor_api.rs` defined directly on the WASM module, not the editor instance (generated by wasm-bindgen)
|
||||
if (!wasmImport) throw new Error("Editor WASM backend was not initialized at application startup");
|
||||
const raw: WasmRawInstance = wasmImport;
|
||||
|
||||
// Functions from `api.rs` that are part of the editor instance (generated by wasm-bindgen)
|
||||
const instance: WasmEditorInstance = new raw.JsEditorHandle(invokeJsMessageSubscription);
|
||||
function invokeJsMessageSubscription(messageType: JsMessageType, data: Record<string, unknown>): void {
|
||||
subscriptions.handleJsMessage(messageType, data, raw, instance);
|
||||
}
|
||||
// Instance: Object containing many functions from `editor_api.rs` that are part of the editor instance (generated by wasm-bindgen)
|
||||
const instance: WasmEditorInstance = new raw.JsEditorHandle((messageType: JsMessageType, messageData: Record<string, unknown>): void => {
|
||||
// This callback is called by WASM when a FrontendMessage is received from the WASM wrapper editor instance
|
||||
// We pass along the first two arguments then add our own `raw` and `instance` context for the last two arguments
|
||||
subscriptions.handleJsMessage(messageType, messageData, raw, instance);
|
||||
});
|
||||
|
||||
// Allows subscribing to messages in JS that are sent from the WASM backend
|
||||
// Subscriptions: Allows subscribing to messages in JS that are sent from the WASM backend
|
||||
const subscriptions: SubscriptionRouter = createSubscriptionRouter();
|
||||
|
||||
return {
|
||||
@@ -43,8 +45,3 @@ export function createEditor() {
|
||||
subscriptions,
|
||||
};
|
||||
}
|
||||
|
||||
export function getWasmInstance(): WasmRawInstance {
|
||||
if (wasmImport) return wasmImport;
|
||||
throw new Error("Editor WASM backend was not initialized at application startup");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user