aboutsummaryrefslogtreecommitdiff
path: root/src/background.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/background.js')
-rw-r--r--src/background.js39
1 files changed, 32 insertions, 7 deletions
diff --git a/src/background.js b/src/background.js
index e9b0f60..4bee605 100644
--- a/src/background.js
+++ b/src/background.js
@@ -1,40 +1,52 @@
import * as gapi from './gapi';
import { msgType, Msg } from './msg';
+import { Duration } from './duration';
let mainPatterns = [];
let analyzePatterns = [];
let calendars = {};
let calData = {};
+let config = {
+ trackedPeriods: [
+ {name: 'Today', start: Duration.days(1), end: Duration.days(0)},
+ {name: 'Yesterday', start: Duration.days(2), end: Duration.days(1)},
+ {name: 'This Week', start: Duration.weeks(1), end: Duration.weeks(0)},
+ {name: 'This Month', start: Duration.months(1), end: Duration.months(0)}]
+};
chrome.runtime.onConnect.addListener(function(port) {
console.assert(port.name == 'main');
port.onMessage.addListener(function(_msg) {
let msg = Msg.inflate(_msg);
console.log(msg);
- if (msg.type == msgType.updatePatterns) {
+ switch (msg.type) {
+ case msgType.updatePatterns: {
if (msg.data.id == 'analyze')
analyzePatterns = msg.data.patterns;
else
mainPatterns = msg.data.patterns;
port.postMessage(msg.genResp(null));
+ break;
}
- else if (msg.type == msgType.getPatterns) {
+ case msgType.getPatterns: {
let patterns;
if (msg.data.id == 'analyze')
patterns = analyzePatterns;
else
patterns = mainPatterns;
port.postMessage(msg.genResp(patterns));
+ break;
}
- else if (msg.type == msgType.updateCalendars) {
+ case msgType.updateCalendars: {
calendars = msg.data;
for (let id in calendars) {
if (!calData.hasOwnProperty(id))
calData[id] = new gapi.GCalendar(id, calendars[id].summary);
}
port.postMessage(msg.genResp(null));
+ break;
}
- else if (msg.type == msgType.getCalendars) {
+ case msgType.getCalendars: {
let cals = calendars;
if (msg.data.enabledOnly)
{
@@ -43,8 +55,9 @@ chrome.runtime.onConnect.addListener(function(port) {
.reduce((res, id) => (res[id] = calendars[id], res), {});
}
port.postMessage(msg.genResp(cals));
+ break;
}
- else if (msg.type == msgType.getCalEvents) {
+ case 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);
@@ -62,9 +75,21 @@ chrome.runtime.onConnect.addListener(function(port) {
console.log(resp);
port.postMessage(resp);
});
+ break;
}
- else {
- console.error("unknown msg type");
+ case msgType.updateConfig: {
+ for (let prop in msg.data)
+ config[prop] = msg.data[prop];
+ port.postMessage(msg.genResp(null));
+ break;
+ }
+ case msgType.getConfig: {
+ let res = {};
+ msg.data.forEach(prop => res[prop] = config[prop]);
+ port.postMessage(msg.genResp(res));
+ break;
+ }
+ default: console.error("unknown msg type");
}
});
});