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

@ -14,18 +14,6 @@ export class FileSystemProvider extends Provider {
this._walk(this.root, (entry) => { this._inodes[entry.inode] = entry; }); 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) { _walk(node, callback) {
callback(node); callback(node);

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

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

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

Loading…
Cancel
Save