From 9875e6f342aea3469b3eff4b63b4f4db2fe34577 Mon Sep 17 00:00:00 2001 From: Andrew Stein Date: Fri, 19 Apr 2019 15:31:27 -0400 Subject: [PATCH 1/4] Added arrow support to remote perspective API --- examples/git_history/chained.html | 54 +++++++++++++++++++ .../perspective/src/js/perspective.node.js | 10 +++- .../src/js/perspective.parallel.js | 14 ++++- 3 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 examples/git_history/chained.html diff --git a/examples/git_history/chained.html b/examples/git_history/chained.html new file mode 100644 index 0000000000..d7f83f44d1 --- /dev/null +++ b/examples/git_history/chained.html @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/packages/perspective/src/js/perspective.node.js b/packages/perspective/src/js/perspective.node.js index ccc66d67b5..90eed24ef3 100644 --- a/packages/perspective/src/js/perspective.node.js +++ b/packages/perspective/src/js/perspective.node.js @@ -186,8 +186,14 @@ class WebSocketHost extends module.exports.Host { }); } - post(msg) { - this.REQS[msg.id].send(JSON.stringify(msg)); + post(msg, transferable) { + if (transferable) { + msg.is_transferable = true; + this.REQS[msg.id].send(JSON.stringify(msg)); + this.REQS[msg.id].send(transferable[0]); + } else { + this.REQS[msg.id].send(JSON.stringify(msg)); + } delete this.REQS[msg.id]; } diff --git a/packages/perspective/src/js/perspective.parallel.js b/packages/perspective/src/js/perspective.parallel.js index c04f20ba1a..8acb5c9182 100644 --- a/packages/perspective/src/js/perspective.parallel.js +++ b/packages/perspective/src/js/perspective.parallel.js @@ -116,6 +116,7 @@ class WebSocketWorker extends worker { pingTimeout: 15000, pingMsg: "heartbeat" }); + this._ws.ws.binaryType = "arraybuffer"; this._ws.onopen = () => { this.send({id: -1, cmd: "init"}); }; @@ -123,7 +124,18 @@ class WebSocketWorker extends worker { if (msg.data === "heartbeat") { return; } - this._handle({data: JSON.parse(msg.data)}); + if (this._pending_arrow) { + this._handle({data: {id: this._pending_arrow, data: msg.data}}); + delete this._pending_arrow; + } else { + msg = JSON.parse(msg.data); + if (msg.is_transferable) { + console.warn("Arrow transfer detected!"); + this._pending_arrow = msg.id; + } else { + this._handle({data: msg}); + } + } }; } From fde5895ca1fafae054a9ab639de7516e5229e5e4 Mon Sep 17 00:00:00 2001 From: Andrew Stein Date: Fri, 19 Apr 2019 16:10:18 -0400 Subject: [PATCH 2/4] Add support for hosting `view()` as well as `table()` --- examples/git_history/chained.html | 4 +--- examples/git_history/server.js | 3 ++- packages/perspective/src/js/api.js | 13 ++++++++++++- packages/perspective/src/js/perspective.node.js | 8 ++++++-- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/examples/git_history/chained.html b/examples/git_history/chained.html index d7f83f44d1..181f6c197f 100644 --- a/examples/git_history/chained.html +++ b/examples/git_history/chained.html @@ -41,10 +41,8 @@ window.addEventListener('WebComponentsReady', async function() { var elem = document.getElementById('view1'); var worker = perspective.worker(window.location.origin.replace('http', 'ws')); - let table = worker.open('data_source_one'); - let view = table.view(); + let view = worker.open_view('data_source_one'); let arrow = await view.to_arrow(); - view.delete(); elem.load(perspective.worker().table(arrow)); }); diff --git a/examples/git_history/server.js b/examples/git_history/server.js index e065996664..19c219b86f 100644 --- a/examples/git_history/server.js +++ b/examples/git_history/server.js @@ -18,5 +18,6 @@ function execute(command, callback) { execute(`git log --date=iso --pretty=format:'"%h","%an","%aD","%s","%ae"'`, log => { const host = new WebSocketHost({assets: [__dirname]}); - host.open("data_source_one", table("Hash,Name,Date,Message,Email\n" + log)); + const tbl = table("Hash,Name,Date,Message,Email\n" + log); + host.host_view("data_source_one", tbl.view()); }); diff --git a/packages/perspective/src/js/api.js b/packages/perspective/src/js/api.js index ae2e719e9b..5c06824a7d 100644 --- a/packages/perspective/src/js/api.js +++ b/packages/perspective/src/js/api.js @@ -84,6 +84,13 @@ function view(worker, table_name, config) { bindall(this); } +function proxy_view(worker, name) { + this._worker = worker; + this._name = name; +} + +proxy_view.prototype = view.prototype; + view.prototype.get_config = async_queue("get_config"); view.prototype.to_json = async_queue("to_json"); @@ -251,10 +258,14 @@ worker.prototype.send = function() { throw new Error("post() not implemented"); }; -worker.prototype.open = function(name) { +worker.prototype.open_table = function(name) { return new proxy_table(this, name); }; +worker.prototype.open_view = function(name) { + return new proxy_view(this, name); +}; + let _initialized = false; worker.prototype._handle = function(e) { diff --git a/packages/perspective/src/js/perspective.node.js b/packages/perspective/src/js/perspective.node.js index 90eed24ef3..7273562bc5 100644 --- a/packages/perspective/src/js/perspective.node.js +++ b/packages/perspective/src/js/perspective.node.js @@ -197,9 +197,13 @@ class WebSocketHost extends module.exports.Host { delete this.REQS[msg.id]; } - open(name, table) { + host_table(name, table) { this._tables[name] = table; - table.view({aggregate: []}); + table.view({columns: []}); + } + + host_view(name, view) { + this._views[name] = view; } close() { From 4a93fcd9eff5ab5b2e7a9608491faebec4242456 Mon Sep 17 00:00:00 2001 From: Andrew Stein Date: Fri, 19 Apr 2019 16:17:48 -0400 Subject: [PATCH 3/4] Updated documentation --- docs/md/usage.md | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/docs/md/usage.md b/docs/md/usage.md index 2ae35a64ac..99b738a1a9 100644 --- a/docs/md/usage.md +++ b/docs/md/usage.md @@ -432,21 +432,30 @@ const fs = require("fs"); // module's directory. const host = new WebSocketHost({ assets: [__dirname], port: 8080 }); -// Read an arrow file from the file system and load it as a named data source. +// Read an arrow file from the file system and load it as a named table. const arr = fs.readFileSync(__dirname + "/superstore.arrow"); -host.open("data_source_one", table(arr)); +const tbl = table(arr); +host.host_table("table_one", tbl); + +// Or host a view +const view = tbl.view({filter: [["State", "==", "Texas"]]}); +host.host_view("view_one", view); ``` In the browser: ```javascript -var elem = document.getElementsByTagName("perspective-viewer")[0]; +const elem = document.getElementsByTagName("perspective-viewer")[0]; // Bind to the server's worker instead of instantiating a Web Worker. -var worker = perspective.worker(window.location.origin.replace("http", "ws")); +const worker = perspective.worker(window.location.origin.replace("http", "ws")); // Bind the viewer to the preloaded data source. -elem.load(worker.open("data_source_one")); +elem.load(worker.open_table("table_one")); + +// Or load data from a view +const arrow = await worker.open_view("view_one").to_arrow(); +elem.load(arrow); ``` `` instances bound in this way are otherwise no different From 965363e073ae90f01eeee246c0eaf6470b8b5c71 Mon Sep 17 00:00:00 2001 From: Andrew Stein Date: Fri, 19 Apr 2019 16:19:36 -0400 Subject: [PATCH 4/4] Update `perspective-cli` package --- packages/perspective-cli/src/html/index.html | 2 +- packages/perspective-cli/src/js/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/perspective-cli/src/html/index.html b/packages/perspective-cli/src/html/index.html index 9b74a9c49c..f6da25f974 100644 --- a/packages/perspective-cli/src/html/index.html +++ b/packages/perspective-cli/src/html/index.html @@ -49,7 +49,7 @@ window.addEventListener('WebComponentsReady', function() { var elem = document.getElementById('view1'); var worker = perspective.worker(window.location.origin.replace('http', 'ws')); - elem.load(worker.open('data_source_one')); + elem.load(worker.open_table('data_source_one')); }); diff --git a/packages/perspective-cli/src/js/index.js b/packages/perspective-cli/src/js/index.js index e99c3b4e38..79b23dd47b 100755 --- a/packages/perspective-cli/src/js/index.js +++ b/packages/perspective-cli/src/js/index.js @@ -74,7 +74,7 @@ async function host(filename, options) { } else { file = await read_stdin(); } - server.open("data_source_one", table(file)); + server.host_table("data_source_one", table(file)); if (options.open) { open_browser(options.port); }