Skip to content

Commit 6d24ac0

Browse files
committed
finish instant requests
1 parent e4bd6ad commit 6d24ac0

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/content-script.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
const callbackRe = /\(\w+,(\w+),(\w+)\)=>.*\2\.(\w+)\(\1,{([^}]+)}/;
6161
const objectKvRe = /(\w+):\(\)=>\w+,?/g;
6262
const scheduleRe = /^\/ftm\/district\/school\/[\w-]+\/flex-period\/schedule$/;
63-
const activityListRe = /^\/ftm\/district\/school\/flex-period\/[\w-]+\/scheduled-activity$/;
64-
const registrationRe = /^\/ftm\/district\/school\/flex-period\/activity\/scheduled-activity\/scheduled-activity-scheduling\/([\w+])\/student\/registration$/;
63+
const activityListRe = /^\/ftm\/district\/school\/flex-period\/([\w-]+)\/scheduled-activity$/;
64+
const registrationRe = /^\/ftm\/district\/school\/flex-period\/activity\/scheduled-activity\/scheduled-activity-scheduling\/([\w-]+)\/student\/registration$/;
6565

6666
function getFunKey(paramObject, idx) {
6767
const matches = [...paramObject.matchAll(objectKvRe)];
@@ -85,6 +85,7 @@
8585
/** @type {{ defaultScheduleTab: string, defaultScreen: string, idbUrl: string, sessionCaching: boolean, instantRequests: boolean } | null} */
8686
let data = null;
8787
let mostRecentActivityItems = null;
88+
let mostRecentActivityScheduleId = null;
8889

8990
const chunks = [
9091
// config chunk (app.config.ts)
@@ -136,22 +137,45 @@
136137
xhrBackend.prototype.handle = new Proxy(xhrBackend.prototype.handle, {
137138
apply(_target, _thisArg, [request]) {
138139
const url = new URL(request.urlWithParams, location.href);
139-
if (url.pathname.match(registrationRe) && data?.instantRequests && request.method === "POST") {
140-
// reflect registration in the cache immediately
141140

141+
const registrationMatch = url.pathname.match(registrationRe);
142+
const activityListMatch = url.pathname.match(activityListRe);
143+
if (registrationMatch && data?.instantRequests && request.method === "POST") {
144+
// reflect registration in the cache immediately
142145
const reqObservable = Reflect.apply(...arguments);
143146

144147
// wrap the original observable
145148
return new Observable(observer => {
146-
reqObservable.subscribe(r => {
149+
reqObservable.subscribe(async r => {
147150
// 4 = successful resposne
148151
if (r.type === 4) {
149-
console.log(url, mostRecentActivityItems);
152+
const item = mostRecentActivityItems?.find(a => a.uuid === registrationMatch[1]);
153+
if (item && mostRecentActivityScheduleId) {
154+
log("instant requests", `auto cached request for ${item.activityDate}`);
155+
const tx = (await db.promise).transaction(["schedulings-cache", "catchall-cache"], "readwrite");
156+
const date = Date.parse(item.scheduledDate);
157+
// delete any old items
158+
const range = IDBKeyRange.only(
159+
[mostRecentActivityScheduleId, date]
160+
);
161+
await tx.objectStore("schedulings-cache").delete(range);
162+
await tx.objectStore("catchall-cache").delete(range);
163+
164+
// cache this item
165+
const dbItem = {
166+
...item,
167+
isRegistered: true,
168+
scheduleUuid: mostRecentActivityScheduleId,
169+
dateNumber: date,
170+
lastAccessed: Date.now()
171+
};
172+
await tx.objectStore("schedulings-cache").put(dbItem);
173+
}
150174
}
151175
observer.next(r);
152176
});
153177
});
154-
} if (url.pathname.match(activityListRe) && data?.instantRequests) {
178+
} else if (activityListMatch && data?.instantRequests) {
155179
// keep track of fetched activity items
156180

157181
const reqObservable = Reflect.apply(...arguments);
@@ -161,12 +185,14 @@
161185
reqObservable.subscribe(r => {
162186
// 4 = successful resposne
163187
if (r.type === 4) {
188+
log("instant requests", "got new activity list");
164189
mostRecentActivityItems = r.body;
190+
mostRecentActivityScheduleId = activityListMatch[1];
165191
}
166192
observer.next(r);
167193
});
168194
});
169-
} if (url.pathname.match(scheduleRe) && data?.sessionCaching) {
195+
} else if (url.pathname.match(scheduleRe) && data?.sessionCaching) {
170196
// cache schedules
171197

172198
return new Observable(observer => {

0 commit comments

Comments
 (0)