-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathMessageStore.ts
More file actions
65 lines (60 loc) · 1.75 KB
/
MessageStore.ts
File metadata and controls
65 lines (60 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import FileStore from "./FileStore";
import StoreCache from "./StoreCache";
import StoreLogger from "./StoreLogger";
import IStore from "./IStore";
export default class MessageStore {
store : IStore;
cache: StoreCache;
logger: StoreLogger;
constructor(directory: string) {
this.logger = new StoreLogger();
this.store = new FileStore(directory, this.Logger);
this.cache = new StoreCache(this.Logger);
}
/**
* A getter that returns an instance of logger
* Purpose of this is to be able to extend this class
* and use a different type of logger (that inherits from StoreLogger)
*/
get Logger() {
return this.logger;
}
/**
* A getter that returns an instance of store
* Purpose of this is to be able to use a different type of store
* that would implement the IStore interface
*/
get Store() {
return this.store;
}
/**
*
* @param id the id of the file to save
* @param message the text message to write to the file
*
* Function writes the file to disk using the id as part
* of the filename. The id is a number and the file name is
* formed as a .txt file using the pattern id.txt. Its saved
* in the relative directory as set in the constructor.
*/
public save (id: number, message: string) {
this.Store.save(id, message);
this.cache.addOrUpdate(id, message);
}
/**
*
* @param id the id of the message to read
*
* Function asks the cache to fetch the message
* by id and passes an anonymous function that
* fetches the message from the stoere
* if the message is not in the cache.
*
* @returns message string
*/
public read(id: number): string {
var message = this.cache.getOrAdd(
id, () => this.Store.read(id))
return message
}
}