Skip to content

Commit

Permalink
Initialize the citeproc-rs wasm module exactly once; wait if in-flight.
Browse files Browse the repository at this point in the history
  • Loading branch information
cormacrelf committed Dec 1, 2020
1 parent 08b6dc4 commit 5a45aa8
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions chrome/content/zotero/xpcom/citeproc-rs-to-citeproc-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,38 @@
// Zotero.Prefs.set('cite.useCiteprocRs', true);

Zotero.CiteprocRs = {
wasmInitPromise: null,

init: async function () {
Zotero.debug("require('citeproc_rs_wasm_include')");
const { CiteprocRsError } = require('citeproc_rs_wasm_include')
Zotero.debug("require('citeproc_rs_wasm')");
let init = require('citeproc_rs_wasm');
// Initialize the wasm code
Zotero.debug("Loading citeproc-rs wasm binary");
const xhr = await Zotero.HTTP.request('GET', 'resource://zotero/citeproc_rs_wasm_bg.wasm', {
responseType: "arraybuffer"
});
Zotero.debug("Initializing the CiteprocRs wasm driver");
await init(Promise.resolve(xhr.response));
Zotero.debug("CiteprocRs driver initialized successfully");
if (Zotero.CiteprocRs.wasmInitPromise != null) {
// will return on next tick if already loaded
// or if not complete, will wait for it to finish.
Zotero.debug("CiteprocRs: Already loaded or started to load wasm module");
await Zotero.CiteprocRs.wasmInitPromise;
} else {
async function wasmInit() {
// First load the JS code that interacts with the wasm
Zotero.debug("require('citeproc_rs_wasm_include')");
require('citeproc_rs_wasm_include')
Zotero.debug("require('citeproc_rs_wasm')");
let initWasmModule = require('citeproc_rs_wasm');

// The Driver & parseStyleMetadata are now stored on Zotero.CiteprocRs
// As are the helpers/error classes in citeproc_rs_wasm_include.
// However, Driver/parseStyleMetadata will not be able to work until
// the wasm itself is loaded.

// Initialize the wasm itself
Zotero.debug("Loading citeproc-rs wasm binary");
const xhr = await Zotero.HTTP.request('GET', 'resource://zotero/citeproc_rs_wasm_bg.wasm', {
responseType: "arraybuffer"
});
Zotero.debug("Initializing the CiteprocRs wasm module");
await initWasmModule(Promise.resolve(xhr.response));
Zotero.debug("CiteprocRs wasm module initialized successfully");
}
Zotero.CiteprocRs.wasmInitPromise = wasmInit();
}
},

Engine: class {
Expand Down

0 comments on commit 5a45aa8

Please sign in to comment.