1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00

refactor: moved connect/disconnect events from provider to client

This commit is contained in:
Falk Werner 2019-02-22 15:48:49 +01:00
parent dd5a8ebd73
commit 024ae2cfba
5 changed files with 27 additions and 42 deletions

View File

@ -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";
}

View File

@ -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);

View File

@ -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;

View File

@ -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,9 +27,14 @@ export class Client {
disconnect() {
if (this._ws) {
this._ws.close();
this._ws = null;
}
}
isConnected() {
return ((this._ws) && (this._ws.readyState === WebSocket.OPEN));
}
_onmessage(message) {
try {
const request = JSON.parse(message.data);

View File

@ -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);
}