Skip to content

Commit

Permalink
Merge pull request #634 from finos/node-async-fix
Browse files Browse the repository at this point in the history
Fixed async loading of node.js module
  • Loading branch information
texodus authored Jun 27, 2019
2 parents ed90f78 + 364bc8d commit 35fb44a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
16 changes: 11 additions & 5 deletions packages/perspective/src/js/API/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
21 changes: 12 additions & 9 deletions packages/perspective/src/js/perspective.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;

Expand Down
12 changes: 7 additions & 5 deletions packages/perspective/test/js/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
16 changes: 16 additions & 0 deletions packages/perspective/test/js/sync_load.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 35fb44a

Please sign in to comment.