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

ES6 Client API #751

Merged
merged 1 commit into from
Oct 4, 2019
Merged
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
187 changes: 97 additions & 90 deletions packages/perspective/src/js/api/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,114 +20,121 @@ import {bindall} from "../utils.js";
*
* @export
*/
export function Client() {
this._worker = {
initialized: {value: false},
transferable: false,
msg_id: 0,
handlers: {},
messages: []
};
bindall(this);
}
export class Client {
constructor() {
this._initialized = false;
this._worker = {
initialized: {value: false},
transferable: false,
msg_id: 0,
handlers: {},
messages: []
};
bindall(this);
}

/**
* Remove a listener for a Perspective-generated event.
*/
Client.prototype.unsubscribe = function(cmd, handler) {
for (let key of Object.keys(this._worker.handlers)) {
if (this._worker.handlers[key].resolve === handler) {
delete this._worker.handlers[key];
/**
* Remove a listener for a Perspective-generated event.
*/
unsubscribe(cmd, handler) {
for (let key of Object.keys(this._worker.handlers)) {
if (this._worker.handlers[key].resolve === handler) {
delete this._worker.handlers[key];
}
}
}
};

/**
* Process an asynchronous message.
*/
Client.prototype.post = function(msg, resolve, reject, keep_alive = false) {
if (resolve || reject) {
this._worker.handlers[++this._worker.msg_id] = {resolve, reject, keep_alive};
}
msg.id = this._worker.msg_id;
if (this._worker.initialized.value) {
this.send(msg);
} else {
this._worker.messages.push(() => this.send(msg));
/**
* Process an asynchronous message.
*/
post(msg, resolve, reject, keep_alive = false) {
if (resolve || reject) {
this._worker.handlers[++this._worker.msg_id] = {resolve, reject, keep_alive};
}
msg.id = this._worker.msg_id;
if (this._worker.initialized.value) {
this.send(msg);
} else {
this._worker.messages.push(() => this.send(msg));
}
}
};

Client.prototype.initialize_profile_thread = function() {
if (this._worker.initialized.value) {
this.send({id: -1, cmd: "init_profile_thread"});
} else {
this._worker.messages.push(() => this.send({id: -1, cmd: "init_profile_thread"}));
initialize_profile_thread() {
if (this._worker.initialized.value) {
this.send({id: -1, cmd: "init_profile_thread"});
} else {
this._worker.messages.push(() => this.send({id: -1, cmd: "init_profile_thread"}));
}
}
};

/**
* Must be implemented in order to transport commands to the server.
*/
Client.prototype.send = function() {
throw new Error("send() not implemented");
};

Client.prototype.open_table = function(name) {
return new proxy_table(this, name);
};
/**
* Must be implemented in order to transport commands to the server.
*/
send() {
throw new Error("send() not implemented");
}

Client.prototype.open_view = function(name) {
return new proxy_view(this, name);
};
/**
* Given the name of a table that is hosted on the server (e.g. using `perspective-python` or `perspective` in NodeJS),
* return a `table` instance that sends all operations and instructions to the `table` on the server.
*
* @param {string} name
*/
open_table(name) {
return new proxy_table(this, name);
}

let _initialized = false;
open_view(name) {
return new proxy_view(this, name);
}

/**
* Handle a command from Perspective. If the Client is not initialized, initialize it and dispatch the `perspective-ready` event.
*
* Otherwise, reject or resolve the incoming command.
*/
Client.prototype._handle = function(e) {
if (!this._worker.initialized.value) {
if (!_initialized && typeof document !== "undefined") {
var event = document.createEvent("Event");
event.initEvent("perspective-ready", false, true);
window.dispatchEvent(event);
_initialized = true;
}
/**
* Handle a command from Perspective. If the Client is not initialized, initialize it and dispatch the `perspective-ready` event.
*
* Otherwise, reject or resolve the incoming command.
*/
_handle(e) {
if (!this._worker.initialized.value) {
if (!this._initialized && typeof document !== "undefined") {
var event = document.createEvent("Event");
event.initEvent("perspective-ready", false, true);
window.dispatchEvent(event);
this._initialized = true;
}

const msgs = this._worker.messages;
this._worker.initialized.value = true;
this._worker.messages = [];
const msgs = this._worker.messages;
this._worker.initialized.value = true;
this._worker.messages = [];

if (msgs) {
for (const m in msgs) {
if (msgs.hasOwnProperty(m)) {
msgs[m]();
if (msgs) {
for (const m in msgs) {
if (msgs.hasOwnProperty(m)) {
msgs[m]();
}
}
}
}
}
if (e.data.id) {
var handler = this._worker.handlers[e.data.id];
if (handler) {
if (e.data.error) {
handler.reject(e.data.error);
} else {
handler.resolve(e.data.data);
}
if (!handler.keep_alive) {
delete this._worker.handlers[e.data.id];
if (e.data.id) {
var handler = this._worker.handlers[e.data.id];
if (handler) {
if (e.data.error) {
handler.reject(e.data.error);
} else {
handler.resolve(e.data.data);
}
if (!handler.keep_alive) {
delete this._worker.handlers[e.data.id];
}
}
}
}
};

Client.prototype.table = function(data, options) {
return new table(this, data, options || {});
};
table(data, options) {
return new table(this, data, options || {});
}

Client.prototype.terminate = function() {
this._worker.terminate();
this._worker = undefined;
};
terminate() {
this._worker.terminate();
this._worker = undefined;
}
}