Commit 1871d8b6 by Alexander Bazo

Refactoring

parent f1ae651e
class Event {
constructor(type, data) {
this.type = type;
this.data = data;
}
}
class DataEvent extends Event {
constructor(data) {
super("dataavailable", data);
}
}
class ConnectionOpenedEvent extends Event {
constructor() {
super("connectionopened");
}
}
class ConnectionClosedEvent extends Event {
constructor() {
super("connectionclosed");
}
}
export {DataEvent, ConnectionOpenedEvent, ConnectionClosedEvent};
\ No newline at end of file
import Observable from "../utils/Observable.js";
import { ConnectionOpenedEvent, DataEvent } from "./Events.js";
class WebSocketClient extends Observable {
constructor() {
super();
}
connect(url) {
this.ws = new WebSocket(url);
this.ws.onopen = onOpen.bind(this);
this.ws.onmessage = onMessage.bind(this);
}
}
function onOpen() {
let connectionEvent = new ConnectionOpenedEvent();
this.notifyAll(connectionEvent);
}
function onMessage(event) {
let dataEvent = new DataEvent(event.data);
this.notifyAll(dataEvent);
}
export default WebSocketClient;
\ No newline at end of file
/* eslint-env browser */ import Observable from "./utils/Observable.js";
import WebSocketClient from "./com/WebSocketClient.js";
import { Event, Observable } from "./Observable.js";
import WebSocketClient from "./WebSocketClient.js";
class GazeClient extends Observable { class GazeClient extends Observable {
...@@ -12,21 +10,22 @@ class GazeClient extends Observable { ...@@ -12,21 +10,22 @@ class GazeClient extends Observable {
connect(url) { connect(url) {
this.url = url; this.url = url;
this.client = new WebSocketClient(); this.client = new WebSocketClient();
this.client.addEventListener("connected", this.onConnected.bind(this)); this.client.addEventListener("connectionopened", this.onConnected.bind(this));
this.client.addEventListener("gazeDataAvailable", this.onGazeDataAvailable this.client.addEventListener("connectionclosed", this.onDisconnected.bind(this));
this.client.addEventListener("dataAvailable", this.onDataAvailable
.bind(this)); .bind(this));
this.client.connect(url); this.client.connect(url);
} }
onOpen() { onConnected(event) {
this.client.send("hello server"); this.notifyAll(event);
} }
onConnected(event) { onDisconnected(event) {
// TODO: Implement event broadcasting this.notifyAll(event);
} }
onGazeDataAvailable(event) { onDataAvailable(event) {
this.notifyAll(event); this.notifyAll(event);
} }
......
class Observable {
constructor() {
this.listeners = {};
}
addEventListener(type, callback) {
if (this.listeners[type] === undefined) {
this.listeners[type] = [];
}
this.listeners[type].push(callback);
}
notifyAll(event) {
let listeners = this.listeners[event.type];
if (listeners) {
for (let i = 0; i < listeners.length; i++) {
listeners[i](event);
}
}
}
}
export default Observable;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment