Skip to content

Commit

Permalink
Switch to onRequest symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
webdev03 committed Oct 15, 2023
1 parent 2aa44c8 commit 4b8bd57
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/classes/cloud/PacketCloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import type CloudConnection from "./CloudConnection";
import events from "events";
import { encode, decode } from "./utils";

type OnRequestFn = (name: string, value: string) => void;

class PacketCloud extends events.EventEmitter {
connection: CloudConnection;
onRequestListeners: OnRequestFn[] = [];
onRequest = Symbol("onRequest");
constructor(connection: CloudConnection) {
super();
this.connection = connection;
Expand All @@ -16,17 +14,12 @@ class PacketCloud extends events.EventEmitter {
const name = decoder.next().value;
const value = decoder.next().value;
if(!name || !value) return;
for(const fn of this.onRequestListeners) try { fn(name, value) } catch(e) { console.error(e) };
this.emit(this.onRequest, name, value);
this.emit(name, value);
}
});
}

onRequest(fn: OnRequestFn) {
this.onRequestListeners.push(fn);
return this;
}

send(name: string, value: string) {
const val = encode(name) + encode(value);
if (val.length > 250) console.warn("Packet length is greater than 250!");
Expand Down

0 comments on commit 4b8bd57

Please sign in to comment.