Skip to content

Add more JS/TS Framework support by adding the ability to choose the … #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

const bindings = require("./pkg/matrix_sdk_crypto_wasm_bg.cjs");

const moduleUrl = require.resolve("./pkg/matrix_sdk_crypto_wasm_bg.wasm");
const defaultURL = require.resolve("./pkg/matrix_sdk_crypto_wasm_bg.wasm");

// We want to throw an error if the user tries to use the bindings before
// calling `initAsync`.
Expand All @@ -47,10 +47,11 @@ let modPromise = null;
/**
* Loads and instantiates the WASM module asynchronously
*
* @param {URL | string} url - The URL to fetch the WebAssembly module from
* @returns {Promise<void>}
*/
async function loadModuleAsync() {
const { instance } = await WebAssembly.instantiateStreaming(fetch(moduleUrl), {
async function loadModuleAsync(url) {
const { instance } = await WebAssembly.instantiateStreaming(fetch(url), {
// @ts-expect-error: The bindings don't exactly match the 'ExportValue' type
"./matrix_sdk_crypto_wasm_bg.js": bindings,
});
Expand All @@ -65,10 +66,11 @@ async function loadModuleAsync() {
*
* Returns a promise which will resolve once the other methods are ready.
*
* @param {URL | string} [url] - The URL to fetch the WebAssembly module from. If not provided, a default URL will be used.
* @returns {Promise<void>}
*/
async function initAsync() {
if (!modPromise) modPromise = loadModuleAsync();
async function initAsync(url = defaultURL) {
if (!modPromise) modPromise = loadModuleAsync(url);
await modPromise;
}

Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export * from "./pkg/matrix_sdk_crypto_wasm.js";
* Load the WebAssembly module in the background, if it has not already been loaded.
*
* Returns a promise which will resolve once the other methods are ready.
*
* @param {URL | string} [url] - The URL to fetch the WebAssembly module from. If not provided, a default URL will be used.
* @returns {Promise<void>}
*/
export declare function initAsync(): Promise<void>;
export declare function initAsync(url?: URL | string): Promise<void>;

// The auto-generated typescript definitions are a good start, but could do with tightening up in a lot of areas.
// The following is a manually-curated set of typescript definitions.
Expand Down
12 changes: 7 additions & 5 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import * as bindings from "./pkg/matrix_sdk_crypto_wasm_bg.js";

const moduleUrl = new URL("./pkg/matrix_sdk_crypto_wasm_bg.wasm", import.meta.url);
const defaultURL = new URL("./pkg/matrix_sdk_crypto_wasm_bg.wasm", import.meta.url);

// Although we could simply instantiate the WASM at import time with a top-level `await`,
// we avoid that, to make it easier for callers to delay loading the WASM (and instead
Expand Down Expand Up @@ -52,10 +52,11 @@ let modPromise = null;
/**
* Loads and instantiates the WASM module asynchronously
*
* @param {URL | string} url - The URL to fetch the WebAssembly module from
* @returns {Promise<void>}
*/
async function loadModuleAsync() {
const { instance } = await WebAssembly.instantiateStreaming(fetch(moduleUrl), {
async function loadModuleAsync(url) {
const { instance } = await WebAssembly.instantiateStreaming(fetch(url), {
// @ts-expect-error: The bindings don't exactly match the 'ExportValue' type
"./matrix_sdk_crypto_wasm_bg.js": bindings,
});
Expand All @@ -70,10 +71,11 @@ async function loadModuleAsync() {
*
* Returns a promise which will resolve once the other methods are ready.
*
* @param {URL | string} [url] - The URL to fetch the WebAssembly module from. If not provided, a default URL will be used.
* @returns {Promise<void>}
*/
export async function initAsync() {
if (!modPromise) modPromise = loadModuleAsync();
export async function initAsync(url = defaultURL) {
if (!modPromise) modPromise = loadModuleAsync(url);
await modPromise;
}

Expand Down