aboutsummaryrefslogtreecommitdiff
path: root/src/msg.js
blob: c0aaf1104b59ec51e0c2b9cbd4c5872ef2960072 (plain) (blame)
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
const _updatePatterns = "updatePatterns";
const _getPatterns = "getPatterns";
const _updateCalendars = "updateCalendars";
const _getCalendars = "getCalendars";
const _getCalEvents = "getCalEvents";

export const msgType = Object.freeze({
    updatePatterns: Symbol(_updatePatterns),
    getPatterns: Symbol(_getPatterns),
    updateCalendars: Symbol(_updateCalendars),
    getCalendars: Symbol(_getCalendars),
    getCalEvents: Symbol(_getCalEvents),
});

function stringifyMsgType(mt) {
    switch (mt) {
        case msgType.updatePatterns: return _updatePatterns;
        case msgType.getPatterns: return _getPatterns;
        case msgType.updateCalendars: return _updateCalendars;
        case msgType.getCalendars: return _getCalendars;
        case msgType.getCalEvents: return _getCalEvents;
    }
}

function parseMsgType(s) {
    switch(s) {
        case _updatePatterns: return msgType.updatePatterns;
        case _getPatterns: return msgType.getPatterns;
        case _updateCalendars: return msgType.updateCalendars;
        case _getCalendars: return msgType.getCalendars;
        case _getCalEvents: return msgType.getCalEvents;
    }
}

export class Msg {
    constructor(id, type, data) {
        this.id = id;
        this.type = type;
        this.data = data;
    }
    genResp(data) { return new Msg(this.id, this.type, data); }
    deflate() {
        return {
            id: this.id,
            type: stringifyMsgType(this.type),
            data: this.data
        }
    }
    static inflate = obj => new Msg(obj.id, parseMsgType(obj.type), obj.data);
}