|
60 | 60 | const callbackRe = /\(\w+,(\w+),(\w+)\)=>.*\2\.(\w+)\(\1,{([^}]+)}/;
|
61 | 61 | const objectKvRe = /(\w+):\(\)=>\w+,?/g;
|
62 | 62 | 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$/; |
65 | 65 |
|
66 | 66 | function getFunKey(paramObject, idx) {
|
67 | 67 | const matches = [...paramObject.matchAll(objectKvRe)];
|
|
85 | 85 | /** @type {{ defaultScheduleTab: string, defaultScreen: string, idbUrl: string, sessionCaching: boolean, instantRequests: boolean } | null} */
|
86 | 86 | let data = null;
|
87 | 87 | let mostRecentActivityItems = null;
|
| 88 | + let mostRecentActivityScheduleId = null; |
88 | 89 |
|
89 | 90 | const chunks = [
|
90 | 91 | // config chunk (app.config.ts)
|
|
136 | 137 | xhrBackend.prototype.handle = new Proxy(xhrBackend.prototype.handle, {
|
137 | 138 | apply(_target, _thisArg, [request]) {
|
138 | 139 | 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 |
141 | 140 |
|
| 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 |
142 | 145 | const reqObservable = Reflect.apply(...arguments);
|
143 | 146 |
|
144 | 147 | // wrap the original observable
|
145 | 148 | return new Observable(observer => {
|
146 |
| - reqObservable.subscribe(r => { |
| 149 | + reqObservable.subscribe(async r => { |
147 | 150 | // 4 = successful resposne
|
148 | 151 | 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 | + } |
150 | 174 | }
|
151 | 175 | observer.next(r);
|
152 | 176 | });
|
153 | 177 | });
|
154 |
| - } if (url.pathname.match(activityListRe) && data?.instantRequests) { |
| 178 | + } else if (activityListMatch && data?.instantRequests) { |
155 | 179 | // keep track of fetched activity items
|
156 | 180 |
|
157 | 181 | const reqObservable = Reflect.apply(...arguments);
|
|
161 | 185 | reqObservable.subscribe(r => {
|
162 | 186 | // 4 = successful resposne
|
163 | 187 | if (r.type === 4) {
|
| 188 | + log("instant requests", "got new activity list"); |
164 | 189 | mostRecentActivityItems = r.body;
|
| 190 | + mostRecentActivityScheduleId = activityListMatch[1]; |
165 | 191 | }
|
166 | 192 | observer.next(r);
|
167 | 193 | });
|
168 | 194 | });
|
169 |
| - } if (url.pathname.match(scheduleRe) && data?.sessionCaching) { |
| 195 | + } else if (url.pathname.match(scheduleRe) && data?.sessionCaching) { |
170 | 196 | // cache schedules
|
171 | 197 |
|
172 | 198 | return new Observable(observer => {
|
|
0 commit comments