diff --git a/example/daemon/www/js/connection_view.js b/example/daemon/www/js/connection_view.js index a75955e..37c0247 100644 --- a/example/daemon/www/js/connection_view.js +++ b/example/daemon/www/js/connection_view.js @@ -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"; } diff --git a/example/daemon/www/js/filesystem_provider.js b/example/daemon/www/js/filesystem_provider.js index be9a32b..c2f9fbe 100644 --- a/example/daemon/www/js/filesystem_provider.js +++ b/example/daemon/www/js/filesystem_provider.js @@ -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); diff --git a/example/daemon/www/js/startup.js b/example/daemon/www/js/startup.js index 3118a74..fa164f0 100644 --- a/example/daemon/www/js/startup.js +++ b/example/daemon/www/js/startup.js @@ -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; diff --git a/example/daemon/www/js/wsfs/client.js b/example/daemon/www/js/wsfs/client.js index 7b369e5..7c1b2bb 100644 --- a/example/daemon/www/js/wsfs/client.js +++ b/example/daemon/www/js/wsfs/client.js @@ -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); diff --git a/example/daemon/www/js/wsfs/provider.js b/example/daemon/www/js/wsfs/provider.js index c9008f9..b9c5dc2 100644 --- a/example/daemon/www/js/wsfs/provider.js +++ b/example/daemon/www/js/wsfs/provider.js @@ -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); }