This repository has been archived on 2025-03-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
wetsock/lib/WetSock/Message.js
2024-04-08 01:08:06 +01:00

44 lines
1.1 KiB
JavaScript

/**
* Represents a socket message.
*/
export default class Message {
#origin;
#data;
#message;
/**
* Creates an instance of Message.
* @param {string} rawText - The raw text of the socket message.
*/
constructor(rawText) {
this.#origin = rawText;
try {
this.#data = JSON.parse(rawText.trim());
this.#message = this.#data.message;
} catch(err) {
this.#data = undefined;
this.#message = undefined;
}
}
/**
* Returns an iterable of the property names in the message data.
* @returns {Generator<string>} - An iterable of property names.
*/
*getProps() {
if (!this.#data || typeof(this.#data) != 'object') return;
for (const prop in this.#data) {
yield prop;
}
}
/**
* Retrieves the value of a specific property in the message data.
* @param {string} key - The key of the property.
* @returns {*} - The value associated with the specified key.
*/
get(key) {
return this.#data[key];
}
}