1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2026-03-02 04:09:18 +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

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