Skip to content
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

Bind functions #260

Merged
merged 2 commits into from
Oct 4, 2018
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,5 @@ website/i18n/*
cmake-build-debug
.ipynb_checkpoints

package.json.lerna_backup
package.json.lerna_backup
website/static/css/material.dark.css
6 changes: 6 additions & 0 deletions packages/perspective/src/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*
*/

import {bindall} from "./utils.js";

function subscribe(method, cmd) {
return function() {
let resolve = arguments[arguments.length - 1];
Expand Down Expand Up @@ -52,6 +54,7 @@ function view(worker, table_name, config) {
config: config
};
this._worker.post(msg);
bindall(this);
}

view.prototype.to_json = async_queue("to_json");
Expand Down Expand Up @@ -94,6 +97,8 @@ function table(worker, data, options) {
};
this._worker.post(msg);
this._name = name;

bindall(this);
}

function computed_table(worker, computed, name) {
Expand Down Expand Up @@ -178,6 +183,7 @@ export function worker() {
handlers: {},
messages: []
};
bindall(this);
}

worker.prototype.post = function(msg, resolve, reject, keep_alive = false) {
Expand Down
3 changes: 3 additions & 0 deletions packages/perspective/src/js/perspective.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import {AGGREGATE_DEFAULTS, FILTER_DEFAULTS, SORT_ORDERS, TYPE_AGGREGATES, TYPE_FILTERS, COLUMN_SEPARATOR_STRING} from "./defaults.js";
import {DateParser, is_valid_date} from "./date_parser.js";
import {bindall} from "./utils.js";

import {Precision} from "@apache-arrow/es5-esm/type";
import {Table} from "@apache-arrow/es5-esm/table";
Expand Down Expand Up @@ -445,6 +446,7 @@ module.exports = function(Module) {
this.callbacks = callbacks;
this.name = name;
this.table = table;
bindall(this);
}

/**
Expand Down Expand Up @@ -906,6 +908,7 @@ module.exports = function(Module) {
this.views = [];
this.limit = limit;
this.limit_index = limit_index;
bindall(this);
}

table.prototype._update_callback = function() {
Expand Down
2 changes: 1 addition & 1 deletion packages/perspective/src/js/perspective.parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class WebWorker extends worker {
super();
if (window.__PSP_WORKER__) {
this._start_embedded();
} else if (window.location.host === __SCRIPT_PATH__.host().trim(window.location.host.length)) {
} else if (window.location.origin === __SCRIPT_PATH__.host().slice(0, window.location.origin.length)) {
this._start_same_origin();
} else {
this._start_cross_origin();
Expand Down
16 changes: 16 additions & 0 deletions packages/perspective/src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
*
*/

/**
* Bind all methods in a class to the class instance. It is sad that this is
* necessary.
*
* @export
* @param {*} self
*/
export function bindall(self) {
for (const key of Object.getOwnPropertyNames(self.constructor.prototype)) {
const value = self[key];
if (key !== "constructor" && typeof value === "function") {
self[key] = value.bind(self);
}
}
}

/**
* Detect Internet Explorer.
*
Expand Down
8 changes: 8 additions & 0 deletions packages/perspective/test/js/updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ module.exports = perspective => {
expect(result).toEqual(expected);
});

it("`update()` unbound to table", async function() {
var table = perspective.table(meta);
var updater = table.update;
updater(data);
let result = await table.view().to_json();
expect(data).toEqual(result);
});

it("Multiple `update()`s", async function() {
var table = perspective.table(meta);
table.update(data);
Expand Down