Caching is still being designed but it _will_ be written in typescript to a more well docc'ed api for everything else
+ Basically im doing this to straight up abuse - Moving cache types + methods to their own module
This commit is contained in:
parent
9ce04e96a7
commit
5b2e92da06
63
freechat-client/src/cache.ts
Normal file
63
freechat-client/src/cache.ts
Normal file
@ -0,0 +1,63 @@
|
||||
import { ServerConfig, Message, Channel } from './types';
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
url(): String{
|
||||
let url = `${this.config.protocol}://${this.config.hostname}`
|
||||
// Only add the port WITH THE COLON if the config requires it
|
||||
if(this.config.port) {
|
||||
url += `:${this.config.port}`
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
push_message(message: Message) {
|
||||
let ref = this.messages.get(message.cid)
|
||||
if(ref) {
|
||||
ref.push(message)
|
||||
} else {
|
||||
this.messages.set(message.cid, [message])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class Cache {
|
||||
// hostname -> Cache
|
||||
servers: Map<String, ServerCache>
|
||||
|
||||
constructor(servers: Array<ServerCache>) {
|
||||
for(const server of servers) {
|
||||
const hostname = server.config.hostname
|
||||
this.servers.set(hostname, server)
|
||||
}
|
||||
}
|
||||
|
||||
add_message(hostname: String, message: Message) {
|
||||
let ref = this.servers.get(hostname)
|
||||
if(!ref) {
|
||||
throw `No server config found for ${hostname}`
|
||||
}
|
||||
|
||||
ref.push_message(message)
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ export class Message {
|
||||
|
||||
this.type = type
|
||||
|
||||
// throw away the content if its not of a valid type
|
||||
if(this.TYPES.indexOf(content) == -1) {
|
||||
this.content = null
|
||||
} else {
|
||||
@ -53,40 +54,4 @@ export class ServerConfig {
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
//
|
||||
// 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