From 8b24b7bf2409549a5714fac32d52efb05d7d6621 Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 1 Feb 2019 16:05:33 -0500 Subject: maintain data store in background --- src/background.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/background.js (limited to 'src/background.js') diff --git a/src/background.js b/src/background.js new file mode 100644 index 0000000..7bc0318 --- /dev/null +++ b/src/background.js @@ -0,0 +1,58 @@ +import * as gapi from './gapi'; + +let patterns = []; +let calendars = {}; +let calData = {}; + +chrome.runtime.onConnect.addListener(function(port) { + console.assert(port.name == 'main'); + port.onMessage.addListener(function(msg) { + console.log(msg); + if (msg.type == 0) { + patterns = msg.data; + } + else if (msg.type == 1) { + port.postMessage({ id: msg.id, type: 1, data: patterns }); + } + else if (msg.type == 2) { + calendars = msg.data; + } + else if (msg.type == 3) { + port.postMessage({ id: msg.id, type: 3, data: calendars }); + } + else if (msg.type == 4) { + 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}`); + return []; + }) + .then(data => { + console.log(data); + let resp = { id: msg.id, type: 4, data: 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"); + } + }); +}); + +chrome.browserAction.onClicked.addListener(function() { + chrome.tabs.create({url: 'index.html'}); +}); + -- cgit v1.2.3