aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2019-02-03 01:14:45 -0500
committerDeterminant <[email protected]>2019-02-03 01:14:45 -0500
commit5552d53304a26f53172e1cc0d0f86e01131eaadd (patch)
tree7276c23d174ac84c73de595b16ce841ae7b36f22 /src
parent580990a5eb4a79892c48e3a3fce3386fe80e6cc2 (diff)
use Msg objects
Diffstat (limited to 'src')
-rw-r--r--src/App.js48
-rw-r--r--src/background.js33
-rw-r--r--src/gapi.js28
-rw-r--r--src/msg.js50
4 files changed, 104 insertions, 55 deletions
diff --git a/src/App.js b/src/App.js
index 43beb53..f067250 100644
--- a/src/App.js
+++ b/src/App.js
@@ -20,6 +20,7 @@ import AddCircleIcon from '@material-ui/icons/AddCircle';
import IconButton from '@material-ui/core/IconButton';
import Logo from './Logo';
import * as gapi from './gapi';
+import { msgType, Msg } from './msg';
import { Pattern, PatternEntry } from './pattern';
import PieChart from './Chart';
import PatternTable from './PatternTable';
@@ -81,21 +82,21 @@ class Dashboard extends React.Component {
constructor(props) {
super(props);
let port = chrome.runtime.connect({name: 'main'});
- const getCallBack = t => this.requestCallback[t];
+ const getCallBack = rcb => this.requestCallback;
port.onMessage.addListener(function(msg) {
console.log(msg);
- let t = getCallBack(msg.type);
- let e = t.inFlight[msg.id];
- console.assert(e !== undefined);
- t.ids.push(msg.id);
- e(msg);
+ let rcb = getCallBack(msg.type);
+ let cb = rcb.inFlight[msg.id];
+ console.assert(cb !== undefined);
+ rcb.ids.push(msg.id);
+ cb(msg);
});
this.port = port;
- this.requestCallback = {};
- this.sendMsg({ type: 1 }).then(msg => {
+ this.requestCallback = {inFlight: {}, ids: [], maxId: 0};
+ this.sendMsg({ type: msgType.getPatterns }).then(msg => {
this.setState({ patterns: msg.data.map(p => PatternEntry.revive(p)) });
});
- this.sendMsg({ type: 3 }).then(msg => {
+ this.sendMsg({ type: msgType.getCalendars }).then(msg => {
this.setState({ calendars: msg.data });
});
}
@@ -104,7 +105,7 @@ class Dashboard extends React.Component {
let patterns = this.state.patterns;
patterns[idx][field] = value;
this.setState({ patterns });
- this.sendMsg({ type: 0, data: patterns });
+ this.sendMsg({ type: msgType.updatePatterns, data: patterns });
};
removePattern = idx => {
@@ -113,7 +114,7 @@ class Dashboard extends React.Component {
for (let i = 0; i < patterns.length; i++)
patterns[i].idx = i;
this.setState({ patterns });
- this.sendMsg({ type: 0, data: patterns });
+ this.sendMsg({ type: msgType.updatePatterns, data: patterns });
};
newPattern = () => {
@@ -121,39 +122,36 @@ class Dashboard extends React.Component {
for (let i = 1; i < patterns.length; i++)
patterns[i].idx = i;
this.setState({ patterns });
- this.sendMsg({ type: 0, data: patterns });
+ this.sendMsg({ type: msgType.updatePatterns, data: patterns });
};
loadPatterns = patterns => {
this.setState({ patterns });
- this.sendMsg({ type: 0, data: patterns });
+ this.sendMsg({ type: msgType.updatePatterns, data: patterns });
};
loadCalendars = calendars => {
this.setState({ calendars });
- this.sendMsg({ type: 5, data: calendars });
+ this.sendMsg({ type: msgType.updateCalendars, data: calendars });
};
- sendMsg = msg => {
- if (!this.requestCallback.hasOwnProperty(msg.type))
- this.requestCallback[msg.type] = {inFlight: {}, ids: [], maxId: 0};
- let t = this.requestCallback[msg.type];
+ sendMsg = ({ type, data }) => {
+ let rcb = this.requestCallback;
let cb;
let pm = new Promise(resolve => { cb = resolve; });
let id;
- if (t.ids.length > 0) {
- id = t.ids.pop();
+ if (rcb.ids.length > 0) {
+ id = rcb.ids.pop();
} else {
- id = t.maxId++;
+ id = rcb.maxId++;
}
- t.inFlight[id] = cb;
- msg.id = id;
- this.port.postMessage(msg);
+ rcb.inFlight[id] = cb;
+ this.port.postMessage((new Msg(id, type, data)).deflate());
return pm;
}
getCalEvents = (id, start, end) => {
- return this.sendMsg({ type: 4, data: { id,
+ return this.sendMsg({ type: msgType.getCalEvents, data: { id,
start: start.getTime(),
end: end.getTime() } })
.then(({ data }) => data.map(e => {
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");
}
diff --git a/src/gapi.js b/src/gapi.js
index b5816ca..25d1cbf 100644
--- a/src/gapi.js
+++ b/src/gapi.js
@@ -202,16 +202,18 @@ export class GCalendar {
sync() {
return this.token.then(token => getEvents(this.calId, token, this.syncToken).then(r => {
- this.syncToken = r.nextSyncToken;
- let pm_results = r.results.map(e => e.start ? Promise.resolve(e) : getEvent(this.calId, e.id, token));
- return Promise.all(pm_results).then(results => results.forEach(e => {
- e.start = new Date(e.start.dateTime);
- e.end = new Date(e.end.dateTime);
- if (e.status === 'confirmed')
- this.addEvent(e);
- else if (e.status === 'cancelled')
- this.removeEvent(e);
- }));
+ let pms = r.results.map(e => e.start ? Promise.resolve(e) : getEvent(this.calId, e.id, token));
+ return Promise.all(pms).then(results => {
+ results.forEach(e => {
+ e.start = new Date(e.start.dateTime);
+ e.end = new Date(e.end.dateTime);
+ if (e.status === 'confirmed')
+ this.addEvent(e);
+ else if (e.status === 'cancelled')
+ this.removeEvent(e);
+ });
+ this.syncToken = r.nextSyncToken;
+ });
})).catch(e => {
if (e === GApiError.invalidSyncToken) {
this.syncToken = '';
@@ -260,9 +262,7 @@ export class GCalendar {
return this.token.then(token => getEvents(this.calId, token, null,
this.slotStartDate(query.start).toISOString(),
this.slotEndDate(query.end).toISOString()).then(r => {
- if (this.syncToken === '')
- this.syncToken = r.nextSyncToken;
- return r.results.forEach(e => {
+ r.results.forEach(e => {
if (e.status === 'confirmed')
{
console.assert(e.start);
@@ -271,6 +271,8 @@ export class GCalendar {
this.addEvent(e, true);
}
});
+ if (this.syncToken === '')
+ this.syncToken = r.nextSyncToken;
})).then(() => this.sync())
.then(() => this.getCachedEvents({ start, end }));
}
diff --git a/src/msg.js b/src/msg.js
new file mode 100644
index 0000000..c0aaf11
--- /dev/null
+++ b/src/msg.js
@@ -0,0 +1,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);
+}