forked from thunderbird/webext-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
153 lines (125 loc) · 5.1 KB
/
background.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Tempted to just use `var { calendar } = messenger;`?
// You will find yourself naming function arguments `calendar` way too often.
var { calendar: lightning } = messenger;
lightning.calendars.onCreated.addListener((calendar) => {
console.log("Created calendar", calendar);
});
lightning.calendars.onUpdated.addListener((calendar, changeInfo) => {
console.log("Updated calendar", calendar, changeInfo);
});
lightning.calendars.onRemoved.addListener((id) => {
console.log("Removed calendar", id);
});
lightning.items.onCreated.addListener((item) => {
console.log("Created item", item);
}, { returnFormat: "ical" });
lightning.items.onUpdated.addListener((item, changeInfo) => {
console.log("Updated item", item, changeInfo);
}, { returnFormat: "ical" });
lightning.items.onRemoved.addListener((calendarId, id) => {
console.log("Deleted item", id);
});
lightning.items.onAlarm.addListener((item, alarm) => {
console.log("Alarm item", item, alarm);
}, { returnFormat: "ical" });
function icalDate(date) {
return date.toISOString().replace(/\.\d+Z$/, "").replace(/[:-]/g, "");
}
lightning.provider.onItemCreated.addListener(async (calendar, item) => {
console.log("Provider add to calendar", item);
item.metadata = { created: true };
return item;
}, { returnFormat: "ical" });
lightning.provider.onItemUpdated.addListener(async (calendar, item, oldItem) => {
console.log("Provider modify in calendar", item, oldItem);
item.metadata = { updated: true };
return item;
}, { returnFormat: "ical" });
lightning.provider.onItemRemoved.addListener(async (calendar, item) => {
console.log("Provider remove from calendar", item);
});
let ticks = {};
lightning.provider.onInit.addListener(async (calendar) => {
console.log("Initializing", calendar);
});
lightning.provider.onSync.addListener(async (calendar) => {
console.log("Synchronizing", calendar, "tick", ticks[calendar.id]);
if (!ticks[calendar.id]) {
ticks[calendar.id] = 0;
await lightning.items.create(calendar.cacheId, {
id: "findme",
type: "event",
title: "New Event",
startDate: icalDate(new Date()),
endDate: icalDate(new Date()),
metadata: { etag: 123 }
});
} else if (ticks[calendar.id] == 1) {
await lightning.items.update(calendar.cacheId, "findme", {
title: "Updated",
startDate: icalDate(new Date()),
endDate: icalDate(new Date()),
metadata: { etag: 234 }
});
} else if (ticks[calendar.id] == 2) {
await lightning.calendars.clear(calendar.cacheId);
} else {
ticks[calendar.id] = -1;
}
ticks[calendar.id]++;
});
lightning.provider.onResetSync.addListener(async (calendar) => {
console.log("Reset sync for", calendar);
delete ticks[calendar.id];
});
// TODO - see comment in ext-calendar-provider.js. Provider should be registered after first tick so
// onInit handler has a chance to execute, but before the async function is executed.
setTimeout(async () => {
let calendars = await lightning.calendars.query({ type: "ext-" + messenger.runtime.id });
await Promise.all(calendars.map((calendar) => lightning.calendars.remove(calendar.id)));
let calendar = await lightning.calendars.create({
type: "ext-" + messenger.runtime.id,
url: "custom://test",
name: "calendar ext"
});
console.log("create immediate", calendar);
await lightning.calendars.update(calendar.id, { color: "#FF0000", readOnly: true });
let calendar2 = await lightning.calendars.get(calendar.id);
console.log("got calendar", calendar2);
await lightning.calendars.synchronize();
await new Promise(resolve => setTimeout(resolve, 500));
let gotitem = await lightning.items.get(calendar2.id, "findme");
console.log("Retrieved item", gotitem);
let [home, ...rest] = await lightning.calendars.query({ type: "storage" });
console.log("queried calendars", home, rest);
if (!home) {
home = await lightning.calendars.create({
type: "storage",
url: "moz-storage-calendar://",
name: "Home"
});
}
home.enabled = !home.enabled;
await lightning.calendars.update(home.id, { enabled: home.enabled });
if (home.enabled) {
let item = await lightning.items.create(home.id, { type: "event", title: "hello", location: "here", categories: ["Birthdays"], returnFormat: "ical" });
console.log("Created item", item, home);
let updated = await lightning.items.update(home.id, item.id, { title: "world" });
console.log("Updated item", updated);
await new Promise(resolve => setTimeout(resolve, 500));
let home2 = await lightning.calendars.create({
type: "storage",
url: "moz-storage-calendar://",
name: "temp move",
color: "#00FF00"
});
await lightning.items.move(home.id, item.id, home2.id);
await new Promise(resolve => setTimeout(resolve, 1000));
await lightning.items.remove(home2.id, item.id);
await lightning.calendars.remove(home2.id);
}
await new Promise(resolve => setTimeout(resolve, 2000));
}, 2000);