diff options
Diffstat (limited to 'src/background.js')
-rw-r--r-- | src/background.js | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/src/background.js b/src/background.js index 4fefceb..177ec68 100644 --- a/src/background.js +++ b/src/background.js @@ -1,4 +1,5 @@ import * as gapi from './gapi'; +import { msgType, Msg } from './msg'; let patterns = []; let calendars = {}; @@ -6,21 +7,26 @@ let calData = {}; chrome.runtime.onConnect.addListener(function(port) { console.assert(port.name == 'main'); - port.onMessage.addListener(function(msg) { + port.onMessage.addListener(function(_msg) { + let msg = Msg.inflate(_msg); console.log(msg); - if (msg.type == 0) { + if (msg.type == msgType.updatePatterns) { patterns = msg.data; } - else if (msg.type == 1) { - port.postMessage({ id: msg.id, type: 1, data: patterns }); + else if (msg.type == msgType.getPatterns) { + port.postMessage(msg.genResp(patterns)); } - else if (msg.type == 2) { + else if (msg.type == msgType.updateCalendars) { calendars = msg.data; + for (let id in calendars) { + if (!calData.hasOwnProperty(id)) + calData[id] = new gapi.GCalendar(id, calendars[id].summary); + } } - else if (msg.type == 3) { - port.postMessage({ id: msg.id, type: 3, data: calendars }); + else if (msg.type == msgType.getCalendars) { + port.postMessage(msg.genResp(calendars)); } - else if (msg.type == 4) { + else if (msg.type == msgType.getCalEvents) { calData[msg.data.id].getEvents(new Date(msg.data.start), new Date(msg.data.end)) .catch(e => { console.log(`cannot load calendar ${msg.data.id}`, e); @@ -28,24 +34,17 @@ chrome.runtime.onConnect.addListener(function(port) { }) .then(data => { console.log(data); - let resp = { id: msg.id, type: 4, data: data.map(e => { + let resp = msg.genResp(data.map(e => { return { id: e.id, start: e.start.getTime(), end: e.end.getTime() } - })}; + })); console.log(resp); port.postMessage(resp); }); } - else if (msg.type == 5) { - calendars = msg.data; - for (let id in calendars) { - if (!calData.hasOwnProperty(id)) - calData[id] = new gapi.GCalendar(id, calendars[id].summary); - } - } else { console.error("unknown msg type"); } |