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
This commit is contained in:
parent
049e8aea82
commit
d620d3cc61
5
freechat-client/package-lock.json
generated
5
freechat-client/package-lock.json
generated
@ -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",
|
||||
|
@ -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": {
|
||||
|
92
freechat-client/src/types.ts
Normal file
92
freechat-client/src/types.ts
Normal file
@ -0,0 +1,92 @@
|
||||
// Basically here we define various types that are used pretty much everywhere
|
||||
|
||||
export class Message {
|
||||
private TYPES: Array<String> = ['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>
|
||||
// Channel id's are used to key into the list of messages for that given channel
|
||||
messages: Map<Number, Array<Message>>
|
||||
config: ServerConfig
|
||||
|
||||
constructor(config: ServerConfig, channels: Array<Channel>, messages: Array<Message>) {
|
||||
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<ServerCache>
|
||||
|
||||
constructor(servers: Array<ServerCache>) {
|
||||
this.servers = servers // good chance this will be null on init but thats ok
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user