Skip to content

Commit b01bb57

Browse files
authored
Merge pull request #19 from hello-smile6/patch-1
Make CloudConnection an EventEmitter
2 parents 319f9fe + b368930 commit b01bb57

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/classes/CloudConnection.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22

33
import { WebSocket } from "ws";
44
import { Session } from "../Consts";
5+
import events from "events";
56

6-
class CloudConnection {
7+
class CloudConnection extends events.EventEmitter {
78
creator: string;
89
id: number;
910
session: Session;
1011
server: string;
1112
connection: WebSocket;
13+
open: boolean = false;
14+
private queue: Array<{
15+
user: string;
16+
method: string;
17+
name: string;
18+
value: string | number;
19+
project_id: number;
20+
}> = [];
1221
variables: object = {};
1322
disconnected: boolean = false;
1423
constructor({
@@ -20,6 +29,7 @@ class CloudConnection {
2029
session: Session;
2130
server?: string;
2231
}) {
32+
super();
2333
this.id = id;
2434
this.session = session;
2535
this.server = server;
@@ -28,6 +38,7 @@ class CloudConnection {
2838
}
2939

3040
private connect() {
41+
this.open = false;
3142
this.connection = new WebSocket(this.server, {
3243
headers: {
3344
Cookie: this.session.cookieSet,
@@ -39,29 +50,41 @@ class CloudConnection {
3950
for (const message of e.toString().split("\n")) {
4051
const obj = JSON.parse(message || '{"method": "err"}');
4152
if (obj.method == "set") {
53+
this.emit("set", {name: obj.name, value: obj.value});
4254
this.variables[obj.name] = obj.value;
4355
}
4456
}
4557
});
4658
this.connection.on("open", () => {
59+
this.open = true;
4760
this.send({
4861
method: "handshake",
4962
user: this.session.sessionJSON.user.username,
5063
project_id: this.id.toString()
5164
});
65+
this.emit("connect", null);
66+
// handle queue
67+
for (let item of this.queue) {
68+
this.send(item);
69+
}
5270
});
5371
this.connection.on("error", (err) => {
72+
this.emit("error", err);
5473
throw err;
5574
});
5675
this.connection.on("close", () => {
57-
if (!this.disconnected) this.connect();
76+
if (!this.disconnected) {
77+
this.emit("reconnect", null);
78+
this.connect();
79+
}
5880
});
5981
}
6082

6183
/**
6284
* Sends a packet through cloud
6385
*/
6486
private send(data) {
87+
this.emit("internal-send", data);
6588
this.connection.send(`${JSON.stringify(data)}\n`);
6689
}
6790

@@ -75,6 +98,16 @@ class CloudConnection {
7598
? variable.substring(2)
7699
: variable;
77100
this.variables[`☁ ${varname}`] = value;
101+
if (!this.open) {
102+
this.queue.push({
103+
user: this.session.sessionJSON.user.username,
104+
method: "set",
105+
name: `☁ ${varname}`,
106+
value: value.toString(),
107+
project_id: this.id
108+
});
109+
return;
110+
}
78111
this.send({
79112
user: this.session.sessionJSON.user.username,
80113
method: "set",
@@ -100,6 +133,7 @@ class CloudConnection {
100133
* Closes the cloud connection
101134
*/
102135
close() {
136+
this.emit("close", null);
103137
this.disconnected = true;
104138
this.connection.close();
105139
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"include": ["src/**/*"],
33
"exclude": ["node_modules", "**/*.spec.ts", "dist"],
44
"compilerOptions": {
5-
"moduleResolution": "node"
5+
"moduleResolution": "node",
6+
"allowSyntheticDefaultImports": true
67
}
78
}

0 commit comments

Comments
 (0)