diff --git a/packages/perspective/src/js/API/client.js b/packages/perspective/src/js/API/client.js index 5f73cb7744..8474b2c1c8 100644 --- a/packages/perspective/src/js/API/client.js +++ b/packages/perspective/src/js/API/client.js @@ -95,13 +95,19 @@ Client.prototype._handle = function(e) { window.dispatchEvent(event); _initialized = true; } - for (var m in this._worker.messages) { - if (this._worker.messages.hasOwnProperty(m)) { - this._worker.messages[m](); - } - } + + const msgs = this._worker.messages; + this._worker.initialized.value = true; this._worker.messages = []; + + if (msgs) { + for (const m in msgs) { + if (msgs.hasOwnProperty(m)) { + setTimeout(msgs[m]); + } + } + } } if (e.data.id) { var handler = this._worker.handlers[e.data.id]; diff --git a/packages/perspective/src/js/perspective.node.js b/packages/perspective/src/js/perspective.node.js index c5105fb2d8..29c0507aa5 100644 --- a/packages/perspective/src/js/perspective.node.js +++ b/packages/perspective/src/js/perspective.node.js @@ -29,18 +29,21 @@ const wasm = require("./psp.async.wasm.js"); const buffer = fs.readFileSync(path.join(__dirname, wasm)).buffer; -const sync_module = perspective( - load_perspective({ - wasmBinary: buffer, - wasmJSMethod: "native-wasm" - }) -); - const SYNC_SERVER = new (class extends Server { + init(msg) { + load_perspective({ + wasmBinary: buffer, + wasmJSMethod: "native-wasm" + }).then(core => { + this.perspective = perspective(core); + this.post(msg); + }); + } + post(msg) { SYNC_CLIENT._handle({data: msg}); } -})(sync_module); +})(); const SYNC_CLIENT = new (class extends Client { send(msg) { @@ -51,7 +54,7 @@ const SYNC_CLIENT = new (class extends Client { SYNC_CLIENT.send({id: -1, cmd: "init"}); module.exports = SYNC_CLIENT; -module.exports.sync_module = () => sync_module; +module.exports.sync_module = () => SYNC_SERVER.perspective; let CLIENT_ID_GEN = 0; diff --git a/packages/perspective/test/js/internal.js b/packages/perspective/test/js/internal.js index 07d1aba9f0..96a0e88e42 100644 --- a/packages/perspective/test/js/internal.js +++ b/packages/perspective/test/js/internal.js @@ -15,18 +15,20 @@ const arrow = fs.readFileSync(path.join(__dirname, "..", "arrow", "test-null.arr var arrow_psp_internal_schema = [9, 10, 1, 2, 3, 4, 11, 19, 19, 12, 12, 12, 2]; module.exports = (perspective, mode) => { - // Get the internal module; - if (perspective.sync_module) { - perspective = perspective.sync_module(); - } - describe("Internal API", function() { it("is actually using the correct runtime", async function() { + // Get the internal module; + if (perspective.sync_module) { + perspective = perspective.sync_module(); + } expect(perspective.__module__.wasmJSMethod).toEqual(mode === "ASMJS" ? "asmjs" : "native-wasm"); }); it("Arrow schema types are mapped correctly", async function() { // This only works for non parallel + if (perspective.sync_module) { + perspective = perspective.sync_module(); + } var table = perspective.table(arrow.slice()); let schema, stypes; let types = []; diff --git a/packages/perspective/test/js/sync_load.spec.js b/packages/perspective/test/js/sync_load.spec.js new file mode 100644 index 0000000000..e6c23f3509 --- /dev/null +++ b/packages/perspective/test/js/sync_load.spec.js @@ -0,0 +1,16 @@ +/****************************************************************************** + * + * Copyright (c) 2017, the Perspective Authors. + * + * This file is part of the Perspective library, distributed under the terms of + * the Apache License 2.0. The full license can be found in the LICENSE file. + * + */ + +describe("perspective.js module", function() { + it("does not access the WASM module until it is ready", async () => { + const tbl = require("../../build/perspective.node.js").table([{x: 1}]); + const size = await tbl.size(); + expect(size).toEqual(1); + }); +});