-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathMessageStore.ts
More file actions
77 lines (70 loc) · 2.07 KB
/
MessageStore.ts
File metadata and controls
77 lines (70 loc) · 2.07 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
66
67
68
69
70
71
72
73
74
75
76
77
import FileStore from "./FileStore";
import StoreCache from "./StoreCache";
import StoreLogger from "./StoreLogger";
import IStore from "./IStore";
import IStoreCache from "./IStoreCache";
import IStoreLogger from "./IStoreLogger";
export default class MessageStore {
store : IStore;
cache: IStoreCache;
logger: IStoreLogger;
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(): IStoreLogger {
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(): IStore {
return this.store;
}
/**
* A getter that returns an instance of cache
* Purpose of this is to be able to use a different type of cache
* that would implement the IStore interface
*/
get Cache(): IStoreCache {
return this.cache;
}
/**
*
* @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.save(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
}
}