diff --git a/freechat-client/src/cache.ts b/freechat-client/src/cache.ts new file mode 100644 index 0000000..ddf71a6 --- /dev/null +++ b/freechat-client/src/cache.ts @@ -0,0 +1,63 @@ +import { ServerConfig, Message, Channel } from './types'; + +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) + } + } + } + + 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 + + constructor(servers: Array) { + 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) + } +} + diff --git a/freechat-client/src/types.ts b/freechat-client/src/types.ts index 0dd0415..4668322 100644 --- a/freechat-client/src/types.ts +++ b/freechat-client/src/types.ts @@ -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 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