From d620d3cc61f72e2a3927a3eba09f219e67332c7b Mon Sep 17 00:00:00 2001 From: shockrah Date: Thu, 11 Mar 2021 17:22:05 -0800 Subject: [PATCH] New types module written with typescript to slowly start migrating code over to typescript The main reason for this change is for writing more descriptive code Such a change isn't impossible with JS but requires annoying doccing that most dev environments don't really pick up on ever Also the cache system is goingto be much more complex than anything else in this project so static compilation should help remove annoyances --- freechat-client/package-lock.json | 5 ++ freechat-client/package.json | 3 +- freechat-client/src/types.ts | 92 +++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 freechat-client/src/types.ts diff --git a/freechat-client/package-lock.json b/freechat-client/package-lock.json index f77b6e8..09695fa 100644 --- a/freechat-client/package-lock.json +++ b/freechat-client/package-lock.json @@ -1137,6 +1137,11 @@ "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=", "dev": true }, + "typescript": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.2.3.tgz", + "integrity": "sha512-qOcYwxaByStAWrBf4x0fibwZvMRG+r4cQoTjbPtUlrWjBHbmCAww1i448U0GJ+3cNNEtebDteo/cHOR3xJ4wEw==" + }, "universalify": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", diff --git a/freechat-client/package.json b/freechat-client/package.json index c82813e..13a4ee7 100644 --- a/freechat-client/package.json +++ b/freechat-client/package.json @@ -5,7 +5,7 @@ "scripts": { "build": "sh scripts/build-sass.sh build", "start": "electron . --", - "test": "node testing/main.js" + "test": "node testing/main.js" }, "dependencies": { "argparse": "^2.0.1", @@ -16,6 +16,7 @@ "popper.js": "^1.16.1", "socket.io": "^3.1.1", "socket.io-client": "^3.1.1", + "typescript": "^4.2.3", "ws": "^7.4.3" }, "devDependencies": { diff --git a/freechat-client/src/types.ts b/freechat-client/src/types.ts new file mode 100644 index 0000000..0dd0415 --- /dev/null +++ b/freechat-client/src/types.ts @@ -0,0 +1,92 @@ +// Basically here we define various types that are used pretty much everywhere + +export class Message { + private TYPES: Array = ['text', 'jpeg', 'png', 'webm', 'mp4'] + id: Number + time: Number + type: String + content: String|null + uid: Number + uname: String + cid: Number + + constructor(id: Number, time: Number, content: String, type: String, uid: Number, uname: String, cid: Number) { + this.id = id + this.time = time + this.uid = uid + this.uname = uname + this.cid = cid + + this.type = type + + if(this.TYPES.indexOf(content) == -1) { + this.content = null + } else { + this.content = content + } + } +} + +export class Channel { + name: String + type: Number + id: Number + description: String|null + + constructor(name: String, type: Number, id: Number, desc: String) { + this.name = name + this.type = type + this.id = id + this.description = desc + } +} + +export class ServerConfig { + hostname: String + port: Number + protocol: String + + constructor(protocol: String, hostname: String, port: Number) { + this.protocol = protocol + this.hostname = hostname + this.port = port + } +} + +//////////////////////////////////////////////////////////////////////////////// +// +// +// Server Cache Types +// +// These types are defined for the net-cache system so that client apps can more +// intelligently make network requests. +//////////////////////////////////////////////////////////////////////////////// + +export class ServerCache { + channels: Array + // Channel id's are used to key into the list of messages for that given channel + messages: Map> + config: ServerConfig + + constructor(config: ServerConfig, channels: Array, messages: Array) { + this.config = config + this.channels = channels + for(const message of messages) { + const ref = this.messages.get(message.cid) + + if(!ref) { + this.messages.set(message.cid, [message]) + } else { + ref.push(message) + } + } + } +} + +export class Cache { + servers: Array + + constructor(servers: Array) { + this.servers = servers // good chance this will be null on init but thats ok + } +} \ No newline at end of file