refactor: moved connect/disconnect events from provider to client

pull/10/head
Falk Werner 6 years ago
parent dd5a8ebd73
commit 024ae2cfba

@ -1,10 +1,12 @@
export class ConnectionView {
constructor(client) {
this.connection = client;
this._client = client;
this._client.onopen = () => { this._onConnectionOpened(); };
this._client.onclose = () => { this._onConnectionClosed(); };
this.element = document.createElement("div");
let urlLabel = document.createElement("span");
const urlLabel = document.createElement("span");
urlLabel.textContent = "URL:";
this.element.appendChild(urlLabel);
@ -16,24 +18,25 @@ export class ConnectionView {
this.connectButton = document.createElement("input");
this.connectButton.type = "button";
this.connectButton.value = "connect";
this.connectButton.addEventListener("click", () => { this.onConnectButtonClicked(); });
this.connectButton.addEventListener("click", () => { this._onConnectButtonClicked(); });
this.element.appendChild(this.connectButton);
}
onConnectButtonClicked() {
if (!this.connection.isConnected) {
_onConnectButtonClicked() {
if (!this._client.isConnected()) {
let url = this.urlTextbox.value;
this.connection.connectTo(url);
this._client.connectTo(url);
}
else {
this.connection.disconnect();
this._client.disconnect();
}
}
onConnectionOpened() {
_onConnectionOpened() {
this.connectButton.value = "disconnect";
}
onConnectionClosed() {
_onConnectionClosed() {
this.connectButton.value = "connect";
}

@ -14,18 +14,6 @@ export class FileSystemProvider extends Provider {
this._walk(this.root, (entry) => { this._inodes[entry.inode] = entry; });
}
setView(view) {
this._view = view;
}
connected() {
if (this._view) { this._view.onConnectionOpened(); }
}
disconnected() {
if (this._view) { this._view.onConnectionClosed(); }
}
_walk(node, callback) {
callback(node);

@ -8,7 +8,7 @@ function mode(value) {
}
function startup() {
let provider = new FileSystemProvider({
const provider = new FileSystemProvider({
inode: 1,
mode: mode("0755"),
type: "dir",
@ -17,10 +17,9 @@ function startup() {
"say_hello.sh": { inode: 3, mode: mode("0555"), type: "file", contents: "#!/bin/sh\necho hello\n"}
}
});
let client = new Client(provider);
let connectionView = new ConnectionView(client);
const client = new Client(provider);
const connectionView = new ConnectionView(client);
document.getElementById('connection').appendChild(connectionView.element);
provider.setView(connectionView);
}
window.onload = startup;

@ -6,21 +6,19 @@ export class Client {
constructor(provider) {
this._provider = provider;
this._ws = null;
this.isConnected = false;
this.onopen = () => { };
this.onclose = () => { };
this.onerror = () => { };
}
connectTo(url) {
this.disconnect();
this._ws = new WebSocket(url, Client._PROTOCOL);
this._ws.onopen = () => {
this.isConnected = true;
this._provider.connected();
};
this._ws.onclose = () => {
this.isConnected = false;
this._provider.disconnected();
};
this._ws.onopen = this.onopen;
this._ws.onclose = this.onclose;
this._ws.onerror = this.onerror;
this._ws.onmessage = (message) => {
this._onmessage(message);
};
@ -29,7 +27,12 @@ export class Client {
disconnect() {
if (this._ws) {
this._ws.close();
this._ws = null;
}
}
isConnected() {
return ((this._ws) && (this._ws.readyState === WebSocket.OPEN));
}
_onmessage(message) {

@ -4,14 +4,6 @@ import { BadState } from "./bad_state.js";
export class Provider {
connected() {
// empty
}
disconnected() {
// empty
}
async lookup(_parent, _name) {
throw new BadState(BadState.NOT_IMPLEMENTED);
}

Loading…
Cancel
Save